Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void FaceDetectandDisplayAll(const string& window_name, Mat frame, int minFaceSize, int minEyeSize, vector<Rect>face)
- {
- vector<Rect> faces;
- Mat frame_gray;
- cvtColor(frame, frame_gray, CV_BGR2GRAY);
- equalizeHist(frame_gray, frame_gray);
- //-- Detect faces
- face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE,
- Size(minFaceSize, minFaceSize));
- for (int i = 0; i < faces.size(); i++)
- {
- Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
- Point upLeft(faces[i].x, faces[i].y);
- Point downRight(faces[i].x + faces[i].height, faces[i].y + faces[i].width);
- rectangle(frame, upLeft, downRight, Scalar(255, 0, 255), 2, 8, 0);
- Mat faceROI = frame_gray(faces[i]);
- std::vector<Rect> eyes;
- Rect eyes_rect;
- eyes_rect.x = faces[i].x;
- eyes_rect.y = faces[i].y + 0.2*faces[i].height;
- eyes_rect.width = faces[i].width;
- eyes_rect.height = 0.4*faces[i].height;
- Mat eyes_ROI = frame_gray(eyes_rect);
- eyes_cascade.detectMultiScale(eyes_ROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minEyeSize, minEyeSize));
- for (int j = 0; j < eyes.size(); j++)
- {
- eyes[j].x = eyes[j].x + faces[i].x;
- eyes[j].y = eyes[j].y + faces[i].y + 0.2*faces[i].height;
- rectangle(frame, eyes[j], Scalar(255, 0, 0), 2, 8, 0);
- }
- std::vector<Rect> nose;
- Rect nose_rect;
- nose_rect.x = faces[i].x;
- nose_rect.y = faces[i].y + 0.4*faces[i].height;
- nose_rect.width = faces[i].width;
- nose_rect.height = 0.4*faces[i].height;
- Mat nose_ROI = frame_gray(nose_rect);
- nose_cascade.detectMultiScale(nose_ROI, nose, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minEyeSize, minEyeSize));
- for (int j = 0; j < nose.size(); j++)
- {
- nose[j].x = nose[j].x + faces[i].x;
- nose[j].y = nose[j].y + faces[i].y + 0.4*faces[i].height;
- rectangle(frame, nose[j], Scalar(0, 255, 0), 2, 8, 0);
- }
- std::vector<Rect> mouth;
- Rect mouth_rect;
- mouth_rect.x = faces[i].x;
- mouth_rect.y = faces[i].y + 0.7*faces[i].height;
- mouth_rect.width = faces[i].width;
- mouth_rect.height = 0.32*faces[i].height;
- Mat mouth_ROI = frame_gray(mouth_rect);
- mouth_cascade.detectMultiScale(mouth_ROI, mouth, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(minEyeSize, minEyeSize));
- for (int j = 0; j < mouth.size(); j++)
- {
- mouth[j].x = mouth[j].x + faces[i].x;
- mouth[j].y = mouth[j].y + faces[i].y + 0.7*faces[i].height;
- rectangle(frame, mouth[j], Scalar(0, 0, 255), 2, 8, 0);
- }
- }
- face = faces;
- imshow(window_name, frame); //-- Show what you got
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement