int main(int argc, char** argv) { // Load the cascade classifiers // Make sure you point the XML files to the right path, or // just copy the files from [OPENCV_DIR]/data/haarcascades directory face_cascade.load("haarcascade_frontalface_alt2.xml"); eye_cascade.load("haarcascade_eye.xml"); // Open webcam cv::VideoCapture cap(0); // Check if everything is ok if (face_cascade.empty() || eye_cascade.empty() || !cap.isOpened()) return 1; // Set video to 320x240 cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); cv::Mat frame, eye_tpl; cv::Rect eye_bb; while (cv::waitKey(15) != 'q') { cap >> frame; if (frame.empty()) break; // Flip the frame horizontally, Windows users might need this cv::flip(frame, frame, 1); // Convert to grayscale and // adjust the image contrast using histogram equalization cv::Mat gray; cv::cvtColor(frame, gray, CV_BGR2GRAY); if (eye_bb.width == 0 && eye_bb.height == 0) { // Detection stage // Try to detect the face and the eye of the user detectEye(gray, eye_tpl, eye_bb); } else { // Tracking stage with template matching trackEye(gray, eye_tpl, eye_bb); // Draw bounding rectangle for the eye cv::rectangle(frame, eye_bb, CV_RGB(0,255,0)); } // Display video cv::imshow("video", frame); } return 0; }
int main(int argc, char** argv) { // Load the cascade classifiers // Make sure you point the XML files to the right path, or // just copy the files from [OPENCV_DIR]/data/haarcascades directory face_cascade.load("haarcascade_frontalface_alt2.xml"); eye_cascade.load("haarcascade_eye.xml"); std::cout << "==================\n"; // Open webcam cv::VideoCapture cap(0); // Check if everything is ok if (face_cascade.empty() || eye_cascade.empty() || !cap.isOpened()) { std::cout << "bad\n"; return 1; } cap.set(CV_CAP_PROP_FRAME_WIDTH, WIDTH); cap.set(CV_CAP_PROP_FRAME_HEIGHT, HEIGHT); cv::Mat frame, eye_tpl; cv::Rect eye_bb; while (cv::waitKey(15) != 'q' && cv::waitKey(15) != 'Q') { cap >> frame; if (frame.empty()) break; // Flip the frame horizontally, Windows users might need this cv::flip(frame, frame, 1); // Convert to greyscale and // adjust the image contrast using histogram equalization cv::Mat gray; cv::cvtColor(frame, gray, CV_BGR2GRAY); if (eye_bb.width == 0 && eye_bb.height == 0) { // Detection stage // Try to detect the face and the eye of the user detectEye(gray, eye_tpl, eye_bb); } else { // Tracking stage with template matching trackEye(gray, eye_tpl, eye_bb); // Draw bounding rectangle for the eye cv::rectangle(frame, eye_bb, CV_RGB(0,255,0)); } {//drawing grids struct Line { cv::Point from, to; }; using Lines = std::vector<Line>; Lines lines{ { { 213, 0 }, { 213, 480 } }, { { 427, 0 }, { 427, 480 } }, { { 0, 160 }, { 640, 160 } }, { { 0, 320 }, { 640, 320 } } }; for (auto const& l : lines) cv::line(frame,l.from, l.to, CV_RGB(0,255,0), 1, 1); } {//generate direction command std::vector<cv::Rect> direction_boxes{ cv::Rect{cv::Point{213, 0}, cv::Point{427, 160}}, //F cv::Rect{cv::Point{ 0, 160}, cv::Point{213, 320}}, //L cv::Rect{cv::Point{427, 160}, cv::Point{640, 320}} //R }; auto draw_direction = [&](std::string const &direction) { cv::putText(frame, direction, cv::Point{280, 435}, cv::FONT_HERSHEY_DUPLEX, 3, CV_RGB(70, 130, 180), 5); cv::putText(frame, direction, cv::Point{280, 435}, cv::FONT_HERSHEY_DUPLEX, 3, CV_RGB(102, 105, 170), 4); }; for(int box = 0; box != 3; ++box) { if (box == 0) if (direction_boxes[0].contains(center_of_rect(eye_bb))) { draw_direction("F"); break; } if (box == 1) if (direction_boxes[1].contains(center_of_rect(eye_bb))) { draw_direction("L"); break; } if (box == 2) if (direction_boxes[2].contains(center_of_rect(eye_bb))) { draw_direction("R"); break; } } std::cout << center_of_rect(eye_bb).x << std::endl; } cv::imshow("video", frame); } return 0; }