Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleOCR
提交
999b67ce
P
PaddleOCR
项目概览
s920243400
/
PaddleOCR
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleOCR
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
999b67ce
编写于
7月 22, 2020
作者:
D
Double_V
提交者:
GitHub
7月 22, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #397 from authorfu/develop
修正拍照不清晰的问题
上级
9d33e36d
4cbcee2d
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
131 addition
and
16 deletion
+131
-16
deploy/android_demo/app/src/main/AndroidManifest.xml
deploy/android_demo/app/src/main/AndroidManifest.xml
+9
-0
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java
...ain/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java
+65
-7
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/OCRPredictorNative.java
...va/com/baidu/paddle/lite/demo/ocr/OCRPredictorNative.java
+2
-2
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java
...c/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java
+5
-7
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Utils.java
...p/src/main/java/com/baidu/paddle/lite/demo/ocr/Utils.java
+46
-0
deploy/android_demo/app/src/main/res/xml/file_paths.xml
deploy/android_demo/app/src/main/res/xml/file_paths.xml
+4
-0
未找到文件。
deploy/android_demo/app/src/main/AndroidManifest.xml
浏览文件 @
999b67ce
...
...
@@ -25,6 +25,15 @@
android:name=
"com.baidu.paddle.lite.demo.ocr.SettingsActivity"
android:label=
"Settings"
>
</activity>
<provider
android:name=
"android.support.v4.content.FileProvider"
android:authorities=
"com.baidu.paddle.lite.demo.ocr.fileprovider"
android:exported=
"false"
android:grantUriPermissions=
"true"
>
<meta-data
android:name=
"android.support.FILE_PROVIDER_PATHS"
android:resource=
"@xml/file_paths"
></meta-data>
</provider>
</application>
</manifest>
\ No newline at end of file
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java
浏览文件 @
999b67ce
...
...
@@ -3,14 +3,17 @@ package com.baidu.paddle.lite.demo.ocr;
import
android.Manifest
;
import
android.app.ProgressDialog
;
import
android.content.ContentResolver
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.SharedPreferences
;
import
android.content.pm.PackageManager
;
import
android.database.Cursor
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.media.ExifInterface
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.os.Environment
;
import
android.os.Handler
;
import
android.os.HandlerThread
;
import
android.os.Message
;
...
...
@@ -19,6 +22,7 @@ import android.provider.MediaStore;
import
android.support.annotation.NonNull
;
import
android.support.v4.app.ActivityCompat
;
import
android.support.v4.content.ContextCompat
;
import
android.support.v4.content.FileProvider
;
import
android.support.v7.app.AppCompatActivity
;
import
android.text.method.ScrollingMovementMethod
;
import
android.util.Log
;
...
...
@@ -32,6 +36,8 @@ import android.widget.Toast;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
MainActivity
extends
AppCompatActivity
{
private
static
final
String
TAG
=
MainActivity
.
class
.
getSimpleName
();
...
...
@@ -69,6 +75,7 @@ public class MainActivity extends AppCompatActivity {
protected
float
[]
inputMean
=
new
float
[]{};
protected
float
[]
inputStd
=
new
float
[]{};
protected
float
scoreThreshold
=
0.1f
;
private
String
currentPhotoPath
;
protected
Predictor
predictor
=
new
Predictor
();
...
...
@@ -368,18 +375,56 @@ public class MainActivity extends AppCompatActivity {
}
private
void
takePhoto
()
{
Intent
takePhotoIntent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
);
if
(
takePhotoIntent
.
resolveActivity
(
getPackageManager
())
!=
null
)
{
startActivityForResult
(
takePhotoIntent
,
TAKE_PHOTO_REQUEST_CODE
);
Intent
takePictureIntent
=
new
Intent
(
MediaStore
.
ACTION_IMAGE_CAPTURE
);
// Ensure that there's a camera activity to handle the intent
if
(
takePictureIntent
.
resolveActivity
(
getPackageManager
())
!=
null
)
{
// Create the File where the photo should go
File
photoFile
=
null
;
try
{
photoFile
=
createImageFile
();
}
catch
(
IOException
ex
)
{
Log
.
e
(
"MainActitity"
,
ex
.
getMessage
(),
ex
);
Toast
.
makeText
(
MainActivity
.
this
,
"Create Camera temp file failed: "
+
ex
.
getMessage
(),
Toast
.
LENGTH_SHORT
).
show
();
}
// Continue only if the File was successfully created
if
(
photoFile
!=
null
)
{
Log
.
i
(
TAG
,
"FILEPATH "
+
getExternalFilesDir
(
"Pictures"
).
getAbsolutePath
());
Uri
photoURI
=
FileProvider
.
getUriForFile
(
this
,
"com.baidu.paddle.lite.demo.ocr.fileprovider"
,
photoFile
);
currentPhotoPath
=
photoFile
.
getAbsolutePath
();
takePictureIntent
.
putExtra
(
MediaStore
.
EXTRA_OUTPUT
,
photoURI
);
startActivityForResult
(
takePictureIntent
,
TAKE_PHOTO_REQUEST_CODE
);
Log
.
i
(
TAG
,
"startActivityForResult finished"
);
}
}
}
private
File
createImageFile
()
throws
IOException
{
// Create an image file name
String
timeStamp
=
new
SimpleDateFormat
(
"yyyyMMdd_HHmmss"
).
format
(
new
Date
());
String
imageFileName
=
"JPEG_"
+
timeStamp
+
"_"
;
File
storageDir
=
getExternalFilesDir
(
Environment
.
DIRECTORY_PICTURES
);
File
image
=
File
.
createTempFile
(
imageFileName
,
/* prefix */
".bmp"
,
/* suffix */
storageDir
/* directory */
);
return
image
;
}
@Override
protected
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
super
.
onActivityResult
(
requestCode
,
resultCode
,
data
);
if
(
resultCode
==
RESULT_OK
&&
data
!=
null
)
{
if
(
resultCode
==
RESULT_OK
)
{
switch
(
requestCode
)
{
case
OPEN_GALLERY_REQUEST_CODE:
if
(
data
==
null
)
{
break
;
}
try
{
ContentResolver
resolver
=
getContentResolver
();
Uri
uri
=
data
.
getData
();
...
...
@@ -393,9 +438,22 @@ public class MainActivity extends AppCompatActivity {
}
break
;
case
TAKE_PHOTO_REQUEST_CODE:
Bundle
extras
=
data
.
getExtras
();
Bitmap
image
=
(
Bitmap
)
extras
.
get
(
"data"
);
onImageChanged
(
image
);
if
(
currentPhotoPath
!=
null
)
{
ExifInterface
exif
=
null
;
try
{
exif
=
new
ExifInterface
(
currentPhotoPath
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
int
orientation
=
exif
.
getAttributeInt
(
ExifInterface
.
TAG_ORIENTATION
,
ExifInterface
.
ORIENTATION_UNDEFINED
);
Log
.
i
(
TAG
,
"rotation "
+
orientation
);
Bitmap
image
=
BitmapFactory
.
decodeFile
(
currentPhotoPath
);
image
=
Utils
.
rotateBitmap
(
image
,
orientation
);
onImageChanged
(
image
);
}
else
{
Log
.
e
(
TAG
,
"currentPhotoPath is null"
);
}
break
;
default
:
break
;
...
...
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/OCRPredictorNative.java
浏览文件 @
999b67ce
...
...
@@ -35,8 +35,8 @@ public class OCRPredictorNative {
}
public
void
release
(){
if
(
nativePointer
!=
0
){
public
void
release
()
{
if
(
nativePointer
!=
0
)
{
nativePointer
=
0
;
destory
(
nativePointer
);
}
...
...
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java
浏览文件 @
999b67ce
...
...
@@ -127,12 +127,12 @@ public class Predictor {
}
public
void
releaseModel
()
{
if
(
paddlePredictor
!=
null
){
if
(
paddlePredictor
!=
null
)
{
paddlePredictor
.
release
();
paddlePredictor
=
null
;
}
isLoaded
=
false
;
cpuThreadNum
=
4
;
cpuThreadNum
=
1
;
cpuPowerMode
=
"LITE_POWER_HIGH"
;
modelPath
=
""
;
modelName
=
""
;
...
...
@@ -287,9 +287,7 @@ public class Predictor {
if
(
image
==
null
)
{
return
;
}
// Scale image to the size of input tensor
Bitmap
rgbaImage
=
image
.
copy
(
Bitmap
.
Config
.
ARGB_8888
,
true
);
this
.
inputImage
=
rgbaImage
;
this
.
inputImage
=
image
.
copy
(
Bitmap
.
Config
.
ARGB_8888
,
true
);
}
private
ArrayList
<
OcrResultModel
>
postprocess
(
ArrayList
<
OcrResultModel
>
results
)
{
...
...
@@ -310,7 +308,7 @@ public class Predictor {
private
void
drawResults
(
ArrayList
<
OcrResultModel
>
results
)
{
StringBuffer
outputResultSb
=
new
StringBuffer
(
""
);
for
(
int
i
=
0
;
i
<
results
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
results
.
size
();
i
++)
{
OcrResultModel
result
=
results
.
get
(
i
);
StringBuilder
sb
=
new
StringBuilder
(
""
);
sb
.
append
(
result
.
getLabel
());
...
...
@@ -320,7 +318,7 @@ public class Predictor {
sb
.
append
(
"("
).
append
(
p
.
x
).
append
(
","
).
append
(
p
.
y
).
append
(
") "
);
}
Log
.
i
(
TAG
,
sb
.
toString
());
outputResultSb
.
append
(
i
+
1
).
append
(
": "
).
append
(
result
.
getLabel
()).
append
(
"\n"
);
outputResultSb
.
append
(
i
+
1
).
append
(
": "
).
append
(
result
.
getLabel
()).
append
(
"\n"
);
}
outputResult
=
outputResultSb
.
toString
();
outputImage
=
inputImage
;
...
...
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Utils.java
浏览文件 @
999b67ce
...
...
@@ -2,6 +2,8 @@ package com.baidu.paddle.lite.demo.ocr;
import
android.content.Context
;
import
android.graphics.Bitmap
;
import
android.graphics.Matrix
;
import
android.media.ExifInterface
;
import
android.os.Environment
;
import
java.io.*
;
...
...
@@ -110,4 +112,48 @@ public class Utils {
}
return
Bitmap
.
createScaledBitmap
(
bitmap
,
newWidth
,
newHeight
,
true
);
}
public
static
Bitmap
rotateBitmap
(
Bitmap
bitmap
,
int
orientation
)
{
Matrix
matrix
=
new
Matrix
();
switch
(
orientation
)
{
case
ExifInterface
.
ORIENTATION_NORMAL
:
return
bitmap
;
case
ExifInterface
.
ORIENTATION_FLIP_HORIZONTAL
:
matrix
.
setScale
(-
1
,
1
);
break
;
case
ExifInterface
.
ORIENTATION_ROTATE_180
:
matrix
.
setRotate
(
180
);
break
;
case
ExifInterface
.
ORIENTATION_FLIP_VERTICAL
:
matrix
.
setRotate
(
180
);
matrix
.
postScale
(-
1
,
1
);
break
;
case
ExifInterface
.
ORIENTATION_TRANSPOSE
:
matrix
.
setRotate
(
90
);
matrix
.
postScale
(-
1
,
1
);
break
;
case
ExifInterface
.
ORIENTATION_ROTATE_90
:
matrix
.
setRotate
(
90
);
break
;
case
ExifInterface
.
ORIENTATION_TRANSVERSE
:
matrix
.
setRotate
(-
90
);
matrix
.
postScale
(-
1
,
1
);
break
;
case
ExifInterface
.
ORIENTATION_ROTATE_270
:
matrix
.
setRotate
(-
90
);
break
;
default
:
return
bitmap
;
}
try
{
Bitmap
bmRotated
=
Bitmap
.
createBitmap
(
bitmap
,
0
,
0
,
bitmap
.
getWidth
(),
bitmap
.
getHeight
(),
matrix
,
true
);
bitmap
.
recycle
();
return
bmRotated
;
}
catch
(
OutOfMemoryError
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
deploy/android_demo/app/src/main/res/xml/file_paths.xml
0 → 100644
浏览文件 @
999b67ce
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<external-files-path
name=
"my_images"
path=
"Pictures"
/>
</paths>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录