Skip to content

Commit cd5cc98

Browse files
authored
[Doc] Update paddledetection/android app example (PaddlePaddle#663)
* [Bug Fix] fix android app detail page errors * [Android] fix realtime camera mode and shutter * [Bug Fix] fix AllocateSegmentationResultFromJava error * [Bug Fix] fix camera preview size setting problem * [Model] use uint8 buffer instead of fp32 in ppseg postprocess * [Model] revert changes in ppseg * [Model] revert postprocess changes in ppseg * [Android] add fastdeploy android sdk download task * [Bug Fix] fix yolov5face scale error in small image * [Doc] Update Android SDK docs * [Doc] Update Android SDK usage docs * [Android] Update paddledetection/android app example
1 parent 08a2a28 commit cd5cc98

Some content is hidden

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

49 files changed

+8839
-2199
lines changed

examples/vision/detection/paddledetection/android/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ app/build
1111
app/src/main/assets/models/*
1212
app/.gradle
1313
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

examples/vision/detection/paddledetection/android/README.md

+29-34
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,27 @@
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. 目标检测 PicoDet Demo 位于 `fastdeploy/examples/vision/detection/paddledetection/android` 目录
1513
2. 用 Android Studio 打开 paddledetection/android 工程
1614
3. 手机连接电脑,打开 USB 调试和文件传输模式,并在 Android Studio 上连接自己的手机设备(手机需要开启允许从 USB 安装软件权限)
1715

1816
<p align="center">
19-
<img width="1280" alt="image" src="https://user-images.githubusercontent.com/31974251/197168120-7f77fbb7-3850-44f0-b6fa-865a98951226.png">
17+
<img width="1440" alt="image" src="https://user-images.githubusercontent.com/31974251/203257262-71b908ab-bb2b-47d3-9efb-67631687b774.png">
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 配置方法,你可以在 `paddledetection/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
28-
4. 点击 Run 按钮,自动编译 APP 并安装到手机。(该过程会自动下载预编译的 FastDeploy Android 库,需要联网)
23+
4. 点击 Run 按钮,自动编译 APP 并安装到手机。(该过程会自动下载预编译的 FastDeploy Android 库 以及 模型文件,需要联网)
2924
成功后效果如下,图一:APP 安装到手机;图二: APP 打开后的效果,会自动识别图片中的物体并标记;图三:APP设置选项,点击右上角的设置图片,可以设置不同选项进行体验。
3025

31-
| APP 图标 | APP 效果 | APP设置项
26+
| 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/197169609-bb214af3-d6e7-4433-bb96-1225cddd441c.jpg) | ![app_setup](https://user-images.githubusercontent.com/31974251/197332983-afbfa6d5-4a3b-4c54-a528-4a3e58441be1.jpg) |
28+
| ![app_pic](https://user-images.githubusercontent.com/31974251/203268599-c94018d8-3683-490a-a5c7-a8136a4fa284.jpg) | ![app_res](https://user-images.githubusercontent.com/31974251/197169609-bb214af3-d6e7-4433-bb96-1225cddd441c.jpg) | ![app_setup](https://user-images.githubusercontent.com/31974251/197332983-afbfa6d5-4a3b-4c54-a528-4a3e58441be1.jpg) |
3429

35-
## PicoDet Java API 说明
30+
### PicoDet Java API 说明
3631
- 模型初始化 API: 模型初始化API包含两种方式,方式一是通过构造函数直接初始化;方式二是,通过调用init函数,在合适的程序节点进行初始化。PicoDet初始化参数说明如下:
3732
- modelFile: String, paddle格式的模型文件路径,如 model.pdmodel
3833
- paramFile: String, paddle格式的参数文件路径,如 model.pdiparams
@@ -56,30 +51,34 @@ public boolean init(String modelFile, String paramsFile, String configFile, Stri
5651
// 直接预测:不保存图片以及不渲染结果到Bitmap上
5752
public DetectionResult predict(Bitmap ARGB8888Bitmap);
5853
// 预测并且可视化:预测结果以及可视化,并将可视化后的图片保存到指定的途径,以及将可视化结果渲染在Bitmap上
59-
public DetectionResult predict(Bitmap ARGB8888Bitmap, String savedImagePath, float scoreThreshold)
54+
public DetectionResult predict(Bitmap ARGB8888Bitmap, String savedImagePath, float scoreThreshold);
55+
public DetectionResult predict(Bitmap ARGB8888Bitmap, boolean rendering, float scoreThreshold); // 只渲染 不保存图片
6056
```
6157
- 模型资源释放 API:调用 release() API 可以释放模型资源,返回true表示释放成功,false表示失败;调用 initialized() 可以判断模型是否初始化成功,true表示初始化成功,false表示失败。
6258
```java
6359
public boolean release(); // 释放native资源
6460
public boolean initialized(); // 检查是否初始化成功
65-
```
61+
```
62+
6663
- RuntimeOption设置说明
6764
```java
6865
public void enableLiteFp16(); // 开启fp16精度推理
6966
public void disableLiteFP16(); // 关闭fp16精度推理
7067
public void setCpuThreadNum(int threadNum); // 设置线程数
7168
public void setLitePowerMode(LitePowerMode mode); // 设置能耗模式
7269
public void setLitePowerMode(String modeStr); // 通过字符串形式设置能耗模式
73-
public void enableRecordTimeOfRuntime(); // 是否打印模型运行耗时
7470
```
7571

7672
- 模型结果DetectionResult说明
7773
```java
78-
public float[][] mBoxes; // [n,4] 检测框 (x1,y1,x2,y2)
79-
public float[] mScores; // [n] 得分
80-
public int[] mLabelIds; // [n] 分类ID
81-
public boolean initialized(); // 检测结果是否有效
74+
public class DetectionResult {
75+
public float[][] mBoxes; // [n,4] 检测框 (x1,y1,x2,y2)
76+
public float[] mScores; // [n] 每个检测框得分(置信度,概率值)
77+
public int[] mLabelIds; // [n] 分类ID
78+
public boolean initialized(); // 检测结果是否有效
79+
}
8280
```
81+
其他参考:C++/Python对应的DetectionResult说明: [api/vision_results/detection_result.md](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/api/vision_results/detection_result.md)
8382

8483
- 模型调用示例1:使用构造函数以及默认的RuntimeOption
8584
```java
@@ -125,32 +124,28 @@ String configFile = "picodet_s_320_coco_lcnet/infer_cfg.yml";
125124
RuntimeOption option = new RuntimeOption();
126125
option.setCpuThreadNum(2);
127126
option.setLitePowerMode(LitePowerMode.LITE_POWER_HIGH);
128-
option.enableRecordTimeOfRuntime();
129127
option.enableLiteFp16();
130128
// 使用init函数初始化
131129
model.init(modelFile, paramFile, configFile, option);
132130
// Bitmap读取、模型预测、资源释放 同上 ...
133131
```
134-
更详细的用法请参考 [MainActivity](./app/src/main/java/com/baidu/paddle/fastdeploy/examples/MainActivity.java#L207) 中的用法
135-
136-
## 替换 FastDeploy 预测库和模型
137-
替换FastDeploy预测库和模型的步骤非常简单。预测库所在的位置为 `app/libs/fastdeploy-android-xxx-shared`,其中 `xxx` 表示当前您使用的预测库版本号。模型所在的位置为,`app/src/main/assets/models/picodet_s_320_coco_lcnet`
138-
- 替换FastDeploy预测库的步骤:
139-
- 下载或编译最新的FastDeploy Android预测库,解压缩后放在 `app/libs` 目录下;
140-
- 修改 `app/src/main/cpp/CMakeLists.txt` 中的预测库路径,指向您下载或编译的预测库路径。如:
141-
```cmake
142-
set(FastDeploy_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/fastdeploy-android-xxx-shared")
143-
```
132+
更详细的用法请参考 [DetectionMainActivity](./app/src/main/java/com/baidu/paddle/fastdeploy/app/examples/detection/DetectionMainActivity.java) 中的用法
133+
134+
## 替换 FastDeploy SDK和模型
135+
替换FastDeploy预测库和模型的步骤非常简单。预测库所在的位置为 `app/libs/fastdeploy-android-sdk-xxx.aar`,其中 `xxx` 表示当前您使用的预测库版本号。模型所在的位置为,`app/src/main/assets/models/picodet_s_320_coco_lcnet`
136+
- 替换FastDeploy Android SDK: 下载或编译最新的FastDeploy Android SDK,解压缩后放在 `app/libs` 目录下;详细配置文档可参考:
137+
- [在 Android 中使用 FastDeploy Java SDK](../../../../../java/android/)
138+
144139
- 替换PicoDet模型的步骤:
145140
- 将您的PicoDet模型放在 `app/src/main/assets/models` 目录下;
146141
- 修改 `app/src/main/res/values/strings.xml` 中模型路径的默认值,如:
147142
```xml
148143
<!-- 将这个路径指修改成您的模型,如 models/picodet_l_320_coco_lcnet -->
149-
<string name="MODEL_DIR_DEFAULT">models/picodet_s_320_coco_lcnet</string>
150-
<string name="LABEL_PATH_DEFAULT">labels/coco_label_list.txt</string>
144+
<string name="DETECTION_MODEL_DIR_DEFAULT">models/picodet_s_320_coco_lcnet</string>
145+
<string name="DETECTION_LABEL_PATH_DEFAULT">labels/coco_label_list.txt</string>
151146
```
152147

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

examples/vision/detection/paddledetection/android/app/build.gradle

+59-42
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,49 +37,81 @@ dependencies {
5237
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
5338
}
5439

55-
def archives = [
56-
[
57-
'src' : 'https://bj.bcebos.com/fastdeploy/release/android/fastdeploy-android-0.4.0-shared.tgz',
58-
'dest': 'libs'
59-
],
40+
def FD_MODEL = [
6041
[
61-
'src': 'https://bj.bcebos.com/paddlehub/fastdeploy/picodet_s_320_coco_lcnet.tgz',
62-
'dest' : 'src/main/assets/models'
63-
],
42+
'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/picodet_s_320_coco_lcnet.tgz',
43+
'dest': 'src/main/assets/models'
44+
]
45+
]
46+
47+
def FD_JAVA_SDK = [
6448
[
65-
'src': 'https://bj.bcebos.com/paddlehub/fastdeploy/picodet_l_320_coco_lcnet.tgz',
66-
'dest' : 'src/main/assets/models'
49+
'src' : 'https://bj.bcebos.com/fastdeploy/test/fastdeploy-android-sdk-latest-dev.aar',
50+
'dest': 'libs'
6751
]
6852
]
6953

70-
task downloadAndExtractArchives(type: DefaultTask) {
54+
task downloadAndExtractModels(type: DefaultTask) {
7155
doFirst {
72-
println "Downloading and extracting archives including libs and models"
56+
println "Downloading and extracting fastdeploy models ..."
7357
}
7458
doLast {
75-
// Prepare cache folder for archives
7659
String cachePath = "cache"
7760
if (!file("${cachePath}").exists()) {
7861
mkdir "${cachePath}"
7962
}
80-
archives.eachWithIndex { archive, index ->
63+
FD_MODEL.eachWithIndex { model, index ->
8164
MessageDigest messageDigest = MessageDigest.getInstance('MD5')
82-
messageDigest.update(archive.src.bytes)
83-
String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
84-
// Download the target archive if not exists
85-
boolean copyFiles = !file("${archive.dest}").exists()
86-
if (!file("${cachePath}/${cacheName}.tgz").exists()) {
87-
ant.get(src: archive.src, dest: file("${cachePath}/${cacheName}.tgz"))
88-
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
74+
}
75+
if (copyFiles) {
76+
println "Coping ${cachePath}/${modelName} -> ${model.dest}"
77+
copy {
78+
from tarTree("${cachePath}/${modelName}")
79+
into "${model.dest}"
80+
}
81+
}
82+
}
83+
}
84+
}
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
89104
}
90-
// Extract the target archive if its dest path does not exists
91105
if (copyFiles) {
106+
println "Coping ${cachePath}/${sdkName} -> ${sdk.dest}/${sdkName}"
92107
copy {
93-
from tarTree("${cachePath}/${cacheName}.tgz")
94-
into "${archive.dest}"
108+
from "${cachePath}/${sdkName}"
109+
into "${sdk.dest}"
95110
}
96111
}
97112
}
98113
}
99114
}
100-
preBuild.dependsOn downloadAndExtractArchives
115+
116+
preBuild.dependsOn downloadAndExtractSDKs
117+
preBuild.dependsOn downloadAndExtractModels

examples/vision/detection/paddledetection/android/app/libs/.gitignore

-1
This file was deleted.

examples/vision/detection/paddledetection/android/app/src/main/AndroidManifest.xml

+3-3
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=".detection.DetectionMainActivity">
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=".detection.DetectionSettingsActivity"
2626
android:label="Settings">
2727
</activity>
2828
</application>

0 commit comments

Comments
 (0)