Skip to content

Commit 0d68d8a

Browse files
WinterGengDefTruth
andauthored
[Android] Add classification and ocr app examples (#676)
* Remove redundant code for segmentation. * Classification Documentation and example improvement. * Ocr Documentation and example improvement. Co-authored-by: DefTruth <[email protected]>
1 parent 27e68a0 commit 0d68d8a

File tree

134 files changed

+10872
-2268
lines changed

Some content is hidden

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

134 files changed

+10872
-2268
lines changed

examples/vision/classification/paddleclas/android/.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ app/build
1111
app/src/main/assets/models/*
1212
app/.gradle
1313
app/.idea
14-
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

examples/vision/classification/paddleclas/android/README.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
1. 在本地环境安装好 Android Studio 工具,详细安装方法请见[Android Stuido 官网](https://developer.android.com/studio)
88
2. 准备一部 Android 手机,并开启 USB 调试模式。开启方法: `手机设置 -> 查找开发者选项 -> 打开开发者选项和 USB 调试模式`
99

10-
**注意**:如果您的 Android Studio 尚未配置 NDK ,请根据 Android Studio 用户指南中的[安装及配置 NDK 和 CMake ](https://developer.android.com/studio/projects/install-ndk)内容,预先配置好 NDK 。您可以选择最新的 NDK 版本,或者使用 FastDeploy Android 预测库版本一样的 NDK
11-
1210
## 部署步骤
1311

1412
1. 目标检测 PaddleClas Demo 位于 `fastdeploy/examples/vision/classification/paddleclas/android` 目录
@@ -20,17 +18,14 @@
2018
</p>
2119

2220
> **注意:**
23-
>> 如果您在导入项目、编译或者运行过程中遇到 NDK 配置错误的提示,请打开 ` File > Project Structure > SDK Location`,修改 `Andriod NDK location` 为您本机配置的 NDK 所在路径。本工程默认使用的NDK版本为20.
24-
>> 如果您是通过 Andriod Studio 的 SDK Tools 下载的 NDK (见本章节"环境准备"),可以直接点击下拉框选择默认路径。
25-
>> 还有一种 NDK 配置方法,你可以在 `paddleclas/android/local.properties` 文件中手动完成 NDK 路径配置,如下图所示
26-
>> 如果以上步骤仍旧无法解决 NDK 配置错误,请尝试根据 Andriod Studio 官方文档中的[更新 Android Gradle 插件](https://developer.android.com/studio/releases/gradle-plugin?hl=zh-cn#updating-plugin)章节,尝试更新Android Gradle plugin版本。
21+
>> 如果您在导入项目、编译或者运行过程中遇到 NDK 配置错误的提示,请打开 ` File > Project Structure > SDK Location`,修改 `Andriod SDK location` 为您本机配置的 SDK 所在路径。
2722
2823
4. 点击 Run 按钮,自动编译 APP 并安装到手机。(该过程会自动下载预编译的 FastDeploy Android 库,需要联网)
2924
成功后效果如下,图一:APP 安装到手机;图二: APP 打开后的效果,会自动识别图片中的物体并标记;图三:APP设置选项,点击右上角的设置图片,可以设置不同选项进行体验。
3025

3126
| APP 图标 | APP 效果 | APP设置项
3227
| --- | --- | --- |
33-
| ![app_pic ](https://user-images.githubusercontent.com/31974251/197170082-a2bdd49d-60ea-4df0-af63-18ed898a746e.jpg) | ![app_res](https://user-images.githubusercontent.com/31974251/197339363-ae7acd5d-88b8-4365-aea5-b27826c6a25f.jpg) | ![app_setup](https://user-images.githubusercontent.com/31974251/197339378-bb30b108-2d77-4b30-981d-d687b6fca8f6.jpg) |
28+
| ![app_pic ](https://user-images.githubusercontent.com/14995488/203484427-83de2316-fd60-4baf-93b6-3755f9b5559d.jpg) | ![app_res](https://user-images.githubusercontent.com/14995488/203494666-16528cb3-0ce2-48fc-9f9e-37da17b2c2f6.jpg) | ![app_setup](https://user-images.githubusercontent.com/14995488/203484436-57fdd041-7dcc-4e0e-b6cb-43e5ac1e729b.jpg) |
3429

3530
## PaddleClasModel Java API 说明
3631
- 模型初始化 API: 模型初始化API包含两种方式,方式一是通过构造函数直接初始化;方式二是,通过调用init函数,在合适的程序节点进行初始化。PaddleClasModel初始化参数说明如下:
@@ -145,11 +140,11 @@ set(FastDeploy_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/fastdeploy-android
145140
- 修改 `app/src/main/res/values/strings.xml` 中模型路径的默认值,如:
146141
```xml
147142
<!-- 将这个路径指修改成您的模型,如 models/MobileNetV2_x0_25_infer -->
148-
<string name="MODEL_DIR_DEFAULT">models/MobileNetV1_x0_25_infer</string>
149-
<string name="LABEL_PATH_DEFAULT">labels/imagenet1k_label_list.txt</string>
143+
<string name="CLASSIFICATION_MODEL_DIR_DEFAULT">models/MobileNetV1_x0_25_infer</string>
144+
<string name="CLASSIFICATION_LABEL_PATH_DEFAULT">labels/imagenet1k_label_list.txt</string>
150145
```
151146

152-
## 如何通过 JNI 在 Native 层接入 FastDeploy C++ API ?
153-
如果您对如何通过JNI来接入FastDeploy C++ API感兴趣,可以参考以下内容:
154-
- [app/src/main/cpp 代码实现](./app/src/main/cpp/)
155-
- [在 Android 中通过 JNI 使用 FastDeploy C++ SDK](../../../../../docs/cn/faq/use_cpp_sdk_on_android.md)
147+
## 更多参考文档
148+
如果您想知道更多的FastDeploy Java API文档以及如何通过JNI来接入FastDeploy C++ API感兴趣,可以参考以下内容:
149+
- [在 Android 中使用 FastDeploy Java SDK](../../../../../java/android/)
150+
- [在 Android 中使用 FastDeploy C++ SDK](../../../../../docs/cn/faq/use_cpp_sdk_on_android.md)

examples/vision/classification/paddleclas/android/app/build.gradle

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,13 @@ android {
66
compileSdk 28
77

88
defaultConfig {
9-
applicationId "com.baidu.paddle.fastdeploy"
9+
applicationId 'com.baidu.paddle.fastdeploy.app.examples'
1010
minSdkVersion 15
1111
//noinspection ExpiredTargetSdkVersion
1212
targetSdkVersion 28
1313
versionCode 1
1414
versionName "1.0"
1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16-
17-
externalNativeBuild {
18-
cmake {
19-
arguments '-DANDROID_PLATFORM=android-21', '-DANDROID_STL=c++_shared', "-DANDROID_TOOLCHAIN=clang"
20-
abiFilters 'armeabi-v7a', 'arm64-v8a'
21-
cppFlags "-std=c++11"
22-
}
23-
}
2416
}
2517

2618
buildTypes {
@@ -30,17 +22,10 @@ android {
3022
}
3123
}
3224

33-
externalNativeBuild {
34-
cmake {
35-
path file('src/main/cpp/CMakeLists.txt')
36-
version '3.10.2'
37-
}
38-
}
39-
ndkVersion '20.1.5948944'
4025
}
4126

4227
dependencies {
43-
implementation fileTree(include: ['*.jar'], dir: 'libs')
28+
implementation fileTree(include: ['*.aar'], dir: 'libs')
4429
implementation 'com.android.support:appcompat-v7:28.0.0'
4530
//noinspection GradleDependency
4631
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
@@ -52,45 +37,81 @@ dependencies {
5237
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
5338
}
5439

55-
def archives = [
40+
def FD_MODEL = [
5641
[
57-
'src' : 'https://bj.bcebos.com/fastdeploy/release/android/fastdeploy-android-0.4.0-shared.tgz',
58-
'dest': 'libs'
59-
],
42+
'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/MobileNetV1_x0_25_infer.tgz',
43+
'dest': 'src/main/assets/models'
44+
]
45+
]
46+
47+
def FD_JAVA_SDK = [
6048
[
61-
'src': 'https://bj.bcebos.com/paddlehub/fastdeploy/MobileNetV1_x0_25_infer.tgz',
62-
'dest' : 'src/main/assets/models'
49+
'src' : 'https://bj.bcebos.com/fastdeploy/test/fastdeploy-android-sdk-latest-dev.aar',
50+
'dest': 'libs'
6351
]
6452
]
6553

66-
task downloadAndExtractArchives(type: DefaultTask) {
54+
task downloadAndExtractModels(type: DefaultTask) {
6755
doFirst {
68-
println "Downloading and extracting archives including libs and models"
56+
println "Downloading and extracting fastdeploy models ..."
6957
}
7058
doLast {
71-
// Prepare cache folder for archives
7259
String cachePath = "cache"
7360
if (!file("${cachePath}").exists()) {
7461
mkdir "${cachePath}"
7562
}
76-
archives.eachWithIndex { archive, index ->
63+
FD_MODEL.eachWithIndex { model, index ->
7764
MessageDigest messageDigest = MessageDigest.getInstance('MD5')
78-
messageDigest.update(archive.src.bytes)
79-
String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
80-
// Download the target archive if not exists
81-
boolean copyFiles = !file("${archive.dest}").exists()
82-
if (!file("${cachePath}/${cacheName}.tgz").exists()) {
83-
ant.get(src: archive.src, dest: file("${cachePath}/${cacheName}.tgz"))
84-
copyFiles = true // force to copy files from the latest archive files
65+
messageDigest.update(model.src.bytes)
66+
String[] modelPaths = model.src.split("/")
67+
String modelName = modelPaths[modelPaths.length - 1]
68+
// Download the target model if not exists
69+
boolean copyFiles = !file("${model.dest}").exists()
70+
if (!file("${cachePath}/${modelName}").exists()) {
71+
println "Downloading ${model.src} -> ${cachePath}/${modelName}"
72+
ant.get(src: model.src, dest: file("${cachePath}/${modelName}"))
73+
copyFiles = true
8574
}
86-
// Extract the target archive if its dest path does not exists
8775
if (copyFiles) {
76+
println "Coping ${cachePath}/${modelName} -> ${model.dest}"
8877
copy {
89-
from tarTree("${cachePath}/${cacheName}.tgz")
90-
into "${archive.dest}"
78+
from tarTree("${cachePath}/${modelName}")
79+
into "${model.dest}"
9180
}
9281
}
9382
}
9483
}
9584
}
96-
preBuild.dependsOn downloadAndExtractArchives
85+
86+
task downloadAndExtractSDKs(type: DefaultTask) {
87+
doFirst {
88+
println "Downloading and extracting fastdeploy android java sdk ..."
89+
}
90+
doLast {
91+
String cachePath = "cache"
92+
if (!file("${cachePath}").exists()) {
93+
mkdir "${cachePath}"
94+
}
95+
FD_JAVA_SDK.eachWithIndex { sdk, index ->
96+
String[] sdkPaths = sdk.src.split("/")
97+
String sdkName = sdkPaths[sdkPaths.length - 1]
98+
// Download the target SDK if not exists
99+
boolean copyFiles = !file("${sdk.dest}/${sdkName}").exists()
100+
if (!file("${cachePath}/${sdkName}").exists()) {
101+
println "Downloading ${sdk.src} -> ${cachePath}/${sdkName}"
102+
ant.get(src: sdk.src, dest: file("${cachePath}/${sdkName}"))
103+
copyFiles = true
104+
}
105+
if (copyFiles) {
106+
println "Coping ${cachePath}/${sdkName} -> ${sdk.dest}/${sdkName}"
107+
copy {
108+
from "${cachePath}/${sdkName}"
109+
into "${sdk.dest}"
110+
}
111+
}
112+
}
113+
}
114+
}
115+
116+
preBuild.dependsOn downloadAndExtractSDKs
117+
preBuild.dependsOn downloadAndExtractModels

examples/vision/classification/paddleclas/android/app/libs/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/vision/classification/paddleclas/android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.baidu.paddle.fastdeploy.examples">
3+
package="com.baidu.paddle.fastdeploy.app.examples">
44

55
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
66
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
@@ -15,14 +15,14 @@
1515
android:roundIcon="@mipmap/ic_launcher_round"
1616
android:supportsRtl="true"
1717
android:theme="@style/AppTheme">
18-
<activity android:name="com.baidu.paddle.fastdeploy.examples.MainActivity">
18+
<activity android:name=".classification.ClassificationMainActivity">
1919
<intent-filter>
2020
<action android:name="android.intent.action.MAIN"/>
2121
<category android:name="android.intent.category.LAUNCHER"/>
2222
</intent-filter>
2323
</activity>
2424
<activity
25-
android:name="com.baidu.paddle.fastdeploy.examples.SettingsActivity"
25+
android:name=".classification.ClassificationSettingsActivity"
2626
android:label="Settings">
2727
</activity>
2828
</application>

examples/vision/classification/paddleclas/android/app/src/main/assets/labels/coco_label_list.txt

Lines changed: 0 additions & 80 deletions
This file was deleted.

examples/vision/classification/paddleclas/android/app/src/main/assets/labels/imagenet1k_label_list.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,4 +997,4 @@
997997
996 hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa
998998
997 bolete
999999
998 ear, spike, capitulum
1000-
999 toilet tissue, toilet paper, bathroom tissue
1000+
999 toilet tissue, toilet paper, bathroom tissue

examples/vision/classification/paddleclas/android/app/src/main/assets/labels/pascalvoc_label_list

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)