-
Notifications
You must be signed in to change notification settings - Fork 189
/
FaceDetectorAndTracker.h
97 lines (72 loc) · 2.43 KB
/
FaceDetectorAndTracker.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <opencv2/core/core.hpp>
#include <vector>
#include <string>
#include <memory>
namespace cv
{
class VideoCapture;
class CascadeClassifier;
}
class FaceDetectorAndTracker
{
public:
/*
* Initializes detector with cascade file, initializes camera with camera index and sets number of faces to track
*/
FaceDetectorAndTracker(const std::string cascadeFilePath, const int cameraIndex, size_t numFaces);
~FaceDetectorAndTracker();
/*
* Returns next camera frame and detects faces
*/
void operator>>(cv::Mat &frame);
/*
* Returns vector of detected faces
*/
std::vector<cv::Rect> faces();
private:
void detect();
void track();
/* Returns double inputRect size centered around the same point */
static cv::Rect doubleRectSize(const cv::Rect &rect, const cv::Size &frameSize);
/*
* Private members
*/
/*
* Video capture object used for retrieving camera frames
*/
std::unique_ptr<cv::VideoCapture> m_camera;
/*
* Cascade classifier object used for detecting faces in frames
*/
std::unique_ptr<cv::CascadeClassifier> m_faceCascade;
/*
* Downscaled camera frame. Downscaling speeds up detection
*/
cv::Mat m_downscaledFrame;
/*
* Width of downscaled camera frame. Height is calculated to preserve aspect ratio
*/
static const int m_downscaledFrameWidth = 256;
/*
* Vector of rectangles representing faces in camera frame
*/
std::vector<cv::Rect> m_facesRects;
/*
* Vector of vector of faces. Used in tracking. One vector per detected face
*/
std::vector<cv::Rect> m_tmpFacesRect;
std::vector<bool> m_tmRunningInRoi;
std::vector<long long> m_tmStartTime;
std::vector<long long> m_tmEndTime;
std::vector<cv::Point2f> m_facePositions;
std::vector<cv::Mat> m_faceTemplates;
std::vector<cv::Rect> m_faceRois;
cv::Mat m_matchingResult;
cv::Size m_downscaledFrameSize;
cv::Size m_originalFrameSize;
cv::Point2f m_ratio;
bool m_tracking = false;
size_t m_numFaces = 0;
const double m_tmMaxDuration = 2.0;
};