Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. void FaceDetectandDisplayAll(const string& window_name, Mat frame, int minFaceSize, int minEyeSize, vector<Rect>face)
  2. {
  3. vector<Rect> faces;
  4. Mat frame_gray;
  5. cvtColor(frame, frame_gray, CV_BGR2GRAY);
  6. equalizeHist(frame_gray, frame_gray);
  7. //-- Detect faces
  8. face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE,
  9. Size(minFaceSize, minFaceSize));
  10. for (int i = 0; i < faces.size(); i++)
  11. {
  12. Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
  13. Point upLeft(faces[i].x, faces[i].y);
  14. Point downRight(faces[i].x + faces[i].height, faces[i].y + faces[i].width);
  15.  
  16. rectangle(frame, upLeft, downRight, Scalar(255, 0, 255), 2, 8, 0);
  17. Mat faceROI = frame_gray(faces[i]);
  18.  
  19. std::vector<Rect> eyes;
  20. Rect eyes_rect;
  21. eyes_rect.x = faces[i].x;
  22. eyes_rect.y = faces[i].y + 0.2*faces[i].height;
  23. eyes_rect.width = faces[i].width;
  24. eyes_rect.height = 0.4*faces[i].height;
  25. Mat eyes_ROI = frame_gray(eyes_rect);
  26. eyes_cascade.detectMultiScale(eyes_ROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minEyeSize, minEyeSize));
  27.  
  28.  
  29. for (int j = 0; j < eyes.size(); j++)
  30. {
  31. eyes[j].x = eyes[j].x + faces[i].x;
  32. eyes[j].y = eyes[j].y + faces[i].y + 0.2*faces[i].height;
  33. rectangle(frame, eyes[j], Scalar(255, 0, 0), 2, 8, 0);
  34. }
  35.  
  36. std::vector<Rect> nose;
  37. Rect nose_rect;
  38. nose_rect.x = faces[i].x;
  39. nose_rect.y = faces[i].y + 0.4*faces[i].height;
  40. nose_rect.width = faces[i].width;
  41. nose_rect.height = 0.4*faces[i].height;
  42. Mat nose_ROI = frame_gray(nose_rect);
  43. nose_cascade.detectMultiScale(nose_ROI, nose, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minEyeSize, minEyeSize));
  44.  
  45. for (int j = 0; j < nose.size(); j++)
  46. {
  47. nose[j].x = nose[j].x + faces[i].x;
  48. nose[j].y = nose[j].y + faces[i].y + 0.4*faces[i].height;
  49.  
  50. rectangle(frame, nose[j], Scalar(0, 255, 0), 2, 8, 0);
  51. }
  52.  
  53. std::vector<Rect> mouth;
  54. Rect mouth_rect;
  55. mouth_rect.x = faces[i].x;
  56. mouth_rect.y = faces[i].y + 0.7*faces[i].height;
  57. mouth_rect.width = faces[i].width;
  58. mouth_rect.height = 0.32*faces[i].height;
  59. Mat mouth_ROI = frame_gray(mouth_rect);
  60. mouth_cascade.detectMultiScale(mouth_ROI, mouth, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minEyeSize, minEyeSize));
  61.  
  62. for (int j = 0; j < mouth.size(); j++)
  63. {
  64. mouth[j].x = mouth[j].x + faces[i].x;
  65. mouth[j].y = mouth[j].y + faces[i].y + 0.7*faces[i].height;
  66.  
  67. rectangle(frame, mouth[j], Scalar(0, 0, 255), 2, 8, 0);
  68. }
  69. }
  70. face = faces;
  71. imshow(window_name, frame); //-- Show what you got
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement