-
Notifications
You must be signed in to change notification settings - Fork 0
/
face.cpp
83 lines (64 loc) · 2.24 KB
/
face.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/////////////////////////////////////////////////////////////////////////////
//
// COMS30121 - face.cpp
//
/////////////////////////////////////////////////////////////////////////////
// header inclusion
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "p1.cpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay( Mat frame );
/** Global variables */
String cascade_name = "dartcascade/cascade.xml";
CascadeClassifier cascade;
/** @function main */
int main( int argc, const char** argv )
{
// 1. Read Input Image
Mat frame = imread(argv[1], CV_LOAD_IMAGE_COLOR);
// 2. Load the Strong Classifier in a structure called `Cascade'
if( !cascade.load( cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
// 3. Detect Faces and Display Result
detectAndDisplay( frame );
// 4. Save Result Image
const char* output = argv[2];
imwrite( output, frame );
return 0;
}
/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
// 1. Prepare Image by turning it into Grayscale and normalising lighting
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
// 2. Perform Viola-Jones Object Detection
cascade.detectMultiScale( frame_gray, faces, 1.1, 1, 0|CV_HAAR_SCALE_IMAGE, Size(50, 50), Size(500,500) );
// 3. Print number of Faces found
// std::cout << faces.size() << std::endl;
// 4. Draw box around faces found
std::vector<Rect> points;
doDetect(frame, points);
for( int i = 0; i < faces.size(); i++ )
{
rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar( 0, 0, 0 ), 2);
}
for (int i = 0; i < faces.size(); i++){
for (int j = 0; j < points.size(); j++){
// make sure points from hough are inside viola jones
Rect intersect = faces[i] & points[j];
if ((intersect == points[j])){
rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar( 255, 0, 0 ), 3);
}
}
}
}