#include "opencv2/opencv.hpp" #include "opencv2/videoio.hpp" #include "opencv2/highgui.hpp" #include const int FPS = 30; bool isDiscardData = true; int countDiscard = 0; const int DISCARD_DURATION = 5; int main() { cv::VideoCapture cap; cap.open(0); if (!cap.isOpened()) { std::cerr << "[ERROR] Unable to open camera!" << std::endl; return -2; } //If Haard Cascade not found: error cv::CascadeClassifier faceDetector; if( !faceDetector.load("./haarcascade_frontalface_alt.xml")) { std::cerr << "[ERROR] Unable to load face cascade" << std::endl; return -1; }; while (true) { if (isDiscardData) { countDiscard++; if (countDiscard == DISCARD_DURATION*FPS) isDiscardData = false; } else { // create a matrix to store the image from the cam cv::Mat frame; // wait for a new frame from camera and store it into 'frame' cap.read(frame); // check if we succeeded if (frame.empty()) { std::cerr << "[ERROR] blank frame grabbed" << std::endl; break; } //Draw Rectangle around the face std::vector faceRectangles; faceDetector.detectMultiScale(frame, faceRectangles, 1.1, 3, 0, cv::Size(20, 20)); cv::rectangle(frame, faceRectangles[0], cv::Scalar(0, 0, 255), 1, 1, 0); //Reduce to ROI cv::Rect foreheadROI; foreheadROI = faceRectangles[0]; foreheadROI.height *= 0.5; cv::imshow("Your Face PLS", frame); if (cv::waitKey(1000.0/FPS) >= 0) break; } } return 0; }