Opencv-real-time detection of human eyes on cameras

Source: Internet
Author: User

This article mainly applies the built-in functions cascadeclassifier, load and detecmultiscale of opencv to implement real-time detection of human eyes on the camera!

# Include "opencv2/objdetect. HPP "# include" opencv2/highgui. HPP "# include" opencv2/imgproc. HPP "# include <iostream> # include <stdio. h> // namespace using namespace STD; using namespace CV; // function declaration void detectanddisplay (MAT frame); // global variable // -- note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locationsstring face_cascade_name = "haarcas Response "; string response =" response "; cascadeclassifier face_cascade; cascadeclassifier eyes_cascade; string window_name =" Capture-Face Detection "; RNG (12345); int main (void) {cvcapture * capture; MAT frame; // -- 1. load the cascades if (! Face_cascade.load (face_cascade_name) {printf ("--(!) Error loading \ n "); Return-1 ;}; if (! Eyes_cascade.load (eyes_cascade_name) {printf ("--(!) Error loading \ n "); Return-1 ;}; // -- 2. read the video stream capture = cvcapturefromcam (-1); If (capture) {for (;) {frame = cvqueryframe (capture); // -- 3. apply the classifier to the frame if (! Frame. Empty () {detectanddisplay (FRAME);} else {printf ("--(!) No captured frame -- break! "); Break;} int c = waitkey (10); If (char) C = 'C') {break ;}} return 0 ;} void detectanddisplay (MAT frame) {STD: vector <rect> faces; MAT frame_gray; cvtcolor (frame, frame_gray, cv_bgr2gray); equalizehist (frame_gray, frame_gray ); // -- detect faces face_cascade.detectmultiscale (frame_gray, faces, 1.1, 2, 0 | cv_haar_scale_image, size (30, 30); For (size_t I = 0; I <faces. size (); I ++) {Point Center (faces [I]. X + faces [I]. width/2, faces [I]. Y + faces [I]. height/2); ellipse (frame, center, size (faces [I]. width/2, faces [I]. height/2), 0, 0,360, scalar (255, 0,255), 2, 8, 0); MAT faceroi = frame_gray (faces [I]); STD :: vector <rect> eyes; // -- in each face, detect eyes eyes_cascade.detectmultiscale (faceroi, eyes, 1.1, 2, 0 | cv_haar_scale_image, size (30, 30 )); for (size_t J = 0; j <eyes. size (); j ++) {point eye_center (faces [I]. X + eyes [J]. X + eyes [J]. width/2, faces [I]. Y + eyes [J]. Y + eyes [J]. height/2); int radius = cvround (eyes [J]. width + eyes [J]. height) * 0.25); Circle (frame, eye_center, radius, scalar (255, 0, 0), 3, 8, 0 );}} // -- show what you got imshow (window_name, frame );}

The above two files can be found in path: D: \ Program Files \ opencv2.4.3 \ opencv \ data \ haarcascades.

After finding them, put the two files under the program directory, for example, my program directory location:

The test is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.