| 1 | //use the camera to aid the decision to sleep. |
| 2 | //Copyright 2009 Daniel Watson |
| 3 | /* |
| 4 | This file is part of oble. |
| 5 | |
| 6 | oble is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | oble is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with oble. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | */ |
| 20 | #include <cv.h> |
| 21 | #include <highgui.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | CvHaarClassifierCascade* load_object_detector(const char* cascade_path) |
| 25 | { |
| 26 | return (CvHaarClassifierCascade*)cvLoad( cascade_path ); |
| 27 | } |
| 28 | |
| 29 | void detect_and_draw_objects(IplImage* image, |
| 30 | CvHaarClassifierCascade* cascade, |
| 31 | int do_pyramids) |
| 32 | { |
| 33 | IplImage* small_image = image; |
| 34 | CvMemStorage* storage = cvCreateMemStorage(0); |
| 35 | CvSeq* faces; |
| 36 | int i, scale = 1; |
| 37 | |
| 38 | /* if the flag is specified, down-scale the input image to get a |
| 39 | performance boost w/o loosing quality (perhaps) */ |
| 40 | if( do_pyramids ) |
| 41 | { |
| 42 | small_image = cvCreateImage( cvSize(image->width/2,image->height/2), IPL_DEPTH_8U, 3 ); |
| 43 | cvPyrDown( image, small_image, CV_GAUSSIAN_5x5 ); |
| 44 | scale = 2; |
| 45 | } |
| 46 | |
| 47 | /* use the fastest variant */ |
| 48 | faces = cvHaarDetectObjects( small_image, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING ); |
| 49 | |
| 50 | /* draw all the rectangles */ |
| 51 | for( i = 0; i < faces->total; i++ ) |
| 52 | { |
| 53 | /* extract the rectangles only */ |
| 54 | //CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i, 0 ); |
| 55 | CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i); |
| 56 | cvRectangle( image, cvPoint(face_rect.x*scale,face_rect.y*scale), |
| 57 | cvPoint((face_rect.x+face_rect.width)*scale, |
| 58 | (face_rect.y+face_rect.height)*scale), |
| 59 | CV_RGB(255,0,0), 3 ); |
| 60 | } |
| 61 | |
| 62 | if( small_image != image ) |
| 63 | cvReleaseImage( &small_image ); |
| 64 | cvReleaseMemStorage( &storage ); |
| 65 | } |
| 66 | |
| 67 | // A Simple Camera Capture Framework. |
| 68 | >>>>>>> experimental:hello-world.cpp |
| 69 | int main( int argc, char** argv ) |
| 70 | { |
| 71 | CvHaarClassifierCascade* cascade = load_object_detector(argv[1]); |
| 72 | |
| 73 | CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY ); |
| 74 | if( !capture ) { |
| 75 | fprintf( stderr, "ERROR: capture is NULL \n" ); |
| 76 | getchar(); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | // Create a window in which the captured images will be presented |
| 81 | cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); |
| 82 | |
| 83 | // Show the image captured from the camera in the window and repeat |
| 84 | while( 1 ) { |
| 85 | // Get one frame |
| 86 | IplImage* frame = cvQueryFrame(capture); |
| 87 | IplImage* flipped = cvCreateImage(cvSize(frame->width, frame->height), |
| 88 | frame->depth, frame->nChannels); |
| 89 | if( !frame ) { |
| 90 | fprintf( stderr, "ERROR: frame is null...\n" ); |
| 91 | getchar(); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | cvFlip(frame, flipped, 1); |
| 96 | |
| 97 | detect_and_draw_objects(flipped, cascade, 1); |
| 98 | cvShowImage( "mywindow", flipped); |
| 99 | // Do not release the frame! |
| 100 | |
| 101 | //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), |
| 102 | //remove higher bits using AND operator |
| 103 | if( (cvWaitKey(10) & 255) == 27 ) break; |
| 104 | } |
| 105 | |
| 106 | // Release the capture device housekeeping |
| 107 | cvReleaseCapture( &capture ); |
| 108 | cvDestroyWindow( "mywindow" ); |
| 109 | return 0; |
| 110 | } |