Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////// Paste this snipped somewhere callable by the main
- /**
- * @brief Read the webcam video stream and try to find faces
- *
- * @param argc Number of arguments of the main program : should be 2
- * @param argv Arguments of the main program
- * @param scaleFactor Scale factor for the detector
- * @param minNeighborhood Minimal size of the neighbourhood
- * @param frontFaceDetectorType Type of the front face detector
- * @param profileFaceDetectorType Type of the profile face detector
- */
- void webcamFaceDetector(int argc, const char *argv[],
- float scaleFactor = 1.2,
- int minNeighborhood = 5,
- FrontFaceDetectorType frontFaceDetectorType = DefaultFrontFaceDetector,
- ProfileFaceDetectorType profileFaceDetectorType = DefaultProfileFaceDetector)
- {
- // Checking that a webcam is given
- if (arc != 2) {
- throw std::invalid_argument("Usage : 1 argument is required : the device ID of the webcam")
- }
- int deviceId = atoi(argv[1]);
- // Trying to open the webcam
- cv::VideoCapture webcam(deviceId);
- // Check if we can use this device at all:
- if(!webcam.isOpened()) {
- throw std::invalid_argument("Cannot open the specified device")
- }
- // Initializing the detector
- OpenCVCascadeFaceDetector detector(frontDetectorType,
- profileDetectorType,
- scaleFactor,
- minNeighbors);
- // Variables for the detection
- // A frame of the stream
- cv::Mat webcamFrame;
- // The corresponding faceData
- FaceData webcamFaceData;
- // Window to display
- std::string windowName = "Webcam Stream";
- cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE);
- // Boolean for the loop
- bool detectionInProgress = true;
- // Detection loop
- while (detectionInProgress)
- {
- // Extracting the frame
- webcam >> webcamFrame;
- // Loading it into a face data
- webcamFaceData = FaceData(webcamFrame);
- // Perfoming the detection
- detector.estimate(webcamFaceData);
- // Displaying
- if (getWindowProperty(windowName, 0) < 0)
- {
- // The window has been closed
- detectionInProgress = false;
- }
- else
- {
- // TODO write this function
- webcamFaceData.display(false, windowName);
- }
- // Discarding
- webcamFaceData.discardImage();
- }
- // Releasing what is left
- webcamFrame.release();
- webcam.release();
- }
- // In face data
- FaceData::FaceData(cv::mat picture) :
- m_filename("None"),
- m_picture(picture)
- m_rows(picture.rows), m_columns(picture.columns),
- m_truePositive(0), m_trueNegative(0),
- m_falsePositive(0), m_falseNegative(0)
- {
- }
- void FaceData::loadImage()
- {
- if (m_filename != "None")
- {
- m_picture = readImage(m_filename);
- m_rows = m_picture.rows;
- m_columns = m_picture.cols;
- }
- }
- // Ex - displayImageAndWait
- void FaceData::displayImageAndWait(bool wait, const std::string windowName = "") const
- {
- // Loading the picture to be displayed
- cv::Mat image;
- // Checking if the picture is already loaded
- if (m_picture.data)
- {
- image = m_picture.clone();
- }
- else
- {
- // Trying to load the picture
- cv::Mat image = readImage(m_filename);
- if (!image.data) // Check for invalid input
- {
- throw std::invalid_argument("Could not open or find the image");
- }
- }
- for (int row = 0; row < image.rows; row++)
- {
- for (int col = 0; col < image.cols; col++)
- {
- if (isFace(row, col)) {
- colorPixel(image, cv::Point(row, col), cv::Vec3b(255, 0, 0));
- }
- if (isEstimatedFace(row, col)) {
- colorPixel(image, cv::Point(row, col), cv::Vec3b(0, 255, 0));
- }
- }
- }
- if (windowName = "")
- {
- cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
- imshow("Display window", image);
- }
- else
- {
- imshow(windowName, image)
- }
- if (wait)
- {
- cv::waitKey(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement