dbt_face_detection.cpp 3.0 KB

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