Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/dbt_face_detection.cpp
16337 views
1
#if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID) || (defined(_MSC_VER) && _MSC_VER>=1800)
2
3
#include <opencv2/imgproc.hpp> // Gaussian Blur
4
#include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
5
#include <opencv2/videoio.hpp>
6
#include <opencv2/highgui.hpp> // OpenCV window I/O
7
#include <opencv2/features2d.hpp>
8
#include <opencv2/objdetect.hpp>
9
10
#include <stdio.h>
11
12
using namespace std;
13
using namespace cv;
14
15
const string WindowName = "Face Detection example";
16
17
class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
18
{
19
public:
20
CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
21
IDetector(),
22
Detector(detector)
23
{
24
CV_Assert(detector);
25
}
26
27
void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects) CV_OVERRIDE
28
{
29
Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
30
}
31
32
virtual ~CascadeDetectorAdapter() CV_OVERRIDE
33
{}
34
35
private:
36
CascadeDetectorAdapter();
37
cv::Ptr<cv::CascadeClassifier> Detector;
38
};
39
40
int main(int , char** )
41
{
42
namedWindow(WindowName);
43
44
VideoCapture VideoStream(0);
45
46
if (!VideoStream.isOpened())
47
{
48
printf("Error: Cannot open video stream from camera\n");
49
return 1;
50
}
51
52
std::string cascadeFrontalfilename = "../../data/lbpcascades/lbpcascade_frontalface.xml";
53
cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
54
cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade);
55
if ( cascade->empty() )
56
{
57
printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str());
58
return 2;
59
}
60
61
cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
62
cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade);
63
if ( cascade->empty() )
64
{
65
printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str());
66
return 2;
67
}
68
69
DetectionBasedTracker::Parameters params;
70
DetectionBasedTracker Detector(MainDetector, TrackingDetector, params);
71
72
if (!Detector.run())
73
{
74
printf("Error: Detector initialization failed\n");
75
return 2;
76
}
77
78
Mat ReferenceFrame;
79
Mat GrayFrame;
80
vector<Rect> Faces;
81
82
do
83
{
84
VideoStream >> ReferenceFrame;
85
cvtColor(ReferenceFrame, GrayFrame, COLOR_BGR2GRAY);
86
Detector.process(GrayFrame);
87
Detector.getObjects(Faces);
88
89
for (size_t i = 0; i < Faces.size(); i++)
90
{
91
rectangle(ReferenceFrame, Faces[i], Scalar(0,255,0));
92
}
93
94
imshow(WindowName, ReferenceFrame);
95
} while (waitKey(30) < 0);
96
97
Detector.stop();
98
99
return 0;
100
}
101
102
#else
103
104
#include <stdio.h>
105
int main()
106
{
107
printf("This sample works for UNIX or ANDROID or Visual Studio 2013+ only\n");
108
return 0;
109
}
110
111
#endif
112
113