当前位置:   article > 正文

ncnn仅使用opencv实现人脸检测和人脸特征提取_ncnn人形检测流程

ncnn人形检测流程

人脸检测基于opencv-CascadeClassifier, 特征提取使用的是自己insightface的mxnet转化为ncnn的模型。

,cpp 文件:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <time.h>
  7. #include <opencv2/core/core.hpp>
  8. #include <opencv2/highgui/highgui.hpp>
  9. #include "opencv2/imgproc/imgproc.hpp"
  10. #include "opencv2/objdetect/objdetect.hpp"
  11. #include "net.h"
  12. using namespace std;
  13. //print tensor
  14. void pretty_print(const ncnn::Mat& m)
  15. {
  16. int count = 0;
  17. for (int q=0; q<m.c; q++)
  18. {
  19. const float* ptr = m.channel(q);
  20. for (int y=0; y<m.h; y++)
  21. {
  22. for (int x=0; x<m.w; x++)
  23. {
  24. printf("%f ", ptr[x]);
  25. count++;
  26. }
  27. ptr += m.w;
  28. printf("\n");
  29. }
  30. printf("------------------------\n");
  31. printf("%d\n",count);
  32. }
  33. }
  34. //main
  35. int main(int argc, char** argv){
  36. // 定义输入格式
  37. if (argc != 2) {
  38. fprintf(stderr, "input error\n");
  39. return -2;
  40. }
  41. //string path = argv[1];
  42. //定义级联分类器
  43. cv::CascadeClassifier face_detector;
  44. //加在分类器
  45. face_detector.load("haarcascade_frontalface_alt2.xml");
  46. //判断分类器是否加载成功
  47. if (face_detector.empty())
  48. {
  49. std::cerr << "load detector failed!!!" << std::endl;
  50. return -1;
  51. }
  52. cv::Size original_size = face_detector.getOriginalWindowSize();
  53. // 导入模型
  54. ncnn::Net net;
  55. printf("net init\n");
  56. //net.opt.num_threads=1;
  57. net.load_param("model-symbol.param");
  58. printf("load model param\n");
  59. net.load_model("model-symbol.bin");
  60. printf("load model bin\n");
  61. clock_t start, finish;
  62. string img_path = argv[1];
  63. cv::Mat img = cv::imread(img_path, cv::IMREAD_COLOR);
  64. cv::Mat image_gray;
  65. cv::cvtColor(img, image_gray, cv::COLOR_BGR2GRAY);
  66. //用于保存检测到的目标窗口
  67. std::vector<cv::Rect> dets;
  68. //进行多尺度人脸检测
  69. face_detector.detectMultiScale(image_gray, dets, 1.1, 3, 0 | cv::CASCADE_SCALE_IMAGE, original_size);
  70. // 获取所有人脸
  71. /**
  72. for (size_t i = 0; i < faces.size(); i++)
  73. {
  74. cv::rectangle(image, faces[i], cv::Scalar(0, 0, 255), 2, 8, 0);
  75. }
  76. **/
  77. // 获取最大人脸
  78. cv::Rect R;
  79. int Max = 0;
  80. int area = 0;
  81. for (size_t t = 0; t < dets.size(); ++t)
  82. {
  83. if (area < dets[t].width * dets[t].height)
  84. {
  85. area = dets[t].width * dets[t].height;
  86. Max = t;
  87. }
  88. }
  89. R.x = dets[Max].x;
  90. R.y = dets[Max].y;
  91. R.width = dets[Max].width;
  92. R.height = dets[Max].height;
  93. // max_face 最大人脸
  94. cv::Mat max_face = img(R);
  95. // 显示出来最大人脸
  96. // cv::imshow("detect result", max_face);
  97. // cv::waitKey(0);
  98. // resize图片为模型指定的输入
  99. cv::Mat img2;
  100. int input_width = 112;//转onnx时指定的输入大小
  101. int input_height = 112;
  102. // resize
  103. printf("---1111---\n");
  104. cv::resize(max_face, img2, cv::Size(input_width, input_height));
  105. printf("---cv ok---\n");
  106. // load model
  107. // 把opencv的mat转换成ncnn的mat
  108. ncnn::Mat input = ncnn::Mat::from_pixels(img2.data, ncnn::Mat::PIXEL_BGR, img2.cols, img2.rows);
  109. printf("convert ncnn\n");
  110. // ncnn froward
  111. // 模型前向传播
  112. printf("net forward\n");
  113. for(int i=0; i<1000; i++){
  114. start = clock();
  115. ncnn::Extractor extractor = net.create_extractor();
  116. extractor.input("data", input);
  117. ncnn::Mat output0;//取决于模型的输出有几个
  118. extractor.extract("fc1", output0);
  119. finish = clock();
  120. double duration = (double)(finish - start) / CLOCKS_PER_SEC;
  121. printf( "%.4f seconds\n", duration );
  122. //pretty_print(output0);
  123. }
  124. printf("get output0\n");
  125. // 打印 tensor
  126. // pretty_print(output1);
  127. cout<<"done"<<endl;
  128. return 0;
  129. }

 

