| 1 | #include <cv.h> |
| 2 | #include <highgui.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | CvHaarClassifierCascade* load_object_detector( const char* cascade_path ) |
| 6 | { |
| 7 | return (CvHaarClassifierCascade*)cvLoad( cascade_path ); |
| 8 | } |
| 9 | |
| 10 | void detect_and_draw_objects(IplImage* image, |
| 11 | CvHaarClassifierCascade* cascade, |
| 12 | int do_pyramids) |
| 13 | { |
| 14 | IplImage* small_image = image; |
| 15 | CvMemStorage* storage = cvCreateMemStorage(0); |
| 16 | CvSeq* faces; |
| 17 | int i, scale = 1; |
| 18 | |
| 19 | /* if the flag is specified, down-scale the input image to get a |
| 20 | performance boost w/o loosing quality (perhaps) */ |
| 21 | if( do_pyramids ) |
| 22 | { |
| 23 | small_image = cvCreateImage( cvSize(image->width/2,image->height/2), IPL_DEPTH_8U, 3 ); |
| 24 | cvPyrDown( image, small_image, CV_GAUSSIAN_5x5 ); |
| 25 | scale = 2; |
| 26 | } |
| 27 | |
| 28 | /* use the fastest variant */ |
| 29 | faces = cvHaarDetectObjects( small_image, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING ); |
| 30 | |
| 31 | /* draw all the rectangles */ |
| 32 | for( i = 0; i < faces->total; i++ ) |
| 33 | { |
| 34 | /* extract the rectangles only */ |
| 35 | //CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i, 0 ); |
| 36 | CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i); |
| 37 | cvRectangle( image, cvPoint(face_rect.x*scale,face_rect.y*scale), |
| 38 | cvPoint((face_rect.x+face_rect.width)*scale, |
| 39 | (face_rect.y+face_rect.height)*scale), |
| 40 | CV_RGB(255,0,0), 3 ); |
| 41 | } |
| 42 | |
| 43 | if( small_image != image ) |
| 44 | cvReleaseImage( &small_image ); |
| 45 | cvReleaseMemStorage( &storage ); |
| 46 | } |
| 47 | |
| 48 | // A Simple Camera Capture Framework |
| 49 | int main( int argc, char** argv ) |
| 50 | { |
| 51 | CvHaarClassifierCascade* cascade = load_object_detector(argv[1]); |
| 52 | |
| 53 | CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY ); |
| 54 | if( !capture ) { |
| 55 | fprintf( stderr, "ERROR: capture is NULL \n" ); |
| 56 | getchar(); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | // Create a window in which the captured images will be presented |
| 61 | cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); |
| 62 | |
| 63 | // Show the image captured from the camera in the window and repeat |
| 64 | while( 1 ) { |
| 65 | // Get one frame |
| 66 | IplImage* frame = cvQueryFrame( capture ); |
| 67 | if( !frame ) { |
| 68 | fprintf( stderr, "ERROR: frame is null...\n" ); |
| 69 | getchar(); |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | detect_and_draw_objects( frame, cascade, 1 ); |
| 74 | cvShowImage( "mywindow", frame ); |
| 75 | // Do not release the frame! |
| 76 | |
| 77 | //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), |
| 78 | //remove higher bits using AND operator |
| 79 | if( (cvWaitKey(10) & 255) == 27 ) break; |
| 80 | } |
| 81 | |
| 82 | // Release the capture device housekeeping |
| 83 | cvReleaseCapture( &capture ); |
| 84 | cvDestroyWindow( "mywindow" ); |
| 85 | return 0; |
| 86 | } |