Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Point detectFace(Mat frame, Point priorCenter) {
- std::vector<Rect> faces;
- Mat frame_gray, frame_lab, output, temp;
- int h = frame.size().height - 1;
- int w = frame.size().width - 1;
- int minNeighbors = 2;
- bool faceNotFound = false;
- //CascadeClassifier eyeCascade1;
- //CascadeClassifier eyeCascade2;
- cvtColor(frame, frame_gray, COLOR_BGR2GRAY); // Convert to gray
- equalizeHist(frame_gray, frame_gray); // Equalize histogram
- // Detect face with open source cascade
- face_cascade.detectMultiScale(frame_gray, faces,
- 1.1, minNeighbors,
- 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
- // iterate over faces
- for (size_t i = 0; i < faces.size(); i++) {
- // Find center of face
- Point center(faces[i].x + faces[i].width / 2,
- faces[i].y + faces[i].height / 2);
- // Generate width and height of face, round to closest 1/4 of frame height
- h = roundUp(faces[i].height, frame.size().height / 4);
- w = 3 * h / 5;
- // If priorCenter not yet initialized, initialize
- if (priorCenter.x == 0) {
- priorCenter = center;
- temp = outputFrame(frame, center, w, h);
- break;
- }
- // Check to see if it's probably the same user
- if (abs(center.x - priorCenter.x) < frame.size().width / 6 &&
- abs(center.y - priorCenter.y) < frame.size().height / 6) {
- // Check to see if the user moved enough to update position
- if (abs(center.x - priorCenter.x) < 7 &&
- abs(center.y - priorCenter.y) < 7){
- center = priorCenter;
- }
- // Smooth new center compared to old center
- center.x = (center.x + 2 * priorCenter.x) / 3;
- center.y = (center.y + 2 * priorCenter.y) / 3;
- priorCenter = center;
- // output frame of only face
- temp = outputFrame(frame, center, w, h);
- break; // exit, primary users face probably found
- }
- else {
- faceNotFound = true;
- }
- }
- if (faceNotFound)
- {
- // Findface from eyes
- Rect r(priorCenter.x, priorCenter.y, w, h);
- if (priorCenter.x + w > frame_gray.size().width - 2 &&
- priorCenter.y + h > frame_gray.size().height - 2)
- {
- priorCenter = faceFromEyes(priorCenter, frame_gray(r));
- // Generate temporary face location
- temp = outputFrame(frame, priorCenter, w, h);
- }
- }
- // Check to see if new face found
- if (temp.size().width > 2)
- {
- output = temp;
- }
- else
- {
- output = frame;
- }
- // Display only face
- imshow("Face ", output);
- if (output.size().width > 2)
- // Draw ellipse around face
- //ellipse(frame, priorCenter, Size(w / 2, h / 2),0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
- {
- //Point pt(priorCenter.x + w, priorCenter.y + h);
- // Point pt2(priorCenter.x, priorCenter.y);
- //rectangle(output, pt2, pt, CV_RGB(255, 0, 0), 2, CV_AA);
- ellipse(frame, priorCenter, Size(w / 2, h / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
- //rectangle(frame, priorCenter,priorCenter CV_RGB(255, 0, 0), 2, CV_AA);
- // Display output
- imshow("output ", frame);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement