本博客将实现C++版本的人脸检测,人脸关键点检测,人体检测,人脸+人体检测,推理框架采用TNN,在普通Android手机,CPU和GPU都可以达到实时检测的效果
人脸检测+人脸关键点检测+人体检测Android Demo APP(非源码,仅供学习交流)
链接: https://pan.baidu.com/s/1By43I1DbMa0gBPLObtPZMQ 提取码: msnr
尊重原创,转载请注明出处:https://panjinquan.blog.csdn.net/article/details/120688804
训练代码请参考:https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB ,一个基于SSD简化的人脸检测模型,很轻量化,整个模型仅仅1.7M左右,在普通Android手机都可以实时检测。
原始代码使用WiderFace人脸数据集进行训练,仅支持了人脸检测,后经鄙人优化后,提高了人脸检测效果,并支持人脸关键点检测,人体检测。数据集是WiderFace,VOC和COCO。
原始代码已经支持MNN和NCNN
# pull 3rdparty(TNN,base-utils) submodule
git submodule init
git submodule update
推荐opencv-4.3.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make install
Android系统一般都支持OpenCL,Linux系统可参考如下配置:
# 参考安装OpenCL: https://blog.csdn.net/qq_28483731/article/details/68235383,作为测试,安装`intel cpu版本的OpenCL`即可
# 安装clinfo,clinfo是一个显示OpenCL平台和设备的软件
sudo apt-get install clinfo
# 安装依赖
sudo apt install dkms xz-utils openssl libnuma1 libpciaccess0 bc curl libssl-dev lsb-core libicu-dev
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
sudo apt-get install mono-complete
# 在intel官网上下载了intel SDK的tgz文件,并且解压
sudo sh install.sh
Linux OR Windows测试,
CMakeLists.txt
# TNN set
set(TNN_OPENCL_ENABLE ON CACHE BOOL "" FORCE)
set(TNN_CPU_ENABLE ON CACHE BOOL "" FORCE)
set(TNN_X86_ENABLE ON CACHE BOOL "" FORCE)
set(TNN_QUANTIZATION_ENABLE OFF CACHE BOOL "" FORCE)
set(TNN_OPENMP_ENABLE ON CACHE BOOL "" FORCE) # Multi-Thread
add_definitions(-DTNN_OPENCL_ENABLE) # for OpenCL GPU
add_definitions(-DDEBUG_ON) # for WIN/Linux Log
add_definitions(-DDEBUG_LOG_ON) # for WIN/Linux Log
add_definitions(-DDEBUG_IMSHOW_OFF) # for OpenCV show
add_definitions(-DPLATFORM_LINUX)
# add_definitions(-DPLATFORM_WINDOWS)
推理框架使用TNN进行部署,手撸Python转C++实现人脸人体检测推理过程,
下面是测试代码demo部分代码
void test_face_person_detector() {
const int num_thread = 1;
DeviceType device = CPU;
// 人脸和关键点检测
// const char *model_file = (char *) "../data/tnn/face_ldmks/rfb_landm_face_320_320_sim.opt.tnnmodel";
// const char *proto_file = (char *) "../data/tnn/face_ldmks/rfb_landm_face_320_320_sim.opt.tnnproto";
// ObjectDetectiobParam model_param = FACE_LANDMARK_MODEL;
// 人脸+人体检测
// const char *model_file = (char *) "../data/tnn/face_person/rfb1.0_face_person_300_300_sim.opt.tnnmodel";
// const char *proto_file = (char *) "../data/tnn/face_person/rfb1.0_face_person_300_300_sim.opt.tnnproto";
// ObjectDetectiobParam model_param = FACE_PERSON_MODEL;//模型参数
// 人脸检测
const char *model_file = (char *) "../data/tnn/face/rfb1.0_face_320_320.opt.tnnmodel";
const char *proto_file = (char *) "../data/tnn/face/rfb1.0_face_320_320.opt.tnnproto";
ObjectDetectiobParam model_param = FACE_MODEL;//模型参数
// 设置检测阈值
const float scoreThresh = 0.5;
const float iouThresh = 0.3;
ObjectDetection *detector = new ObjectDetection(model_file,
proto_file,
model_param,
num_thread,
device);
string image_dir = "../data/test_image/person";
std::vector image_list = get_files_list(image_dir);
for (string image_path:image_list) {
cv::Mat bgr_image = cv::imread(image_path);
if (bgr_image.empty()) continue;
FrameInfo resultInfo;
printf("init frame\n");
// 开始检测
detector->detect(bgr_image, &resultInfo, scoreThresh, iouThresh);
// 可视化代码
detector->visualizeResult(bgr_image, &resultInfo);
}
delete detector;
detector = nullptr;
printf("FINISHED.\n");
}
可以轻松移植到Android系统,在普通手机,CPU和GPU都可以达到实时检测
人脸检测+人脸关键点检测+人体检测Android Demo APP(非源码,仅供学习交流):
链接: https://pan.baidu.com/s/1By43I1DbMa0gBPLObtPZMQ 提取码: msnr
这是APP的检测效果:
APP | 模型选择 | 人脸检测 |
人脸关键点检测 | 人体检测 | 人脸+人体检测 |
人体关键点检测需要用到人体检测,请查看鄙人另一篇博客:2D Pose人体关键点实时检测(Python/Android /C++ Demo)_pan_jinquan的博客-CSDN博客
如果你觉得该帖子帮到你,还望贵人多多支持,鄙人会再接再厉,继续努力的~