opencv 人脸、人眼识别

雨点打透心脏的1/2处 2022-08-07 16:41 235阅读 0赞
  1. #include "opencv2/objdetect/objdetect.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include <iostream>
  5. #include <stdio.h>
  6. using namespace std;
  7. using namespace cv;
  8. /** Function Headers */
  9. void detectAndDisplay(Mat frame);
  10. /** Global variables */
  11. String face_cascade_name = "haarcascade_frontalface_alt.xml";
  12. String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
  13. CascadeClassifier face_cascade;
  14. CascadeClassifier eyes_cascade;
  15. string window_name = "Capture - Face detection";
  16. RNG rng(12345);
  17. /** @function main */
  18. int main(int argc, const char** argv)
  19. {
  20. CvCapture* capture;
  21. Mat frame;
  22. //-- 1. Load the cascades
  23. if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading\n"); return -1; };
  24. if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading\n"); return -1; };
  25. //-- 2. Read the video stream
  26. capture = cvCaptureFromCAM(-1);
  27. if (capture)
  28. {
  29. while (true)
  30. {
  31. frame = cvQueryFrame(capture);
  32. //-- 3. Apply the classifier to the frame
  33. if (!frame.empty())
  34. {
  35. detectAndDisplay(frame);
  36. }
  37. else
  38. {
  39. printf(" --(!) No captured frame -- Break!"); break;
  40. }
  41. int c = waitKey(10);
  42. if ((char)c == 'c') { break; }
  43. }
  44. }
  45. return 0;
  46. }
  47. /** @function detectAndDisplay */
  48. void detectAndDisplay(Mat frame)
  49. {
  50. std::vector<Rect> faces;
  51. Mat frame_gray;
  52. cvtColor(frame, frame_gray, CV_BGR2GRAY);
  53. equalizeHist(frame_gray, frame_gray);
  54. //-- Detect faces
  55. face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
  56. for (size_t i = 0; i < faces.size(); i++)
  57. {
  58. Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
  59. ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
  60. Mat faceROI = frame_gray(faces[i]);
  61. std::vector<Rect> eyes;
  62. //-- In each face, detect eyes
  63. eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
  64. for (size_t j = 0; j < eyes.size(); j++)
  65. {
  66. Point center(faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5);
  67. int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
  68. circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
  69. }
  70. }
  71. //-- Show what you got
  72. imshow(window_name, frame);
  73. }

发表评论

表情:
评论列表 (有 0 条评论,235人围观)

还没有评论,来说两句吧...

相关阅读

    相关 opencv人脸识别

    我的劳动成果。 1.成功安装了opencv ,可是它还要编译。我放弃。 2.成功训练自己的人脸分类器。(虽然数据是别人的,算法也是系统写好的,图形库也是别人调好的)虽然识别

    相关 opencv人脸识别

    opencv人脸识别: 首先我们来简单的讲一下人脸识别流程:这里采用的是vs2017+opencv3.3扩展库 1、进行人脸检测 2、识别器训练与分类 3、人脸检测