diff --git a/FacialLandmarkDetection/drawLandmarks.hpp b/FacialLandmarkDetection/drawLandmarks.hpp new file mode 100644 index 0000000..ea724ca --- /dev/null +++ b/FacialLandmarkDetection/drawLandmarks.hpp @@ -0,0 +1,59 @@ +#ifndef _renderFace_H_ +#define _renderFace_H_ + +using namespace cv; +using namespace std; + +#define COLOR Scalar(255, 200,0) + +// drawPolyLine draws a poly line by joining +// successive points between the start and end indices. +void drawPolyline +( + Mat &im, + const vector &landmarks, + const int start, + const int end, + bool isClosed = false +) +{ + // Gather all points between the start and end indices + vector points; + for (int i = start; i <= end; i++) + { + points.push_back(cv::Point(landmarks[i].x, landmarks[i].y)); + } + // Draw polylines. + polylines(im, points, isClosed, COLOR, 2, 16); + +} + + +void drawLandmarks(Mat &im, vector &landmarks) +{ + // Draw face for the 68-point model. + if (landmarks.size() == 68) + { + drawPolyline(im, landmarks, 0, 16); // Jaw line + drawPolyline(im, landmarks, 17, 21); // Left eyebrow + drawPolyline(im, landmarks, 22, 26); // Right eyebrow + drawPolyline(im, landmarks, 27, 30); // Nose bridge + drawPolyline(im, landmarks, 30, 35, true); // Lower nose + drawPolyline(im, landmarks, 36, 41, true); // Left eye + drawPolyline(im, landmarks, 42, 47, true); // Right Eye + drawPolyline(im, landmarks, 48, 59, true); // Outer lip + drawPolyline(im, landmarks, 60, 67, true); // Inner lip + } + else + { // If the number of points is not 68, we do not know which + // points correspond to which facial features. So, we draw + // one dot per landamrk. + for(int i = 0; i < landmarks.size(); i++) + { + circle(im,landmarks[i],3, COLOR, FILLED); + } + } + +} + +#endif // _renderFace_H_ \ No newline at end of file diff --git a/FacialLandmarkDetection/facialLandmarkDetection.cpp b/FacialLandmarkDetection/facialLandmarkDetection.cpp index 92c4b06..3101c42 100644 --- a/FacialLandmarkDetection/facialLandmarkDetection.cpp +++ b/FacialLandmarkDetection/facialLandmarkDetection.cpp @@ -1,97 +1,66 @@ #include #include -#include "renderFace.hpp" +#include "drawLandmarks.hpp" -#include -#include using namespace std; using namespace cv; using namespace cv::face; -static bool detectFace(InputArray image, OutputArray faces, CascadeClassifier *cascade) -{ - // Convert image to grayscale - Mat imGray; - cvtColor(image, imGray, COLOR_BGR2GRAY); - - // Variable for storing face rectangles - vector facesRects; - - // Run OpenCV HAAR based face detector - cascade->detectMultiScale(imGray, facesRects, 1.5, 5, CASCADE_SCALE_IMAGE, Size(100, 100)); - - // Copy results to output array - Mat(facesRects).copyTo(faces); - return true; -} - int main(int argc,char** argv) { // Load Face Detector CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml"); - // Create an instance of FacemarkLBF. - // The optional argument params can be used to read the parameters - // of the trained model - FacemarkLBF::Params params; - Ptr facemark = FacemarkLBF::create(params); + // Create an instance of Facemark + Ptr facemark = FacemarkLBF::create(); - // Set face detector - facemark->setFaceDetector((FN_FaceDetector)detectFace, &faceDetector); - // Load landmark detector facemark->loadModel("lbfmodel.yaml"); - + // Set up webcam for video capture VideoCapture cam(0); - // Variable to store a video frame - Mat frame; - - // Variable to store detected face rectangles - vector faces; + // Variable to store a video frame and its grayscale + Mat frame, gray; - int count = 0; - // Read a frame while(cam.read(frame)) { - // Run face detection every 10th frame. - // This is done for speedup - if (count % 10 == 0) - { - faces.clear(); - facemark->getFaces(frame,faces); - count = 0; - } + + // Find face + vector faces; + // Convert frame to grayscale because + // faceDetector requires grayscale image. + cvtColor(frame, gray, COLOR_BGR2GRAY); + + // Detect faces + faceDetector.detectMultiScale(gray, faces); // Variable for landmarks. // Landmarks for one face is a vector of points - // There can be more than one face. Hence, we + // There can be more than one face in the image. Hence, we // use a vector of vector of points. vector< vector > landmarks; - + // Run landmark detector bool success = facemark->fit(frame,faces,landmarks); + if(success) { // If successful, render the landmarks on the face for(int i = 0; i < landmarks.size(); i++) { - renderFace(frame, landmarks[i]); - } - // Display results - imshow("Facial Landmark Detection", frame); - int key = waitKey(1); - - // Exit loop if ESC is pressed - if (key == 27) - { - break; + drawLandmarks(frame, landmarks[i]); } } + + // Display results + imshow("Facial Landmark Detection", frame); + // Exit loop if ESC is pressed + if (waitKey(1) == 27) break; + } return 0; }