CMakeLists.txt 配置(将dlib相关的删除,没有依赖dlib)

  1. macro(ncnn_add_example name)
  2. add_executable(${name} ${name}.cpp)
  3. # add_library(${name} SHARED ${name}.cpp)
  4. target_include_directories(${name} PRIVATE ${OpenCV_INCLUDE_DIRS} ${dlib_INCLUDE_DIRS} )
  5. target_link_libraries(${name} PRIVATE ncnn ${OpenCV_LIBS} ${dlib_LIBRARIES})
  6. # target_include_directories(${name} PRIVATE ${dlib_INCLUDE_DIRS})
  7. # target_link_libraries(${name} PRIVATE ncnn ${dlib_LIBRARIES} )
  8. # add test to a virtual project group
  9. # set_target_properties(dlib PROPERTIES POSITION_INDEPENDENT_CODE ON)
  10. set_property(TARGET ${name} PROPERTY FOLDER "examples")
  11. endmacro()
  12. # find_package(dlib REQUIRED)
  13. # include_directories(${dlib_INCLUDE_DIRS})
  14. # add_executable(${PROJECT_NAME} ${SRC_LIST})
  15. # target_link_libraries(${PROJECT_NAME} ${dlib_LIBRARIES})
  16. # add_subdirectory(/home/zb/zhangr/opencv-2.4.9 ${PROJECT_NAME}/opencv_build)
  17. add_subdirectory(/home/zb/zhangr/dlib dlib_build)
  18. #add_executable(dlib1 dlib1.cpp)
  19. # Finally, you need to tell CMake that this program, assignment_learning_ex,
  20. # depends on dlib. You do that with this statement:
  21. #target_link_libraries(dlib1 dlib::dlib)
  22. #set_property(TARGET dlib1 PROPERTY FOLDER "examples")
  23. #add_executable(dlib2 dlib2.cpp)
  24. #target_link_libraries(dlib2 dlib::dlib)
  25. #set_property(TARGET dlib2 PROPERTY FOLDER "examples")
  26. find_package(OpenCV QUIET COMPONENTS opencv_world objdetect)
  27. find_package(dlib REQUIRED)
  28. #add_executable(main2 main2.cpp)
  29. #target_include_directories(main2 PRIVATE ${OpenCV_INCLUDE_DIRS})
  30. #target_link_libraries(main2 ncnn ${OpenCV_LIBS} dlib::dlib)
  31. #set_property(TARGET main2 PROPERTY FOLDER "examples")
  32. # for opencv 2.4 on ubuntu 16.04, there is no opencv_world but OpenCV_FOUND will be TRUE
  33. if("${OpenCV_LIBS}" STREQUAL "")
  34. set(OpenCV_FOUND FALSE)
  35. endif()
  36. if(NOT OpenCV_FOUND)
  37. find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs videoio)
  38. endif()
  39. if(NOT OpenCV_FOUND)
  40. find_package(OpenCV QUIET COMPONENTS core highgui imgproc)
  41. endif()
  42. if(OpenCV_FOUND)
  43. message(STATUS "OpenCV library: ${OpenCV_INSTALL_PATH}")
  44. message(STATUS " version: ${OpenCV_VERSION}")
  45. message(STATUS " libraries: ${OpenCV_LIBS}")
  46. message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
  47. if(${OpenCV_VERSION_MAJOR} GREATER 3)
  48. set(CMAKE_CXX_STANDARD 11)
  49. endif()
  50. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src)
  51. include_directories(${CMAKE_CURRENT_BINARY_DIR}/../src)
  52. ncnn_add_example(squeezenet)
  53. ncnn_add_example(squeezenet_c_api)
  54. ncnn_add_example(fasterrcnn)
  55. ncnn_add_example(rfcn)
  56. ncnn_add_example(yolov2)
  57. ncnn_add_example(yolov3)
  58. ncnn_add_example(yolov4)
  59. ncnn_add_example(mobilenetv2ssdlite)
  60. ncnn_add_example(mobilenetssd)
  61. ncnn_add_example(squeezenetssd)
  62. ncnn_add_example(shufflenetv2)
  63. ncnn_add_example(peleenetssd_seg)
  64. ncnn_add_example(simplepose)
  65. ncnn_add_example(retinaface)
  66. ncnn_add_example(yolact)
  67. ncnn_add_example(main)
  68. ncnn_add_example(main1)
  69. ncnn_add_example(main2)
  70. ncnn_add_example(main3)
  71. ncnn_add_example(Harr)
  72. ncnn_add_example(only_opencv)
  73. else()
  74. message(WARNING "OpenCV not found, examples won't be built")
  75. endif()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/703294
推荐阅读
相关标签
  

闽ICP备14008679号