Path: blob/master/FacialLandmarkDetection/facialLandmarkDetection.cpp
3119 views
#include <opencv2/opencv.hpp>1#include <opencv2/face.hpp>2#include "drawLandmarks.hpp"345using namespace std;6using namespace cv;7using namespace cv::face;8910int main(int argc,char** argv)11{12// Load Face Detector13CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");1415// Create an instance of Facemark16Ptr<Facemark> facemark = FacemarkLBF::create();1718// Load landmark detector19facemark->loadModel("lbfmodel.yaml");2021// Set up webcam for video capture22VideoCapture cam(0);2324// Variable to store a video frame and its grayscale25Mat frame, gray;2627// Read a frame28while(cam.read(frame))29{3031// Find face32vector<Rect> faces;33// Convert frame to grayscale because34// faceDetector requires grayscale image.35cvtColor(frame, gray, COLOR_BGR2GRAY);3637// Detect faces38faceDetector.detectMultiScale(gray, faces);3940// Variable for landmarks.41// Landmarks for one face is a vector of points42// There can be more than one face in the image. Hence, we43// use a vector of vector of points.44vector< vector<Point2f> > landmarks;4546// Run landmark detector47bool success = facemark->fit(frame,faces,landmarks);4849if(success)50{51// If successful, render the landmarks on the face52for(int i = 0; i < landmarks.size(); i++)53{54drawLandmarks(frame, landmarks[i]);55}56}5758// Display results59imshow("Facial Landmark Detection", frame);60// Exit loop if ESC is pressed61if (waitKey(1) == 27) break;6263}64return 0;65}666768