forked from joemcalister/basic-face-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
65 lines (53 loc) · 1.67 KB
/
main.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
// --- Basic face detection ---
//
// By Joe McAlister 2017,
// @portablestorm - https://joemcalister.com,
// Subject to license as included in repo.
//
//
#include <iostream>
#include <stdio.h>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
void detect(Mat frame);
// face cascade classifier
CascadeClassifier face_cascade;
// Function main
int main(void)
{
// load the initial cascade
if (!face_cascade.load("HAAR FILE LOCATION")){
cout << "Error: Couldn't find the Haar file." << endl;
return -1;
}
// load in the image to analyse
Mat frame = imread("IMAGE TO ANALYSE LOCATION");
// apply the classifier
if (!frame.empty()){
detect(frame);
}else{
cout << "Error: No image submitted." << endl;
}
return 0;
}
// detect the faces in the image
void detect(Mat frame)
{
// declare vector of faces variable
vector<Rect> faces;
Mat frame_gray;
// convert image to grayscale
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
// detect the faces in the image -- adjust 4th parameter for detail
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 10, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
// loop through all the faces found
cout << faces.size() << " face(s) were/was found." << endl;
for (int i = 0; i < faces.size(); i++)
{
cout << "Face geometry for face " << i << " -- x:" << faces[i].x << ", y:" << faces[i].y << ", width:" << faces[i].width << ", height:" << faces[i].height << ";" << endl;
}
}