/* $ sudo yum install opencv-devel Compile with: $ gcc -o facedetect facedetect.c -I /usr/include/opencv -lopencv_core -lopencv_imgproc -lopencv_video -lopencv_highgui -lopencv_objdetect Simple test loop: $ while :; do ./facedetect 2>/dev/null; sleep 1;done */ // OpenCV Sample Application: facedetect.c // Include header files #include "cv.h" #include "highgui.h" #include #include #include #include #include #include #include #include #include // Main function, defines the entry point for the program. int main( int argc, char** argv ) { int try=0; int has_faces=0; // Create memory for calculations static CvMemStorage* storage = 0; // Create a new Haar classifier static CvHaarClassifierCascade* cascade = 0; // Structure for getting video from camera or avi CvCapture* capture = 0; // Images to capture the frame from video or camera or from file IplImage *frame, *frame_copy = 0; // Load the HaarClassifierCascade cascade = (CvHaarClassifierCascade*)cvLoad( "/usr/share/OpenCV/haarcascades/haarcascade_eye_tree_eyeglasses.xml", 0, 0, 0 ); // Check whether the cascade has loaded successfully. Else report and error and quit if( !cascade ) { fprintf( stderr, "ERROR: Could not load classifier cascade /usr/share/OpenCV/haarcascades/haarcascade_eye_tree_eyeglasses.xml\n" ); return -1; } // Allocate the memory storage storage = cvCreateMemStorage(0); capture = cvCaptureFromCAM( 0 ); // Find if the capture is loaded successfully or not. // If loaded succesfully, then: if( capture ) { // Capture from the camera. for(has_faces, try=0; (has_faces < 10) && (try < 30); try++) { // Capture the frame and load it in IplImage if( !cvGrabFrame( capture )) break; frame = cvRetrieveFrame( capture, 0); // If the frame does not exist, quit the loop if( !frame ) continue; // Allocate framecopy as the same size of the frame if( !frame_copy ) frame_copy = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels ); // Check the origin of image. If top left, copy the image frame to frame_copy. if( frame->origin == IPL_ORIGIN_TL ) cvCopy( frame, frame_copy, 0 ); // Else flip and copy the image else cvFlip( frame, frame_copy, 0 ); cvClearMemStorage( storage ); // Detect the objects and store them in the sequence CvSeq* faces = cvHaarDetectObjects( frame_copy, cascade, storage, 1.2, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(40, 40), cvSize(40, 40) ); if ( faces ) has_faces+=faces->total; } } cvReleaseCapture( &capture ); if (has_faces) printf("Eyes detected\n"); else printf("No eyes detected\n"); return (has_faces == 0); }