ASCII码 ASCII码

OpenCV实现人脸检测功能

发布于:2022-03-31 10:22:19  栏目:技术文档

本文实例为大家分享了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. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

相关推荐
阅读 +