Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FacialLandmarkDetection/facialLandmarkDetection.cpp
3119 views
1
#include <opencv2/opencv.hpp>
2
#include <opencv2/face.hpp>
3
#include "drawLandmarks.hpp"
4
5
6
using namespace std;
7
using namespace cv;
8
using namespace cv::face;
9
10
11
int main(int argc,char** argv)
12
{
13
// Load Face Detector
14
CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
15
16
// Create an instance of Facemark
17
Ptr<Facemark> facemark = FacemarkLBF::create();
18
19
// Load landmark detector
20
facemark->loadModel("lbfmodel.yaml");
21
22
// Set up webcam for video capture
23
VideoCapture cam(0);
24
25
// Variable to store a video frame and its grayscale
26
Mat frame, gray;
27
28
// Read a frame
29
while(cam.read(frame))
30
{
31
32
// Find face
33
vector<Rect> faces;
34
// Convert frame to grayscale because
35
// faceDetector requires grayscale image.
36
cvtColor(frame, gray, COLOR_BGR2GRAY);
37
38
// Detect faces
39
faceDetector.detectMultiScale(gray, faces);
40
41
// Variable for landmarks.
42
// Landmarks for one face is a vector of points
43
// There can be more than one face in the image. Hence, we
44
// use a vector of vector of points.
45
vector< vector<Point2f> > landmarks;
46
47
// Run landmark detector
48
bool success = facemark->fit(frame,faces,landmarks);
49
50
if(success)
51
{
52
// If successful, render the landmarks on the face
53
for(int i = 0; i < landmarks.size(); i++)
54
{
55
drawLandmarks(frame, landmarks[i]);
56
}
57
}
58
59
// Display results
60
imshow("Facial Landmark Detection", frame);
61
// Exit loop if ESC is pressed
62
if (waitKey(1) == 27) break;
63
64
}
65
return 0;
66
}
67
68