-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (56 loc) · 2.22 KB
/
main.cpp
File metadata and controls
64 lines (56 loc) · 2.22 KB
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
#include <string>
#include <cstdlib>
#include "opencv2/opencv.hpp"
const int REQ_WIDTH = 320;
int main (int argc, char** argv) {
// Load a pretrained LBP detector for frontal face detection
cv::CascadeClassifier faceDetector;
try {
faceDetector.load("lbpcascade_frontalface.xml");
} catch (cv::Exception e) {
std::cerr << "Could not load face detector" << std::endl;
exit(1);
}
cv::VideoCapture camera(0); // Access webcam
// // Set the camera resolution
// camera.set(CV_CAP_PROP_FRAME_WIDTH, 640);
// camera.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
while (true) {
// get next camera frame
cv::Mat cameraFrame;
camera >> cameraFrame;
// Convert frame to grayscale
cv::Mat gray;
if (cameraFrame.channels() == 3) {
// cameraFrame is a BGR image
cvtColor(cameraFrame, gray, CV_BGR2GRAY);
} else if (cameraFrame.channels() == 4) {
// cameraFrame is a BGRA image
cvtColor(cameraFrame, gray, CV_BGRA2GRAY);
} else {
// cameraFrame is already in grayscale
gray = cameraFrame;
}
// Shrink the grayscale image
cv::Mat smallImg;
float scale = gray.cols / (float) REQ_WIDTH;
if (scale > 1) {
// Shrink image
int scaledHeight = cvRound(gray.rows / scale);
resize(gray, smallImg, cv::Size(REQ_WIDTH, scaledHeight));
} else {
// Input is already small enough
smallImg = gray;
}
// Perform histogram equalization
cv::Mat equalizedImg;
equalizeHist(smallImg, equalizedImg);
int flags = cv::CASCADE_FIND_BIGGEST_OBJECT | cv::CASCADE_DO_ROUGH_SEARCH; // Search for single face. Change to CASCADE_SCALE_IMAGE to search for many faces
cv::Size minFeatureSize(80, 80); // Change to 20 x 20 if you need to detect far away faces
float searchScaleFactor = 1.1f;
int minNeighbours = 4;
std::vector<cv::Rect> faces;
faceDetector.detectMultiScale(equalizedImg, faces, searchScaleFactor, minNeighbours, flags, minFeatureSize);
std::cout << "Number of faces detected: " << faces.size() << std::endl;
}
}