From e32ad3394e051b5d38db92ac411432bde7edc452 Mon Sep 17 00:00:00 2001 From: cc <52520497+juncaipeng@users.noreply.github.com> Date: Fri, 8 May 2020 11:38:15 +0800 Subject: [PATCH] [Demo] v2.6 supports mask_demo (#3571) (#3574) * v2.6 supports mask_demo, test=develop --- lite/demo/cxx/README.md | 68 +++++-------------- .../demo/cxx/mask_detection/mask_detection.cc | 4 +- lite/demo/cxx/mask_detection/prepare.sh | 24 +++++++ lite/demo/cxx/mask_detection/run.sh | 12 ++++ 4 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 lite/demo/cxx/mask_detection/prepare.sh create mode 100644 lite/demo/cxx/mask_detection/run.sh diff --git a/lite/demo/cxx/README.md b/lite/demo/cxx/README.md index ff579a7f36..b36960b32a 100644 --- a/lite/demo/cxx/README.md +++ b/lite/demo/cxx/README.md @@ -12,38 +12,25 @@ **下载Demo并执行** -下载压缩包[mask_detection_files](https://paddle-inference-dist.cdn.bcebos.com/PaddleLiteDemo/mask_detection_files.tgz),解压到本地,其中包括编译好的可执行文件、模型文件、测试图片、PaddleLite 2.3版本动态库。 +下载压缩包[mask_demo](https://paddle-inference-dist.cdn.bcebos.com/PaddleLiteDemo/mask_demo_v2.6.tgz),解压到本地,其中包括编译好的可执行文件、模型文件、测试图片、PaddleLite 2.6版本动态库。 -电脑连接安卓手机,在电脑shell端执行如下命令,将mask_detection_files文件夹push到安卓手机上。 -``` -adb push mask_detection_files /data/local/tmp/ -``` +电脑连接安卓手机,在电脑shell端进入 `mask_demo` 目录。 -在电脑shell端执行如下命令,进入安卓手机,执行demo。 -``` -adb shell -cd /data/local/tmp/mask_detection_files -export LD_LIBRARY_PATH=/data/local/tmp/mask_detection_files:$LD_LIBRARY_PATH -./mask_detection face_detection mask_classification test.jpg -``` +执行 `sh run.sh`,会将文件push到手机端、执行口罩检测、pull结果图片。 -回到电脑端,将结果图片(test_mask_detection_result.jpg)取出,查看检测结果。 -``` -exit -adb pull /data/local/tmp/mask_detection_files/test_mask_detection_result.jpg ./ -``` +在电脑端查看 `test_img_result.jpg`,即是口罩检测结果。 **编译Demo并执行** -参考[源码编译](https://paddlepaddle.github.io/Paddle-Lite/v2.2.0/source_compile/)准备编译环境。 +参考[预测库编译](https://paddle-lite.readthedocs.io/zh/latest/user_guides/source_compile.html)准备编译环境。 -执行下面命令,下载PaddleLite代码,切换到2.3版本分支。 +执行下面命令,下载PaddleLite代码,切换到2.6版本分支。 ```shell git clone https://github.com/PaddlePaddle/Paddle-Lite.git cd Paddle-Lite -git fetch origin release/v2.3:release/v2.3 -git checkout release/v2.3 +git fetch origin release/v2.6:release/v2.6 +git checkout release/v2.6 ``` 进入PaddleLite根目录,编译预测库。 @@ -58,14 +45,20 @@ git checkout release/v2.3 full_publish ``` -进入编译目录,下载模型和图片的压缩包,编译可执行文件。 +编译完成后,进入Demo编译目录,执行脚本,会编译可执行文件,同时将可执行文件、预测库、模型、图片保存到 `mask_demo` 文件中。 ```shell cd build.lite.android.armv8.gcc/inference_lite_lib.android.armv8/demo/cxx/mask_detection -wget https://paddle-inference-dist.bj.bcebos.com/mask_detection.tar.gz -tar zxvf mask_detection.tar.gz -make +sh prepare.sh ``` +电脑连接安卓手机,在电脑shell端进入 `mask_demo` 目录。 + +执行 `sh run.sh`,会将文件push到手机端、执行口罩检测、pull结果图片。 + +在电脑端查看 `test_img_result.jpg`,即是口罩检测结果,如下图。 + +![test_mask_detection_result](https://user-images.githubusercontent.com/7383104/75131866-bae64300-570f-11ea-9cad-17acfaea1cfc.jpg) + 当然,大家也可以通过PaddleHub下载人脸检测模型和口罩佩戴判断模型。 ``` # 下载paddlehub以后,通过python执行以下代码 @@ -77,31 +70,6 @@ pyramidbox_lite_mobile_mask.processor.save_inference_model(dirname="test_program # 从PaddleHub下载的是预测模型,需要使用PaddleLite提供的model_optimize_tools对预测模型进行转换,请参考[模型转换文档](https://paddlepaddle.github.io/Paddle-Lite/v2.2.0/model_optimize_tool/)。 ``` -电脑连接安卓手机,将可执行文件、测试图片、模型文件、预测库push到安卓手机上。 -``` -adb push mask_detection /data/local/tmp/ -adb push test.jpg /data/local/tmp/ -adb push face_detection /data/local/tmp -adb push mask_classification /data/local/tmp -adb push ../../../cxx/lib/libpaddle_light_api_shared.so /data/local/tmp/ -adb shell chmod +x /data/local/tmp/mask_detection -``` - -进入安卓手机,执行demo。 -``` -adb shell -cd /data/local/tmp -export LD_LIBRARY_PATH=/data/local/tmp/:$LD_LIBRARY_PATH -./mask_detection face_detection mask_classification test.jpg -``` - -回到电脑端,将结果取出,查看如下效果图。 -``` -adb pull /data/local/tmp/test_mask_detection_result.jpg ./ -``` - -![test_mask_detection_result](https://user-images.githubusercontent.com/7383104/75131866-bae64300-570f-11ea-9cad-17acfaea1cfc.jpg) - 注:mask_detetion.cc 中的缩放因子shrink, 检测阈值detect_threshold, 可供自由配置: - 缩放因子越大,模型运行速度越慢,检测准确率越高。 - 检测阈值越高,人脸筛选越严格,检测出的人脸框可能越少。 diff --git a/lite/demo/cxx/mask_detection/mask_detection.cc b/lite/demo/cxx/mask_detection/mask_detection.cc index 09a9c0ee15..fe78f5d8d3 100644 --- a/lite/demo/cxx/mask_detection/mask_detection.cc +++ b/lite/demo/cxx/mask_detection/mask_detection.cc @@ -246,7 +246,7 @@ void RunModel(std::string det_model_file, text += prob_str.substr(0, point_idx + 3) + "%"; int font_face = cv::FONT_HERSHEY_SIMPLEX; - double font_scale = 0.25; + double font_scale = 0.38; float thickness = 1; cv::Size text_size = cv::getTextSize(text, font_face, font_scale, thickness, nullptr); @@ -285,7 +285,7 @@ void RunModel(std::string det_model_file, int start = img_path.find_last_of("/"); int end = img_path.find_last_of("."); std::string img_name = img_path.substr(start + 1, end - start - 1); - std::string result_name = img_name + "_mask_detection_result.jpg"; + std::string result_name = img_name + "_result.jpg"; cv::imwrite(result_name, img); std::cout << "write result to file: " << result_name << ", success." << std::endl; diff --git a/lite/demo/cxx/mask_detection/prepare.sh b/lite/demo/cxx/mask_detection/prepare.sh new file mode 100644 index 0000000000..12aad79fee --- /dev/null +++ b/lite/demo/cxx/mask_detection/prepare.sh @@ -0,0 +1,24 @@ +# make +make -j + +# mkdir +gf=mask_demo +if [ -d ${gf} ];then + rm -rf ${gf} +fi +mkdir ${gf} + +# collect files +cp run.sh ${gf} +cp mask_detection ${gf} +cp ../../../cxx/lib/libpaddle_light_api_shared.so ${gf} + +if [ ! -f "mask_models_img.tar.gz" ]; +then + wget -c https://paddle-inference-dist.cdn.bcebos.com/PaddleLiteDemo/mask_models_img.tar.gz +fi +tar zxvf mask_models_img.tar.gz +mv mask_models_img ${gf} + +# clean +make clean diff --git a/lite/demo/cxx/mask_detection/run.sh b/lite/demo/cxx/mask_detection/run.sh new file mode 100644 index 0000000000..536b63c328 --- /dev/null +++ b/lite/demo/cxx/mask_detection/run.sh @@ -0,0 +1,12 @@ +adb push ../mask_demo /data/local/tmp/ + +mask_demo_path="/data/local/tmp/mask_demo" + +adb shell "cd ${mask_demo_path} \ + && export LD_LIBRARY_PATH=${mask_demo_path}:${LD_LIBRARY_PATH} \ + && ./mask_detection \ + mask_models_img/pyramidbox_lite_opt2.nb \ + mask_models_img/mask_detector_opt2.nb \ + mask_models_img/test_img.jpg" + +adb pull ${mask_demo_path}/test_img_result.jpg . -- GitLab