模型部署:(四)安卓端部署Yolov8-v8.2.99實例分割項目全流程記錄
- 1、下載ncnn
- 2、下載opencv-mobile
- 3、文件拷貝
- 4、andorid_studio相關配置
- 5、文件內參數設置
- 5、重構項目:
- 6、打包apk
- 7、部署自己訓練的實例分割模型
1、下載ncnn
地址:
https://github.com/Tencent/ncnn/tree/20230223
下載的版本是
ncnn-20230223-android-vulkan
2、下載opencv-mobile
下載的版本是:opencv-mobile-4.6.0-android
官網地址:
https://github.com/nihui/opencv-mobile/releases?page=2
3、文件拷貝
將上述下載的ncnn-20230223-android-vulkan與opencv-mobile-4.6.0-android分別解壓,拷貝到
目錄D:\ncnn-android-yolov8-seg\app\src\main\jni下
4、andorid_studio相關配置
我的這個版本是:Android Studio Flamingo | 2022.2.1 Patch 1
配置SDK Platforms
配置SDK Platforms
5、文件內參數設置
文件:build.gradle (Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {repositories {jcenter()google()}dependencies {classpath 'com.android.tools.build:gradle:3.5.0'}
}allprojects {repositories {jcenter()google()}
}
文件:build.gradle (Module: app)
apply plugin: 'com.android.application'android {compileSdkVersion 24buildToolsVersion "29.0.2"defaultConfig {applicationId "com.tencent.yolov8ncnn"archivesBaseName = "$applicationId"minSdkVersion 24}externalNativeBuild {cmake {version "3.10.2"path file('src/main/jni/CMakeLists.txt')}}dependencies {implementation 'com.android.support:support-v4:24.0.0'}ndkVersion '24.0.8215888'
}
文件:gradle-wrapper.properties
#Sun Sep 08 23:09:42 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://mirrors.cloud.tencent.com/gradle/gradle-5.4.1-all.zip
文件:local.properties
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Fri Sep 12 17:47:07 CST 2025
cmake.dir=D\:\\Android_SDK\\Sdk\\cmake\\3.10.2.4988404
ndk.dir=D\:\\Android_SDK\\Sdk\\ndk\\24.0.8215888
sdk.dir=D\:\\Android_SDK\\Sdk
文件:settings.gradle
文件:CMakeLists.txt
5、重構項目:
6、打包apk
但是打包過程中會出現報錯,提示信息如下:
Execution failed for task ':app:stripDebugDebugSymbols'.
> No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
解決方案:
同時安裝低版本的ndk(如version=21.0.6113669),將低版
本ndk中toolchains 文件夾下的arm-linux-androideabi等文件復制到高版
本ndk的toolchains 文件夾中
復制后如下所示::復制到24.0.8215888對應的目錄下
再次重構,打包,效果如下所示:
7、部署自己訓練的實例分割模型
#測試 ## 模型推理
yolo segment predict model=yolov8s-seg.pt source=0a2cf558-d1bd-4ad8-9724-ce5760192bb9.jpg
模型轉換
yolo export model=yolov8s-seg.pt format=onnx simplify=True opset=12
# 使用導出的.onnx模型推理
## 默認使用gpu
yolo segment predict model=yolov8s-seg.onnx source=0a2cf558-d1bd-4ad8-9724-ce5760192bb9.jpg
## 默認使用cpu
yolo segment predict model=yolov8s-seg.onnx source=0a2cf558-d1bd-4ad8-9724-ce5760192bb9.jpg device=cpu
對.onnx模型進行去冗余操作:
安裝相關依賴包:
pip install onnx-simplifier
# 去冗余指令:
python -m onnxsim yolov8s-seg.onnx yolov8s-seg-sim.onnx