1
+ // CPP program to detect face in a live video
2
+
3
+ // Include required header files from OpenCV directory
4
+ #include " /usr/local/include/opencv2/objdetect.hpp"
5
+ #include " /usr/local/include/opencv2/highgui.hpp"
6
+ #include " /usr/local/include/opencv2/imgproc.hpp"
7
+ #include < iostream>
8
+
9
+ using namespace std ;
10
+ using namespace cv ;
11
+
12
+ // Function for Face Detection
13
+ void detectAndDraw ( Mat& img, CascadeClassifier& cascade,
14
+ CascadeClassifier& nestedCascade, double scale );
15
+ string cascadeName, nestedCascadeName;
16
+
17
+ int main ( int argc, const char ** argv )
18
+ {
19
+ // VideoCapture class for playing video for which faces to be detected
20
+ VideoCapture capture;
21
+ Mat frame, image;
22
+
23
+ // PreDefined trained XML classifiers with facial features
24
+ CascadeClassifier cascade, nestedCascade;
25
+ double scale=1 ;
26
+
27
+ // Load classifiers from "opencv/data/haarcascades" directory
28
+ nestedCascade.load ( " ../../haarcascade_eye_tree_eyeglasses.xml" ) ;
29
+
30
+ // Change path before execution
31
+ cascade.load ( " ../../haarcascade_frontalcatface.xml" ) ;
32
+
33
+ // Start Video..1) 0 for WebCam 2) "Path to Video" for a Local Video
34
+ capture.open (0 );
35
+ if ( capture.isOpened () )
36
+ {
37
+ // Capture frames from video and detect faces
38
+ cout << " Face Detection Started...." << endl;
39
+ while (1 )
40
+ {
41
+ capture >> frame;
42
+ if ( frame.empty () )
43
+ break ;
44
+ Mat frame1 = frame.clone ();
45
+ detectAndDraw ( frame1, cascade, nestedCascade, scale );
46
+ char c = (char )waitKey (10 );
47
+
48
+ // Press q to exit from window
49
+ if ( c == 27 || c == ' q' || c == ' Q' )
50
+ break ;
51
+ }
52
+ }
53
+ else
54
+ cout<<" Could not Open Camera" ;
55
+ return 0 ;
56
+ }
57
+
58
+ void detectAndDraw ( Mat& img, CascadeClassifier& cascade,
59
+ CascadeClassifier& nestedCascade,
60
+ double scale)
61
+ {
62
+ vector<Rect> faces, faces2;
63
+ Mat gray, smallImg;
64
+
65
+ cvtColor ( img, gray, COLOR_BGR2GRAY ); // Convert to Gray Scale
66
+ double fx = 1 / scale;
67
+
68
+ // Resize the Grayscale Image
69
+ resize ( gray, smallImg, Size (), fx, fx, INTER_LINEAR );
70
+ equalizeHist ( smallImg, smallImg );
71
+
72
+ // Detect faces of different sizes using cascade classifier
73
+ cascade.detectMultiScale ( smallImg, faces, 1.1 ,
74
+ 2 , 0 |CASCADE_SCALE_IMAGE, Size (30 , 30 ) );
75
+
76
+ // Draw circles around the faces
77
+ for ( size_t i = 0 ; i < faces.size (); i++ )
78
+ {
79
+ Rect r = faces[i];
80
+ Mat smallImgROI;
81
+ vector<Rect> nestedObjects;
82
+ Point center;
83
+ Scalar color = Scalar (255 , 0 , 0 ); // Color for Drawing tool
84
+ int radius;
85
+
86
+ double aspect_ratio = (double )r.width /r.height ;
87
+ if ( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
88
+ {
89
+ center.x = cvRound ((r.x + r.width *0.5 )*scale);
90
+ center.y = cvRound ((r.y + r.height *0.5 )*scale);
91
+ radius = cvRound ((r.width + r.height )*0.25 *scale);
92
+ circle ( img, center, radius, color, 3 , 8 , 0 );
93
+ }
94
+ else
95
+ rectangle ( img, cvPoint (cvRound (r.x *scale), cvRound (r.y *scale)),
96
+ cvPoint (cvRound ((r.x + r.width -1 )*scale),
97
+ cvRound ((r.y + r.height -1 )*scale)), color, 3 , 8 , 0 );
98
+ if ( nestedCascade.empty () )
99
+ continue ;
100
+ smallImgROI = smallImg ( r );
101
+
102
+ // Detection of eyes int the input image
103
+ nestedCascade.detectMultiScale ( smallImgROI, nestedObjects, 1.1 , 2 ,
104
+ 0 |CASCADE_SCALE_IMAGE, Size (30 , 30 ) );
105
+
106
+ // Draw circles around eyes
107
+ for ( size_t j = 0 ; j < nestedObjects.size (); j++ )
108
+ {
109
+ Rect nr = nestedObjects[j];
110
+ center.x = cvRound ((r.x + nr.x + nr.width *0.5 )*scale);
111
+ center.y = cvRound ((r.y + nr.y + nr.height *0.5 )*scale);
112
+ radius = cvRound ((nr.width + nr.height )*0.25 *scale);
113
+ circle ( img, center, radius, color, 3 , 8 , 0 );
114
+ }
115
+ }
116
+
117
+ // Show Processed Image with detected faces
118
+ imshow ( " Face Detection" , img );
119
+ }
0 commit comments