13a10215f6c485c9a80016348c8713a52e325f13
1 //use the camera to aid the decision to sleep.
2 //Copyright 2009 Daniel Watson
4 This file is part of oble.
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.
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.
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/>.
31 static string cascade_filename
= "";
32 static int verbose
= 0;
34 // argument for cvFlip(src, dest, FLIP_TYPE)
37 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
39 return (CvHaarClassifierCascade
*)cvLoad(cascade_path
);
42 void detect_and_draw_objects(IplImage
* image
,
43 CvHaarClassifierCascade
* cascade
,
46 IplImage
* small_image
= image
;
47 CvMemStorage
* storage
= cvCreateMemStorage(0);
51 /* if the flag is specified, down-scale the input image to get a
52 performance boost w/o loosing quality (perhaps) */
55 small_image
= cvCreateImage(cvSize(image
->width
/2,
58 cvPyrDown(image
, small_image
, CV_GAUSSIAN_5x5
);
62 /* use the fastest variant */
63 faces
= cvHaarDetectObjects(small_image
, cascade
, storage
, 1.2, 2,
64 CV_HAAR_DO_CANNY_PRUNING
);
66 update_idle(faces
->total
);
68 /* draw all the rectangles */
69 for(i
= 0; i
< faces
->total
; i
++)
71 /* extract the rectangles only */
72 CvRect face
= *(CvRect
*)cvGetSeqElem(faces
, i
);
73 CvPoint upperLeft
= cvPoint(face
.x
* scale
, face
.y
* scale
);
74 CvPoint bottomRight
= cvPoint((face
.x
+ face
.width
) * scale
,
75 (face
.y
+ face
.height
) * scale
);
76 cvRectangle(image
, upperLeft
, bottomRight
, CV_RGB(255,0,0), 3);
79 if(small_image
!= image
)
80 cvReleaseImage(&small_image
);
81 cvReleaseMemStorage(&storage
);
84 void update_idle(int faces_total
)
86 static int saw_last_time
= 0;
90 t_current
= time(NULL
);
97 if(verbose
)printf("\t\tpoking\n");
99 system("gnome-screensaver-command --poke");
108 printf(":( no face \n");
114 void screensave(time_t t_current
)
116 static int last_elapse
= 0;
117 static int activated
= 0;
118 int elapse
= difftime(time(NULL
), t_current
);
120 if(elapse
> timeout
&& elapse
!= last_elapse
)
122 last_elapse
= elapse
;
125 printf("elapse = %d\n", elapse
);
129 printf("activated\n");
131 system("gnome-screensaver-command -a");
134 if(elapse
< timeout
&& activated
)
136 printf("deactivated\n");
138 system("gnome-screensaver-command -d");
142 int parse_opts(int argc
, char **argv
)
146 const char *options
= "vc:\x0";
147 while((c
= getopt(argc
, argv
, options
)) != -1)
151 case 'v': verbose
= 1; break;
152 case 'c': cascade_filename
= string(optarg
); break;
156 printf("option -%c requires an argument.\n", optopt
);
158 else if(isprint (optopt
))
160 printf("unknown option `-%c'.\n", optopt
);
164 printf("unknown option char `\\x%x'.\n", optopt
);
167 case '\x0': break; //ignore. not sure why this shows up. maybe zsh?
172 for(index
= optind
; index
< argc
; index
++)
173 printf("Non-option arg %s\n", argv
[index
]);
175 if(cascade_filename
== "")
177 printf("you must supply a filename with -c option. example:\n");
181 "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"
185 if(verbose
) printf("SILENT ALARM ACTIVATED!!!\n");
189 void get_frames(CvCapture
* capture
, CvHaarClassifierCascade
* cascade
)
191 IplImage
* frame
= NULL
;
192 get_one_frame(capture
, frame
);
195 cvCreateImage(cvGetSize(frame
), frame
->depth
, frame
->nChannels
);
196 // Show the image captured from the camera in the window and repeat
198 if(get_one_frame(capture
, frame
)) break;
200 //flip the image so displayed right/left corresponds to physical right/left
201 cvFlip(frame
, mirrored
, MIRROR
);
203 detect_and_draw_objects(mirrored
, cascade
, 1);
204 cvShowImage("mywindow", mirrored
);
205 // Do not release the frame!
206 //screensave(t_current);
208 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
209 //remove higher bits using AND operator
210 if((cvWaitKey(100) & 255) == 27) break;
212 cvReleaseImage(&mirrored
);
217 CvHaarClassifierCascade
* cascade
=
218 load_object_detector(cascade_filename
.c_str());
220 CvCapture
* capture
= cvCaptureFromCAM(CV_CAP_ANY
);
222 cerr
<< "ERROR: capture is NULL " << endl
;
227 // Create a window in which the captured images will be presented
228 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE
);
229 t_current
= time(NULL
);
230 get_frames(capture
, cascade
);
232 // Release the capture device housekeeping
233 cvReleaseCapture(&capture
);
234 cvDestroyWindow("mywindow");
238 int get_one_frame(CvCapture
* capture
, IplImage
* &frame
)
240 frame
= cvQueryFrame(capture
);
242 cerr
<< "ERROR: frame is null..." << endl
;
249 // A Simple Camera Capture Framework.
250 int main(int argc
, char** argv
)
252 if(parse_opts(argc
, argv
)) return 0;