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