a92408ab |
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 | */ |
b7e484d5 |
20 | #include <cv.h> |
21 | #include <highgui.h> |
22 | #include <stdio.h> |
23 | |
64fb0216 |
24 | CvHaarClassifierCascade* load_object_detector(const char* cascade_path) |
b7e484d5 |
25 | { |
21323a92 |
26 | return (CvHaarClassifierCascade*)cvLoad(cascade_path); |
b7e484d5 |
27 | } |
28 | |
9cd4e78c |
29 | void detect_and_draw_objects(IplImage* image, |
b7e484d5 |
30 | CvHaarClassifierCascade* cascade, |
9cd4e78c |
31 | int do_pyramids) |
b7e484d5 |
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) */ |
21323a92 |
40 | if(do_pyramids) |
b7e484d5 |
41 | { |
21323a92 |
42 | small_image = cvCreateImage(cvSize(image->width/2,image->height/2), |
43 | IPL_DEPTH_8U, 3); |
44 | cvPyrDown(image, small_image, CV_GAUSSIAN_5x5); |
b7e484d5 |
45 | scale = 2; |
46 | } |
47 | |
48 | /* use the fastest variant */ |
21323a92 |
49 | faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2, |
50 | CV_HAAR_DO_CANNY_PRUNING); |
b7e484d5 |
51 | |
52 | /* draw all the rectangles */ |
21323a92 |
53 | for(i = 0; i < faces->total; i++) |
b7e484d5 |
54 | { |
55 | /* extract the rectangles only */ |
21323a92 |
56 | CvRect face = *(CvRect*)cvGetSeqElem(faces, i); |
57 | CvPoint upperLeft = cvPoint(face.x*scale,face.y*scale); |
58 | CvPoint bottomRight = cvPoint((face.x+face.width)*scale, |
59 | (face.y+face.height)*scale); |
60 | cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3); |
b7e484d5 |
61 | } |
62 | |
21323a92 |
63 | if(small_image != image) |
64 | cvReleaseImage(&small_image); |
65 | cvReleaseMemStorage(&storage); |
b7e484d5 |
66 | } |
67 | |
96972357 |
68 | // A Simple Camera Capture Framework. |
21323a92 |
69 | int main(int argc, char** argv) |
b7e484d5 |
70 | { |
71 | CvHaarClassifierCascade* cascade = load_object_detector(argv[1]); |
72 | |
21323a92 |
73 | CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); |
74 | if(!capture) { |
75 | fprintf(stderr, "ERROR: capture is NULL \n"); |
b7e484d5 |
76 | getchar(); |
77 | return -1; |
78 | } |
79 | |
80 | // Create a window in which the captured images will be presented |
21323a92 |
81 | cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); |
b7e484d5 |
82 | |
83 | // Show the image captured from the camera in the window and repeat |
21323a92 |
84 | while(1) { |
b7e484d5 |
85 | // Get one frame |
64fb0216 |
86 | IplImage* frame = cvQueryFrame(capture); |
21323a92 |
87 | if(!frame) { |
88 | fprintf(stderr, "ERROR: frame is null...\n"); |
b7e484d5 |
89 | getchar(); |
90 | break; |
91 | } |
92 | |
21323a92 |
93 | //flip the image so displayed right/left corresponds to physical right/left |
94 | cvFlip(frame, frame, 1); |
64fb0216 |
95 | |
21323a92 |
96 | detect_and_draw_objects(frame, cascade, 1); |
97 | cvShowImage("mywindow", frame); |
b7e484d5 |
98 | // Do not release the frame! |
99 | |
100 | //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), |
101 | //remove higher bits using AND operator |
21323a92 |
102 | if((cvWaitKey(10) & 255) == 27) break; |
b7e484d5 |
103 | } |
104 | |
105 | // Release the capture device housekeeping |
21323a92 |
106 | cvReleaseCapture(&capture); |
107 | cvDestroyWindow("mywindow"); |
b7e484d5 |
108 | return 0; |
109 | } |