Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. //////////////// Paste this snipped somewhere callable by the main
  2.  
  3. /**
  4. * @brief Read the webcam video stream and try to find faces
  5. *
  6. * @param argc                    Number of arguments of the main program : should be 2
  7. * @param argv                    Arguments of the main program
  8. * @param scaleFactor             Scale factor for the detector
  9. * @param minNeighborhood         Minimal size of the neighbourhood
  10. * @param frontFaceDetectorType   Type of the front face detector
  11. * @param profileFaceDetectorType Type of the profile face detector
  12. */
  13.  
  14. void webcamFaceDetector(int argc, const char *argv[],
  15.                         float scaleFactor = 1.2,
  16.                         int minNeighborhood = 5,
  17.                         FrontFaceDetectorType frontFaceDetectorType = DefaultFrontFaceDetector,
  18.                         ProfileFaceDetectorType profileFaceDetectorType = DefaultProfileFaceDetector)
  19. {
  20.     // Checking that a webcam is given
  21.     if (arc != 2) {
  22.         throw std::invalid_argument("Usage : 1 argument is required : the device ID of the webcam")
  23.     }
  24.     int deviceId = atoi(argv[1]);
  25.     // Trying to open the webcam
  26.     cv::VideoCapture webcam(deviceId);
  27.     // Check if we can use this device at all:
  28.     if(!webcam.isOpened()) {
  29.         throw std::invalid_argument("Cannot open the specified device")
  30.     }
  31.    
  32.  
  33.     // Initializing the detector
  34.     OpenCVCascadeFaceDetector detector(frontDetectorType,
  35.                                        profileDetectorType,
  36.                                        scaleFactor,
  37.                                        minNeighbors);  
  38.  
  39.     // Variables for the detection
  40.     // A frame of the stream
  41.     cv::Mat webcamFrame;
  42.     // The corresponding faceData
  43.     FaceData webcamFaceData;
  44.     // Window to display
  45.     std::string windowName = "Webcam Stream";
  46.     cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE);
  47.  
  48.     // Boolean for the loop
  49.     bool detectionInProgress = true;
  50.    
  51.     // Detection loop
  52.     while (detectionInProgress)
  53.     {
  54.         // Extracting the frame
  55.         webcam >> webcamFrame;
  56.      
  57.         // Loading it into a face data
  58.         webcamFaceData = FaceData(webcamFrame);
  59.          
  60.         // Perfoming the detection
  61.         detector.estimate(webcamFaceData);
  62.      
  63.         // Displaying
  64.         if (getWindowProperty(windowName, 0) < 0)
  65.         {
  66.             // The window has been closed
  67.             detectionInProgress = false;
  68.         }
  69.         else
  70.         {
  71.             // TODO write this function
  72.             webcamFaceData.display(false, windowName);
  73.         }
  74.      
  75.         // Discarding
  76.         webcamFaceData.discardImage();
  77.      
  78.     }
  79.     // Releasing what is left
  80.     webcamFrame.release();
  81.     webcam.release();
  82. }
  83.  
  84. // In face data
  85.  
  86. FaceData::FaceData(cv::mat picture) :
  87.     m_filename("None"),
  88.     m_picture(picture)
  89.     m_rows(picture.rows), m_columns(picture.columns),
  90.     m_truePositive(0), m_trueNegative(0),
  91.     m_falsePositive(0), m_falseNegative(0)
  92. {
  93. }
  94.  
  95.  
  96. void FaceData::loadImage()
  97. {
  98.     if (m_filename != "None")
  99.     {
  100.         m_picture = readImage(m_filename);
  101.         m_rows = m_picture.rows;
  102.         m_columns = m_picture.cols;
  103.     }
  104. }
  105.  
  106. // Ex - displayImageAndWait
  107. void FaceData::displayImageAndWait(bool wait, const std::string windowName = "") const
  108. {
  109.     // Loading the picture to be displayed
  110.     cv::Mat image;
  111.     // Checking if the picture is already loaded
  112.     if (m_picture.data)
  113.     {
  114.         image = m_picture.clone();
  115.     }
  116.     else
  117.     {
  118.         // Trying to load the picture
  119.         cv::Mat image = readImage(m_filename);
  120.         if (!image.data) // Check for invalid input
  121.         {
  122.             throw std::invalid_argument("Could not open or find the image");
  123.         }
  124.     }
  125.    
  126.     for (int row = 0; row < image.rows; row++)
  127.     {
  128.         for (int col = 0; col < image.cols; col++)
  129.         {
  130.             if (isFace(row, col)) {
  131.                 colorPixel(image, cv::Point(row, col), cv::Vec3b(255, 0, 0));
  132.             }
  133.  
  134.             if (isEstimatedFace(row, col)) {
  135.                 colorPixel(image, cv::Point(row, col), cv::Vec3b(0, 255, 0));
  136.             }
  137.         }
  138.     }
  139.  
  140.     if (windowName = "")
  141.     {
  142.         cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);  
  143.         imshow("Display window", image);    
  144.     }
  145.     else
  146.     {
  147.         imshow(windowName, image)
  148.     }
  149.  
  150.     if (wait)
  151.     {
  152.         cv::waitKey(0);  
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement