Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleOCR
提交
3ff9cc93
P
PaddleOCR
项目概览
PaddlePaddle
/
PaddleOCR
大约 1 年 前同步成功
通知
1528
Star
32962
Fork
6643
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
108
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleOCR
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
108
Issue
108
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3ff9cc93
编写于
4月 09, 2021
作者:
W
WenmuZhou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update ui
上级
7a32e25c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
135 addition
and
54 deletion
+135
-54
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
+84
-52
deploy/android_demo/app/src/main/res/layout/activity_main.xml
...oy/android_demo/app/src/main/res/layout/activity_main.xml
+51
-2
未找到文件。
deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java
浏览文件 @
3ff9cc93
...
...
@@ -10,7 +10,9 @@ import android.content.pm.PackageManager;
import
android.database.Cursor
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.graphics.drawable.BitmapDrawable
;
import
android.media.ExifInterface
;
import
android.content.res.AssetManager
;
import
android.net.Uri
;
import
android.os.Bundle
;
import
android.os.Environment
;
...
...
@@ -24,6 +26,7 @@ import android.util.Log;
import
android.view.Menu
;
import
android.view.MenuInflater
;
import
android.view.MenuItem
;
import
android.view.View
;
import
android.widget.ImageView
;
import
android.widget.TextView
;
import
android.widget.Toast
;
...
...
@@ -61,6 +64,7 @@ public class MainActivity extends AppCompatActivity {
// UI components of object detection
protected
TextView
tvInputSetting
;
protected
TextView
tvStatus
;
protected
ImageView
ivInputImage
;
protected
TextView
tvOutputResult
;
protected
TextView
tvInferenceTime
;
...
...
@@ -77,6 +81,7 @@ public class MainActivity extends AppCompatActivity {
protected
float
[]
inputStd
=
new
float
[]{};
protected
float
scoreThreshold
=
0.1f
;
private
String
currentPhotoPath
;
private
AssetManager
assetManager
=
null
;
protected
Predictor
predictor
=
new
Predictor
();
...
...
@@ -89,7 +94,16 @@ public class MainActivity extends AppCompatActivity {
SharedPreferences
sharedPreferences
=
PreferenceManager
.
getDefaultSharedPreferences
(
this
);
SharedPreferences
.
Editor
editor
=
sharedPreferences
.
edit
();
editor
.
clear
();
editor
.
commit
();
editor
.
apply
();
// Setup the UI components
tvInputSetting
=
findViewById
(
R
.
id
.
tv_input_setting
);
tvStatus
=
findViewById
(
R
.
id
.
tv_model_img_status
);
ivInputImage
=
findViewById
(
R
.
id
.
iv_input_image
);
tvInferenceTime
=
findViewById
(
R
.
id
.
tv_inference_time
);
tvOutputResult
=
findViewById
(
R
.
id
.
tv_output_result
);
tvInputSetting
.
setMovementMethod
(
ScrollingMovementMethod
.
getInstance
());
tvOutputResult
.
setMovementMethod
(
ScrollingMovementMethod
.
getInstance
());
// Prepare the worker thread for mode loading and inference
receiver
=
new
Handler
()
{
...
...
@@ -97,20 +111,28 @@ public class MainActivity extends AppCompatActivity {
public
void
handleMessage
(
Message
msg
)
{
switch
(
msg
.
what
)
{
case
RESPONSE_LOAD_MODEL_SUCCESSED:
pbLoadModel
.
dismiss
();
if
(
pbLoadModel
!=
null
&&
pbLoadModel
.
isShowing
()){
pbLoadModel
.
dismiss
();
}
onLoadModelSuccessed
();
break
;
case
RESPONSE_LOAD_MODEL_FAILED:
pbLoadModel
.
dismiss
();
if
(
pbLoadModel
!=
null
&&
pbLoadModel
.
isShowing
()){
pbLoadModel
.
dismiss
();
}
Toast
.
makeText
(
MainActivity
.
this
,
"Load model failed!"
,
Toast
.
LENGTH_SHORT
).
show
();
onLoadModelFailed
();
break
;
case
RESPONSE_RUN_MODEL_SUCCESSED:
pbRunModel
.
dismiss
();
if
(
pbRunModel
!=
null
&&
pbRunModel
.
isShowing
()){
pbRunModel
.
dismiss
();
}
onRunModelSuccessed
();
break
;
case
RESPONSE_RUN_MODEL_FAILED:
pbRunModel
.
dismiss
();
if
(
pbRunModel
!=
null
&&
pbRunModel
.
isShowing
()){
pbRunModel
.
dismiss
();
}
Toast
.
makeText
(
MainActivity
.
this
,
"Run model failed!"
,
Toast
.
LENGTH_SHORT
).
show
();
onRunModelFailed
();
break
;
...
...
@@ -146,14 +168,6 @@ public class MainActivity extends AppCompatActivity {
}
}
};
// Setup the UI components
tvInputSetting
=
findViewById
(
R
.
id
.
tv_input_setting
);
ivInputImage
=
findViewById
(
R
.
id
.
iv_input_image
);
tvInferenceTime
=
findViewById
(
R
.
id
.
tv_inference_time
);
tvOutputResult
=
findViewById
(
R
.
id
.
tv_output_result
);
tvInputSetting
.
setMovementMethod
(
ScrollingMovementMethod
.
getInstance
());
tvOutputResult
.
setMovementMethod
(
ScrollingMovementMethod
.
getInstance
());
}
@Override
...
...
@@ -224,17 +238,18 @@ public class MainActivity extends AppCompatActivity {
" Thread Num: "
+
Integer
.
toString
(
cpuThreadNum
)
+
"\n"
+
"CPU Power Mode: "
+
cpuPowerMode
);
tvInputSetting
.
scrollTo
(
0
,
0
);
// Reload model if configure has been changed
loadModel
();
// loadModel();
set_img
();
}
}
public
void
loadModel
()
{
pbLoadModel
=
ProgressDialog
.
show
(
this
,
""
,
"
L
oading model..."
,
false
,
false
);
pbLoadModel
=
ProgressDialog
.
show
(
this
,
""
,
"
l
oading model..."
,
false
,
false
);
sender
.
sendEmptyMessage
(
REQUEST_LOAD_MODEL
);
}
public
void
runModel
()
{
pbRunModel
=
ProgressDialog
.
show
(
this
,
""
,
"
R
unning model..."
,
false
,
false
);
pbRunModel
=
ProgressDialog
.
show
(
this
,
""
,
"
r
unning model..."
,
false
,
false
);
sender
.
sendEmptyMessage
(
REQUEST_RUN_MODEL
);
}
...
...
@@ -252,36 +267,15 @@ public class MainActivity extends AppCompatActivity {
public
void
onLoadModelSuccessed
()
{
// Load test image from path and run model
try
{
if
(
imagePath
.
isEmpty
())
{
return
;
}
Bitmap
image
=
null
;
// Read test image file from custom path if the first character of mode path is '/', otherwise read test
// image file from assets
if
(!
imagePath
.
substring
(
0
,
1
).
equals
(
"/"
))
{
InputStream
imageStream
=
getAssets
().
open
(
imagePath
);
image
=
BitmapFactory
.
decodeStream
(
imageStream
);
}
else
{
if
(!
new
File
(
imagePath
).
exists
())
{
return
;
}
image
=
BitmapFactory
.
decodeFile
(
imagePath
);
}
if
(
image
!=
null
&&
predictor
.
isLoaded
())
{
predictor
.
setInputImage
(
image
);
runModel
();
}
}
catch
(
IOException
e
)
{
Toast
.
makeText
(
MainActivity
.
this
,
"Load image failed!"
,
Toast
.
LENGTH_SHORT
).
show
();
e
.
printStackTrace
();
}
tvStatus
.
setText
(
"STATUS: load model successed"
);
}
public
void
onLoadModelFailed
()
{
tvStatus
.
setText
(
"STATUS: load model failed"
);
}
public
void
onRunModelSuccessed
()
{
tvStatus
.
setText
(
"STATUS: run model successed"
);
// Obtain results and update UI
tvInferenceTime
.
setText
(
"Inference time: "
+
predictor
.
inferenceTime
()
+
" ms"
);
Bitmap
outputImage
=
predictor
.
outputImage
();
...
...
@@ -293,6 +287,7 @@ public class MainActivity extends AppCompatActivity {
}
public
void
onRunModelFailed
()
{
tvStatus
.
setText
(
"STATUS: run model failed"
);
}
public
void
onImageChanged
(
Bitmap
image
)
{
...
...
@@ -303,6 +298,19 @@ public class MainActivity extends AppCompatActivity {
}
}
public
void
set_img
()
{
// Load test image from path and run model
try
{
assetManager
=
getAssets
();
InputStream
in
=
assetManager
.
open
(
imagePath
);
Bitmap
bmp
=
BitmapFactory
.
decodeStream
(
in
);
ivInputImage
.
setImageBitmap
(
bmp
);
}
catch
(
IOException
e
)
{
Toast
.
makeText
(
MainActivity
.
this
,
"Load image failed!"
,
Toast
.
LENGTH_SHORT
).
show
();
e
.
printStackTrace
();
}
}
public
void
onSettingsClicked
()
{
startActivity
(
new
Intent
(
MainActivity
.
this
,
SettingsActivity
.
class
));
}
...
...
@@ -316,8 +324,6 @@ public class MainActivity extends AppCompatActivity {
public
boolean
onPrepareOptionsMenu
(
Menu
menu
)
{
boolean
isLoaded
=
predictor
.
isLoaded
();
menu
.
findItem
(
R
.
id
.
open_gallery
).
setEnabled
(
isLoaded
);
menu
.
findItem
(
R
.
id
.
take_photo
).
setEnabled
(
isLoaded
);
return
super
.
onPrepareOptionsMenu
(
menu
);
}
...
...
@@ -327,16 +333,6 @@ public class MainActivity extends AppCompatActivity {
case
android
.
R
.
id
.
home
:
finish
();
break
;
case
R
.
id
.
open_gallery
:
if
(
requestAllPermissions
())
{
openGallery
();
}
break
;
case
R
.
id
.
take_photo
:
if
(
requestAllPermissions
())
{
takePhoto
();
}
break
;
case
R
.
id
.
settings
:
if
(
requestAllPermissions
())
{
// Make sure we have SDCard r&w permissions to load model from SDCard
...
...
@@ -434,6 +430,9 @@ public class MainActivity extends AppCompatActivity {
Cursor
cursor
=
managedQuery
(
uri
,
proj
,
null
,
null
,
null
);
cursor
.
moveToFirst
();
onImageChanged
(
image
);
if
(
image
!=
null
)
{
ivInputImage
.
setImageBitmap
(
image
);
}
}
catch
(
IOException
e
)
{
Log
.
e
(
TAG
,
e
.
toString
());
}
...
...
@@ -452,6 +451,9 @@ public class MainActivity extends AppCompatActivity {
Bitmap
image
=
BitmapFactory
.
decodeFile
(
currentPhotoPath
);
image
=
Utils
.
rotateBitmap
(
image
,
orientation
);
onImageChanged
(
image
);
if
(
image
!=
null
)
{
ivInputImage
.
setImageBitmap
(
image
);
}
}
else
{
Log
.
e
(
TAG
,
"currentPhotoPath is null"
);
}
...
...
@@ -462,6 +464,36 @@ public class MainActivity extends AppCompatActivity {
}
}
public
void
btn_load_model_click
(
View
view
)
{
tvStatus
.
setText
(
"STATUS: load model ......"
);
loadModel
();
}
public
void
btn_run_model_click
(
View
view
)
{
Bitmap
image
=((
BitmapDrawable
)
ivInputImage
.
getDrawable
()).
getBitmap
();
if
(
image
==
null
)
{
tvStatus
.
setText
(
"STATUS: image is not exists"
);
}
else
if
(!
predictor
.
isLoaded
()){
tvStatus
.
setText
(
"STATUS: model is not loaded"
);
}
else
{
tvStatus
.
setText
(
"STATUS: run model ...... "
);
predictor
.
setInputImage
(
image
);
runModel
();
}
}
public
void
btn_choice_img_click
(
View
view
)
{
if
(
requestAllPermissions
())
{
openGallery
();
}
}
public
void
btn_take_photo_click
(
View
view
)
{
if
(
requestAllPermissions
())
{
takePhoto
();
}
}
@Override
protected
void
onDestroy
()
{
if
(
predictor
!=
null
)
{
...
...
deploy/android_demo/app/src/main/res/layout/activity_main.xml
浏览文件 @
3ff9cc93
...
...
@@ -16,6 +16,43 @@
android:layout_height=
"wrap_content"
android:layout_alignParentTop=
"true"
android:orientation=
"vertical"
>
<LinearLayout
android:id=
"@+id/btn_layout"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
>
<Button
android:id=
"@+id/btn_load_model"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:onClick=
"btn_load_model_click"
android:text=
"加载模型"
/>
<Button
android:id=
"@+id/btn_run_model"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:onClick=
"btn_run_model_click"
android:text=
"运行模型"
/>
<Button
android:id=
"@+id/btn_take_photo"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:onClick=
"btn_take_photo_click"
android:text=
"拍照识别"
/>
<Button
android:id=
"@+id/btn_choice_img"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:onClick=
"btn_choice_img_click"
android:text=
"选取图片"
/>
</LinearLayout>
<TextView
android:id=
"@+id/tv_input_setting"
android:layout_width=
"wrap_content"
...
...
@@ -29,6 +66,19 @@
android:singleLine=
"false"
android:maxLines=
"6"
android:text=
""
/>
<TextView
android:id=
"@+id/tv_model_img_status"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:scrollbars=
"vertical"
android:layout_marginLeft=
"12dp"
android:layout_marginRight=
"12dp"
android:layout_marginTop=
"-5dp"
android:layout_marginBottom=
"5dp"
android:lineSpacingExtra=
"4dp"
android:singleLine=
"false"
android:maxLines=
"6"
android:text=
"STATUS: ok"
/>
</LinearLayout>
...
...
@@ -95,5 +145,4 @@
</RelativeLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
</androidx.constraintlayout.widget.ConstraintLayout>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录