Skip to content

Commit 298a408

Browse files
WinterGengDefTruth
andauthored
[Android] Update ui module structure. (#706)
* Remove redundant code for segmentation. * Classification Documentation and example improvement. * Ocr Documentation and example improvement. * 1.detection、face、seg module add "svPreview.enableCamera();". 2.java/android add fastdepolyUi module. * examples/vision/ to add OCRv3. * Modify UI directory name. * Refresh examples\vision,change OCR README.md * Update ui module * Update ui module Co-authored-by: DefTruth <[email protected]>
1 parent fed3cfc commit 298a408

File tree

129 files changed

+10005
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+10005
-172
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.DS_Store
2+
.idea
3+
.gradle
4+
.cxx
5+
cache
6+
build
7+
app/cache
8+
app/libs/fastdeploy*
9+
app/.cxx
10+
app/build
11+
app/src/main/assets/models/*
12+
app/.gradle
13+
app/.idea
14+
fastdeploy/cache
15+
fastdeploy/libs/fastdeploy*
16+
fastdeploy/.cxx
17+
fastdeploy/build
18+
fastdeploy/src/main/assets/models/*
19+
fastdeploy/.gradle
20+
fastdeploy/.idea
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# OCR文字识别 Android Demo 使用文档
2+
3+
在 Android 上实现实时的OCR文字识别功能,此 Demo 有很好的的易用性和开放性,如在 Demo 中跑自己训练好的模型等。
4+
5+
## 环境准备
6+
7+
1. 在本地环境安装好 Android Studio 工具,详细安装方法请见[Android Stuido 官网](https://developer.android.com/studio)
8+
2. 准备一部 Android 手机,并开启 USB 调试模式。开启方法: `手机设置 -> 查找开发者选项 -> 打开开发者选项和 USB 调试模式`
9+
10+
## 部署步骤
11+
12+
1. OCR文字识别 Demo 位于 `fastdeploy/examples/vision/ocr/PP-OCRv3/android` 目录
13+
2. 用 Android Studio 打开 PP-OCRv3/android 工程
14+
3. 手机连接电脑,打开 USB 调试和文件传输模式,并在 Android Studio 上连接自己的手机设备(手机需要开启允许从 USB 安装软件权限)
15+
16+
<p align="center">
17+
<img width="1440" alt="image" src="https://user-images.githubusercontent.com/31974251/203257262-71b908ab-bb2b-47d3-9efb-67631687b774.png">
18+
</p>
19+
20+
> **注意:**
21+
>> 如果您在导入项目、编译或者运行过程中遇到 NDK 配置错误的提示,请打开 ` File > Project Structure > SDK Location`,修改 `Andriod SDK location` 为您本机配置的 SDK 所在路径。
22+
23+
4. 点击 Run 按钮,自动编译 APP 并安装到手机。(该过程会自动下载预编译的 FastDeploy Android 库 以及 模型文件,需要联网)
24+
成功后效果如下,图一:APP 安装到手机;图二: APP 打开后的效果,会自动识别图片中的物体并标记;图三:APP设置选项,点击右上角的设置图片,可以设置不同选项进行体验。
25+
26+
| APP 图标 | APP 效果 | APP设置项
27+
| --- | --- | --- |
28+
| ![app_pic](https://user-images.githubusercontent.com/14995488/203484427-83de2316-fd60-4baf-93b6-3755f9b5559d.jpg) | ![app_res](https://user-images.githubusercontent.com/14995488/203495616-af42a5b7-d3bc-4fce-8d5e-2ed88454f618.jpg) | ![app_setup](https://user-images.githubusercontent.com/14995488/203484436-57fdd041-7dcc-4e0e-b6cb-43e5ac1e729b.jpg) |
29+
30+
### PP-OCRv2 & PP-OCRv3 Java API 说明
31+
32+
- 模型初始化 API: 模型初始化API包含两种方式,方式一是通过构造函数直接初始化;方式二是,通过调用init函数,在合适的程序节点进行初始化。 PP-OCR初始化参数说明如下:
33+
- modelFile: String, paddle格式的模型文件路径,如 model.pdmodel
34+
- paramFile: String, paddle格式的参数文件路径,如 model.pdiparams
35+
- labelFile: String, 可选参数,表示label标签文件所在路径,用于可视化,如 ppocr_keys_v1.txt,每一行包含一个label
36+
- option: RuntimeOption,可选参数,模型初始化option。如果不传入该参数则会使用默认的运行时选项。
37+
与其他模型不同的是,PP-OCRv2 和 PP-OCRv3 包含 DBDetector、Classifier和Recognizer等基础模型,以及PPOCRv2和PPOCRv3等pipeline类型。
38+
```java
39+
// 构造函数: constructor w/o label file
40+
public DBDetector(String modelFile, String paramsFile);
41+
public DBDetector(String modelFile, String paramsFile, RuntimeOption option);
42+
public Classifier(String modelFile, String paramsFile);
43+
public Classifier(String modelFile, String paramsFile, RuntimeOption option);
44+
public Recognizer(String modelFile, String paramsFile, String labelPath);
45+
public Recognizer(String modelFile, String paramsFile, String labelPath, RuntimeOption option);
46+
public PPOCRv2(); // 空构造函数,之后可以调用init初始化
47+
// Constructor w/o classifier
48+
public PPOCRv2(DBDetector detModel, Recognizer recModel);
49+
public PPOCRv2(DBDetector detModel, Classifier clsModel, Recognizer recModel);
50+
public PPOCRv3(); // 空构造函数,之后可以调用init初始化
51+
// Constructor w/o classifier
52+
public PPOCRv3(DBDetector detModel, Recognizer recModel);
53+
public PPOCRv3(DBDetector detModel, Classifier clsModel, Recognizer recModel);
54+
```
55+
- 模型预测 API:模型预测API包含直接预测的API以及带可视化功能的API。直接预测是指,不保存图片以及不渲染结果到Bitmap上,仅预测推理结果。预测并且可视化是指,预测结果以及可视化,并将可视化后的图片保存到指定的途径,以及将可视化结果渲染在Bitmap(目前支持ARGB8888格式的Bitmap), 后续可将该Bitmap在camera中进行显示。
56+
```java
57+
// 直接预测:不保存图片以及不渲染结果到Bitmap上
58+
public OCRResult predict(Bitmap ARGB8888Bitmap);
59+
// 预测并且可视化:预测结果以及可视化,并将可视化后的图片保存到指定的途径,以及将可视化结果渲染在Bitmap上
60+
public OCRResult predict(Bitmap ARGB8888Bitmap, String savedImagePath);
61+
public OCRResult predict(Bitmap ARGB8888Bitmap, boolean rendering); // 只渲染 不保存图片
62+
```
63+
- 模型资源释放 API:调用 release() API 可以释放模型资源,返回true表示释放成功,false表示失败;调用 initialized() 可以判断模型是否初始化成功,true表示初始化成功,false表示失败。
64+
```java
65+
public boolean release(); // 释放native资源
66+
public boolean initialized(); // 检查是否初始化成功
67+
```
68+
69+
## 替换 FastDeploy SDK和模型
70+
替换FastDeploy预测库和模型的步骤非常简单。预测库所在的位置为 `app/libs/fastdeploy-android-sdk-xxx.aar`,其中 `xxx` 表示当前您使用的预测库版本号。模型所在的位置为,`app/src/main/assets/models`
71+
- 替换FastDeploy Android SDK: 下载或编译最新的FastDeploy Android SDK,解压缩后放在 `app/libs` 目录下;详细配置文档可参考:
72+
- [在 Android 中使用 FastDeploy Java SDK](../../../../../java/android/)
73+
74+
- 替换OCR模型的步骤:
75+
- 将您的OCR模型放在 `app/src/main/assets/models` 目录下;
76+
- 修改 `app/src/main/res/values/strings.xml` 中模型路径的默认值,如:
77+
```xml
78+
<!-- 将这个路径修改成您的模型 -->
79+
<string name="OCR_MODEL_DIR_DEFAULT">models</string>
80+
<string name="OCR_LABEL_PATH_DEFAULT">labels/ppocr_keys_v1.txt</string>
81+
```
82+
83+
## 更多参考文档
84+
如果您想知道更多的FastDeploy Java API文档以及如何通过JNI来接入FastDeploy C++ API感兴趣,可以参考以下内容:
85+
- [在 Android 中使用 FastDeploy Java SDK](../../../../../java/android/)
86+
- [在 Android 中使用 FastDeploy C++ SDK](../../../../../docs/cn/faq/use_cpp_sdk_on_android.md)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import java.security.MessageDigest
2+
3+
apply plugin: 'com.android.application'
4+
5+
android {
6+
compileSdk 28
7+
8+
defaultConfig {
9+
applicationId 'com.baidu.paddle.fastdeploy.app.examples'
10+
minSdkVersion 15
11+
//noinspection ExpiredTargetSdkVersion
12+
targetSdkVersion 28
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
25+
}
26+
27+
dependencies {
28+
implementation fileTree(include: ['*.aar'], dir: 'libs')
29+
implementation 'com.android.support:appcompat-v7:28.0.0'
30+
//noinspection GradleDependency
31+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
32+
implementation 'com.android.support:design:28.0.0'
33+
implementation 'org.jetbrains:annotations:15.0'
34+
//noinspection GradleDependency
35+
testImplementation 'junit:junit:4.12'
36+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
37+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
38+
}
39+
40+
def FD_MODEL = [
41+
[
42+
'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ch_PP-OCRv3_det_infer.tgz',
43+
'dest': 'src/main/assets/models'
44+
],
45+
[
46+
'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ch_ppocr_mobile_v2.0_cls_infer.tgz',
47+
'dest': 'src/main/assets/models'
48+
],
49+
[
50+
'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ch_PP-OCRv3_rec_infer.tgz',
51+
'dest': 'src/main/assets/models'
52+
]
53+
]
54+
55+
def FD_JAVA_SDK = [
56+
[
57+
'src' : 'https://bj.bcebos.com/fastdeploy/test/fastdeploy-android-sdk-latest-dev.aar',
58+
'dest': 'libs'
59+
]
60+
]
61+
62+
task downloadAndExtractModels(type: DefaultTask) {
63+
doFirst {
64+
println "Downloading and extracting fastdeploy models ..."
65+
}
66+
doLast {
67+
String cachePath = "cache"
68+
if (!file("${cachePath}").exists()) {
69+
mkdir "${cachePath}"
70+
}
71+
FD_MODEL.eachWithIndex { model, index ->
72+
MessageDigest messageDigest = MessageDigest.getInstance('MD5')
73+
messageDigest.update(model.src.bytes)
74+
String[] modelPaths = model.src.split("/")
75+
String modelName = modelPaths[modelPaths.length - 1]
76+
// Download the target model if not exists
77+
boolean copyFiles = !file("${model.dest}").exists()
78+
if (!file("${cachePath}/${modelName}").exists()) {
79+
println "Downloading ${model.src} -> ${cachePath}/${modelName}"
80+
ant.get(src: model.src, dest: file("${cachePath}/${modelName}"))
81+
copyFiles = true
82+
}
83+
if (copyFiles) {
84+
println "Coping ${cachePath}/${modelName} -> ${model.dest}"
85+
copy {
86+
from tarTree("${cachePath}/${modelName}")
87+
into "${model.dest}"
88+
}
89+
}
90+
}
91+
}
92+
}
93+
94+
task downloadAndExtractSDKs(type: DefaultTask) {
95+
doFirst {
96+
println "Downloading and extracting fastdeploy android java sdk ..."
97+
}
98+
doLast {
99+
String cachePath = "cache"
100+
if (!file("${cachePath}").exists()) {
101+
mkdir "${cachePath}"
102+
}
103+
FD_JAVA_SDK.eachWithIndex { sdk, index ->
104+
String[] sdkPaths = sdk.src.split("/")
105+
String sdkName = sdkPaths[sdkPaths.length - 1]
106+
// Download the target SDK if not exists
107+
boolean copyFiles = !file("${sdk.dest}/${sdkName}").exists()
108+
if (!file("${cachePath}/${sdkName}").exists()) {
109+
println "Downloading ${sdk.src} -> ${cachePath}/${sdkName}"
110+
ant.get(src: sdk.src, dest: file("${cachePath}/${sdkName}"))
111+
copyFiles = true
112+
}
113+
if (copyFiles) {
114+
println "Coping ${cachePath}/${sdkName} -> ${sdk.dest}/${sdkName}"
115+
copy {
116+
from "${cachePath}/${sdkName}"
117+
into "${sdk.dest}"
118+
}
119+
}
120+
}
121+
}
122+
}
123+
124+
preBuild.dependsOn downloadAndExtractSDKs
125+
preBuild.dependsOn downloadAndExtractModels
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

java/android/app/src/androidTest/java/com/baidu/paddle/fastdeploy/ExampleInstrumentedTest.java renamed to examples/vision/ocr/PP-OCRv3/android/app/src/androidTest/java/com/baidu/paddle/fastdeploy/ExampleInstrumentedTest.java

File renamed without changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.baidu.paddle.fastdeploy.app.examples">
4+
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
7+
<uses-permission android:name="android.permission.CAMERA"/>
8+
<uses-feature android:name="android.hardware.camera" />
9+
<uses-feature android:name="android.hardware.camera.autofocus" />
10+
11+
<application
12+
android:allowBackup="true"
13+
android:icon="@mipmap/ic_launcher"
14+
android:label="@string/app_name"
15+
android:roundIcon="@mipmap/ic_launcher_round"
16+
android:supportsRtl="true"
17+
android:theme="@style/AppTheme">
18+
<activity android:name=".ocr.OcrMainActivity">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN"/>
21+
<category android:name="android.intent.category.LAUNCHER"/>
22+
</intent-filter>
23+
</activity>
24+
<activity
25+
android:name=".ocr.OcrSettingsActivity"
26+
android:label="Settings">
27+
</activity>
28+
</application>
29+
30+
</manifest>

0 commit comments

Comments
 (0)