https://github.com/watersink/mtcnn-linux-as
本代码可以在模拟器下进行跑。
windows10 Android studio 3.6
Sdk: android10 api 29日
Ndk: r15c
Ncnn: 20200226
cd mtcnn_linux/构建 cmake . . 使 。/mtcnn
如果可以跑通,输出正确结果,证明mtcnn代码的准确性。
实际操作的时候,首先基于linux把c++代码调试通,方便后续的android调试。
Android进行c++调试时,使用<代码> __android_log_print 代码>函数进行日志的输出,
(1)工程建立
新建android工程,选择本机c++,工程名为mtcnn, c++标准选择c++ 11
(2)资源文件res修改:
src/main/res/可拉的下面随便复制一张带有人脸的照片,比如这里,复制了一张beauty.png
src/main/res/布局下面新加main。xml。
详细内容,
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android:取向=按怪薄? android: layout_width="宽和" android: layout_height=翱砗汀北? LinearLayout & lt; 面向android:="水平" android: layout_width="宽和" android: layout_height=皐rap_content”比; & lt;按钮 android: id=癅 + id/buttonImage” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本="选图”/比; & lt;按钮 android: id=癅 + id/buttonDetect” android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本="检测”/比; & lt;/LinearLayout> & lt; TextView android: id=癅 + id/infoResult” android: layout_width="宽和" android: layout_height=" wrap_content " android:文本="/比; & lt; ImageView android: id=癅 + id/imageView” android: layout_width="宽和" android: layout_height="宽和" android: layout_weight=" 1 "/比; & lt;/LinearLayout>
(3)增加ncnn的自由文件
src/主要下面新加jniLibs文件夹,加入对应平台的libncnn.a
(4)增加网络模型文件资产
在主要下面新建资产文件夹,里面放入mtcnn的3个网络结构的模型文件。
(5)修改java文件,
修改src/main/java/com/example/mtcnn下面的MainActivity,
主要操作,包括在onCreate函数中对mtcnn这个类进行初始化。然后监听buttonImage, buttonDetect按钮,分别进行实现。
然后在该路径下增加MTCNN类,主要需要实现的方法如下,
包com.example.mtcnn; 公开课MTCNN {//人脸检测模型导入 公共本机布尔FaceDetectionModelInit (byte [] det1_param, byte [] det1_bin, byte [] det2_param, byte [] det2_bin, byte [] det3_param, byte [] det3_bin);//人脸检测 公共int [] FaceDetect (byte [] imageDate, int imageWidth, int imageHeight, int imageChannel); 公共int [] MaxFaceDetect (byte [] imageDate, int imageWidth, int imageHeight, int imageChannel);//人脸检测模型反初始化 公共原生布尔FaceDetectionModelUnInit ();//检测的最小人脸设置 公共原生布尔SetMinFaceSize (int minSize);//线程设置 公共原生布尔SetThreadsNumber (int threadsNumber);//循环测试次数 公共原生布尔SetTimeCount (int timeCount); 静态{ System.loadLibrary (“mtcnn”); } }
(6)修改cpp文件,
首先将ncnn的包括文件夹包含进来。
将模型的3个id.h文件包含进来,det1.id.h, det2.id.h, det3.id.h
mtcnn_jni.cpp负责对人脸检测的几个本地方法进行实现。