将yolov8部署到安卓——开发

QT安卓环境搭建

基本流程

起初打算选择5.14版本的QT进行开发,但是实际安装后发现问题多多,主要是安卓的SDK和NDK环境难搞,因为谷歌似乎不再提供单独的SDK供开发者下载,而是集成到了Android Studio中。索性就选择QT6(6.5.3)好了,关于QT安装的教程,网上到处都是,这里就不赘述了(主要是记不清了,而且懒…),记得安装时选择套件时要勾选上安卓。这里重点讲一下,安装好QT后,配置安卓的环境。这里建议先连接上手机(如果打算用虚拟环境而不是真机调试,那就当我没说),本人设备参数如下:

打开QT后先进入到左上角的“工具”,

选择外部->配置

进入设备->Android,先配置JDK,这个还算容易,网上也很多,注意版本就行,这里就不说了。然后找一个空文件夹,作为Android SDK的路径,选择设置SDK,然后根据提示一路选择,NDK和下面的OpenSSL设置也就设置好了(比起QT5真是方便了太多),记得点应用和确定。

image-20240424114720748

然后再重启一下QT Creator,记得确保手机是连上的。接着创建一个项目

在构建套件里应该是有安卓的选项的,根据你的手机架构选择。如果没有,那就确认手机是否连接,SDK和NDK有没有问题,重启,删除SDK安装目录下的东西重来一次,以上方法随便瞎试吧,毕竟配环境这事主打就是一个玄学。

创建完成后在左下角可以查看配置信息

检查一下CMake Tool,如果你之前配置过CMake的话,这里可能会变成你自己配置的CMake,如果构建出问题的话,建议换成QT自己的。记得应用和确定,不放心可以重启试试。

然后点开ui文件,放几个按钮试试

接着就可以点左下角调试了,第一次构建的话,会下载gradle,可能会卡住,我是切换了几次节点然后就下下来了,有可能自己去gradle官网下载,然后解压到QT的目录里,我是没有试啦,具体也不清楚。然后看手机,如果询问是否安装,就点是。最后手机上效果就是这样

PS:如果你构建的时候报了两个错误,并且是乱码的话,可以在这个地方选UTF-8

不过这个错误似乎问题不大,目前是这样的

其实上面的流程是第二遍的过程,我第一遍进行到构建时没有问题,手机也能正常显示调试画面,但是之后创建的所有项目都会在调试时闪退,我的解决办法是把手机系统从安卓12升级到安卓13,然后在开发者模式里撤销所有的USB调试授权,然后清空SDK目录,再重启,连接手机,按照上面的步骤重新安装SDK。然后目前来看解决了,至于为什么这样能搞定,那纯属瞎猫撞死耗子,只能感叹配环境实在是玄学。

QT安卓OpenCv&Ncnn环境配置

首先添加这几个文件

然后修改.pro文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#自带的
QT += core gui multimedia multimediawidgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
widget.cpp \
yolo.cpp

HEADERS += \
widget.h \
yolo.h

FORMS += \
widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#自带的

#ncnn需要加
QMAKE_CXXFLAGS += -fopenmp
QMAKE_LFLAGS += -fopenmp
LIBS += -fopenmp -static-openmp
#ncnn需要加

#上面添加完libopencv_java4.so文件自动生成的
contains(ANDROID_TARGET_ARCH,arm64-v8a) {
ANDROID_EXTRA_LIBS = \
$$PWD/../../../Dev/OpenCV/opencv-android/OpenCV-android-sdk/sdk/native/libs/arm64-v8a/libopencv_java4.so
}
#上面添加完libopencv_java4.so文件自动生成的

#自己加的
android {
ANDROID_OPENCV = D:/Dev/OpenCV/openCV-android/OpenCV-android-sdk/sdk/native

INCLUDEPATH += \
$$ANDROID_OPENCV/jni/include/opencv2 \
$$ANDROID_OPENCV/jni/include \
D:/Dev/ncnn-20240410-android/arm64-v8a/include \
D:/Dev/ncnn-20240410-android/arm64-v8a/include/ncnn


LIBS += \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_calib3d.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_core.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_dnn.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_features2d.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_flann.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_gapi.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_highgui.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_imgcodecs.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_imgproc.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_ml.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_objdetect.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_photo.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_stitching.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_video.a \
$$ANDROID_OPENCV/staticlibs/arm64-v8a/libopencv_videoio.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libade.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libcpufeatures.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libIlmImf.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libittnotify.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/liblibjpeg-turbo.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/liblibopenjp2.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/liblibpng.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/liblibprotobuf.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/liblibtiff.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/liblibwebp.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libquirc.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libtbb.a \
$$ANDROID_OPENCV/3rdparty/libs/arm64-v8a/libtegra_hal.a \
$$ANDROID_OPENCV/libs/arm64-v8a/libopencv_java4.so \
D:/Dev/ncnn-20240410-android/arm64-v8a/lib/libncnn.a \
D:/Dev/QT-Android/Android-SDK/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/33/libandroid.so \
D:/Dev/QT-Android/Android-SDK/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/14.0.6/lib/linux/aarch64/libomp.a \
D:/Dev/QT-Android/Android-SDK/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/14.0.6/lib/linux/aarch64/libomp.so

#权重的路径
data.files += yolov8n-op12.bin
data.files += yolov8n-op12.param
data.path = /assets/models
INSTALLS += data
#权重的路径

}
#自己加的

#加完以后 上面自动添加libandroid.so和libomp.so文件
ANDROID_EXTRA_LIBS += D:/Dev/QT-Android/Android-SDK/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/33/libandroid.so \
D:/Dev/QT-Android/Android-SDK/ndk/25.1.8937393/toolchains/llvm/prebuilt/windows-x86_64/lib64/clang/14.0.6/lib/linux/aarch64/libomp.so
#加完以后 上面自动添加libandroid.so和libomp.so文件