Download samples/cpp/dbt_face_detection.cpp from tgollogly/test: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/tgollogly/test/resolve/main/samples/cpp/dbt_face_detection.cpp
- Command line
-
hf download hf://tgollogly/test/samples/cpp/dbt_face_detection.cpp
-
curl -L -o dbt_face_detection.cpp https://huggingface.co/tgollogly/test/resolve/main/samples/cpp/dbt_face_detection.cpp
3.03 kB
| using namespace std; | |
| using namespace cv; | |
| const string WindowName = "Face Detection example"; | |
| class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector | |
| { | |
| public: | |
| CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector): | |
| IDetector(), | |
| Detector(detector) | |
| { | |
| CV_Assert(detector); | |
| } | |
| void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects) CV_OVERRIDE | |
| { | |
| Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize); | |
| } | |
| virtual ~CascadeDetectorAdapter() CV_OVERRIDE | |
| {} | |
| private: | |
| CascadeDetectorAdapter(); | |
| cv::Ptr<cv::CascadeClassifier> 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 = samples::findFile("data/lbpcascades/lbpcascade_frontalface.xml"); | |
| cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename); | |
| cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade); | |
| if ( cascade->empty() ) | |
| { | |
| printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str()); | |
| return 2; | |
| } | |
| cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename); | |
| cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade); | |
| if ( cascade->empty() ) | |
| { | |
| printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str()); | |
| return 2; | |
| } | |
| DetectionBasedTracker::Parameters params; | |
| DetectionBasedTracker Detector(MainDetector, TrackingDetector, params); | |
| if (!Detector.run()) | |
| { | |
| printf("Error: Detector initialization failed\n"); | |
| return 2; | |
| } | |
| Mat ReferenceFrame; | |
| Mat GrayFrame; | |
| vector<Rect> Faces; | |
| do | |
| { | |
| VideoStream >> ReferenceFrame; | |
| cvtColor(ReferenceFrame, GrayFrame, COLOR_BGR2GRAY); | |
| 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); | |
| } while (waitKey(30) < 0); | |
| Detector.stop(); | |
| return 0; | |
| } | |
| int main() | |
| { | |
| printf("This sample works for UNIX or ANDROID or Visual Studio 2013+ only\n"); | |
| return 0; | |
| } | |