#if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID) #include // Gaussian Blur #include // Basic OpenCV structures (cv::Mat, Scalar) #include // OpenCV window I/O #include #include #include #include #include using namespace std; using namespace cv; const string WindowName = "Face Detection example"; class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector { public: CascadeDetectorAdapter(cv::Ptr detector): IDetector(), Detector(detector) { CV_Assert(detector); } void detect(const cv::Mat &Image, std::vector &objects) { Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize); } virtual ~CascadeDetectorAdapter() {} private: CascadeDetectorAdapter(); cv::Ptr Detector; }; int main(int , char** ) { namedWindow(WindowName); VideoCapture VideoStream(0); if (!VideoStream.isOpened()) { printf("Error: Cannot open video stream from camera\n"); return 1; } std::string cascadeFrontalfilename = "../../data/lbpcascades/lbpcascade_frontalface.xml"; cv::Ptr cascade = makePtr(cascadeFrontalfilename); cv::Ptr MainDetector = makePtr(cascade); cascade = makePtr(cascadeFrontalfilename); cv::Ptr TrackingDetector = makePtr(cascade); DetectionBasedTracker::Parameters params; DetectionBasedTracker Detector(MainDetector, TrackingDetector, params); if (!Detector.run()) { printf("Error: Detector initialization failed\n"); return 2; } Mat ReferenceFrame; Mat GrayFrame; vector Faces; while(true) { VideoStream >> ReferenceFrame; cvtColor(ReferenceFrame, GrayFrame, COLOR_RGB2GRAY); Detector.process(GrayFrame); Detector.getObjects(Faces); for (size_t i = 0; i < Faces.size(); i++) { rectangle(ReferenceFrame, Faces[i], Scalar(0,255,0)); } imshow(WindowName, ReferenceFrame); if (waitKey(30) >= 0) break; } Detector.stop(); return 0; } #else #include int main() { printf("This sample works for UNIX or ANDROID only\n"); return 0; } #endif