本文实例为大家分享了OpenCV实现人脸检测功能的具体代码,供大家参考,具体内容如下

1、HAAR级联检测

  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. using namespace cv;
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7. int main(int artc, char** argv) {
  8. face_detect_haar();
  9. waitKey(0);
  10. return 0;
  11. }
  12. void face_detect_haar() {
  13. CascadeClassifier faceDetector;
  14. std::string haar_data_file = "./models/haarcascades/haarcascade_frontalface_alt_tree.xml";
  15. faceDetector.load(haar_data_file);
  16. vector<Rect> faces;
  17. //VideoCapture capture(0);
  18. VideoCapture capture("./video/test.mp4");
  19. Mat frame, gray;
  20. int count=0;
  21. while (capture.read(frame)) {
  22. int64 start = getTickCount();
  23. if (frame.empty())
  24. {
  25. break;
  26. }
  27. // 水平镜像调整
  28. // flip(frame, frame, 1);
  29. imshow("input", frame);
  30. if (frame.channels() == 4)
  31. cvtColor(frame, frame, COLOR_BGRA2BGR);
  32. cvtColor(frame, gray, COLOR_BGR2GRAY);
  33. equalizeHist(gray, gray);
  34. faceDetector.detectMultiScale(gray, faces, 1.2, 1, 0, Size(30, 30), Size(400, 400));
  35. for (size_t t = 0; t < faces.size(); t++) {
  36. count++;
  37. rectangle(frame, faces[t], Scalar(0, 255, 0), 2, 8, 0);
  38. }
  39. float fps = getTickFrequency() / (getTickCount() - start);
  40. ostringstream ss;ss.str("");
  41. ss << "FPS: " << fps << " ; inference time: " << time << " ms";
  42. putText(frame, ss.str(), Point(20, 20), 0, 0.75, Scalar(0, 0, 255), 2, 8);
  43. imshow("haar_face_detection", frame);
  44. if (waitKey(1) >= 0) break;
  45. }
  46. printf("total face: %d\n", count);
  47. }

2、 DNN人脸检测

  1. #include <opencv2/dnn.hpp>
  2. #include <opencv2/opencv.hpp>
  3. using namespace cv;
  4. using namespace cv::dnn;
  5. #include <iostream>
  6. #include <cstdlib>
  7. using namespace std;
  8. const size_t inWidth = 300;
  9. const size_t inHeight = 300;
  10. const double inScaleFactor = 1.0;
  11. const Scalar meanVal(104.0, 177.0, 123.0);
  12. const float confidenceThreshold = 0.7;
  13. void face_detect_dnn();
  14. void mtcnn_demo();
  15. int main(int argc, char** argv)
  16. {
  17. face_detect_dnn();
  18. waitKey(0);
  19. return 0;
  20. }
  21. void face_detect_dnn() {
  22. //这里采用tensorflow模型
  23. std::string modelBinary = "./models/dnn/face_detector/opencv_face_detector_uint8.pb";
  24. std::string modelDesc = "./models/dnn/face_detector/opencv_face_detector.pbtxt";
  25. // 初始化网络
  26. dnn::Net net = readNetFromTensorflow(modelBinary, modelDesc);
  27. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  28. net.setPreferableTarget(DNN_TARGET_CPU);
  29. if (net.empty())
  30. {
  31. printf("Load models fail...\n");
  32. return;
  33. }
  34. // 打开摄像头
  35. // VideoCapture capture(0);
  36. VideoCapture capture("./video/test.mp4");
  37. if (!capture.isOpened()) {
  38. printf("Don't find video...\n");
  39. return;
  40. }
  41. Mat frame;
  42. int count=0;
  43. while (capture.read(frame)) {
  44. int64 start = getTickCount();
  45. if (frame.empty())
  46. {
  47. break;
  48. }
  49. // 水平镜像调整
  50. // flip(frame, frame, 1);
  51. imshow("input", frame);
  52. if (frame.channels() == 4)
  53. cvtColor(frame, frame, COLOR_BGRA2BGR);
  54. // 输入数据调整
  55. Mat inputBlob = blobFromImage(frame, inScaleFactor,
  56. Size(inWidth, inHeight), meanVal, false, false);
  57. net.setInput(inputBlob, "data");
  58. // 人脸检测
  59. Mat detection = net.forward("detection_out");
  60. vector<double> layersTimings;
  61. double freq = getTickFrequency() / 1000;
  62. double time = net.getPerfProfile(layersTimings) / freq;
  63. Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
  64. ostringstream ss;
  65. for (int i = 0; i < detectionMat.rows; i++)
  66. {
  67. // 置信度 0~1之间
  68. float confidence = detectionMat.at<float>(i, 2);
  69. if (confidence > confidenceThreshold)
  70. {
  71. count++;
  72. int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
  73. int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
  74. int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
  75. int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
  76. Rect object((int)xLeftBottom, (int)yLeftBottom,
  77. (int)(xRightTop - xLeftBottom),
  78. (int)(yRightTop - yLeftBottom));
  79. rectangle(frame, object, Scalar(0, 255, 0));
  80. ss << confidence;
  81. std::string conf(ss.str());
  82. std::string label = "Face: " + conf;
  83. int baseLine = 0;
  84. Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  85. rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
  86. Size(labelSize.width, labelSize.height + baseLine)),
  87. Scalar(255, 255, 255), FILLED);
  88. putText(frame, label, Point(xLeftBottom, yLeftBottom),
  89. FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
  90. }
  91. }
  92. float fps = getTickFrequency() / (getTickCount() - start);
  93. ss.str("");
  94. ss << "FPS: " << fps << " ; inference time: " << time << " ms";
  95. putText(frame, ss.str(), Point(20, 20), 0, 0.75, Scalar(0, 0, 255), 2, 8);
  96. imshow("dnn_face_detection", frame);
  97. if (waitKey(1) >= 0) break;
  98. }
  99. printf("total face: %d\n", count);
  100. }

更多相关文章

  1. Android(安卓)性能优化之内存泄漏检测以及内存优化(中)
  2. Android版本更新知识(检测、升级)总结
  3. android:layout_weight让layout自动调整到剩余高度
  4. android中怎么调整字体的间距和行间距
  5. android 版本检测与自动更新
  6. android webview在弹出软键盘时,布局没有上移的解决办法
  7. 检查Android进程当前是否正在后台运行
  8. android java 检测文件夹(目录)是否存在,不存在则创建
  9. Android控件属性android:fitsSystemWindows="true"的坑

随机推荐

  1. 从Node.js上的mysql结果JSON获取一个Arra
  2. Windows系统下MySQL解压版添加到系统服务
  3. 【MySQL 技巧分享】 mysql -e 加 v 简化
  4. MySQL索引之B+树
  5. 【PHP】当mysql遇上PHP
  6. phpMyAdmin 尝试连接到 MySQL 服务器,但服
  7. mysql 5.7.14 免安装配置方法教程
  8. mysql表导出导入测试(utf8-utf8)
  9. 理解Mysql binlog 日志的三种模
  10. mysql 安装以及卸载 CentOS 7