1 //use the camera to aid the decision to sleep.
2 //Copyright 2009 Daniel Watson
4 use a camera to prevent screensaver
5 Copyright (C) 2009 daniel watson, ozzloy@gmail.com
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 This file is part of oble.
23 oble is free software: you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation, either version 3 of the License, or
26 (at your option) any later version.
28 oble is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with oble. If not, see <http://www.gnu.org/licenses/>.
48 static string cascade_filename
= "";
49 static int verbose
= 0;
51 // argument for cvFlip(src, dest, FLIP_TYPE)
54 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
56 return (CvHaarClassifierCascade
*)cvLoad(cascade_path
);
59 void detect_and_draw_objects(IplImage
* image
,
60 CvHaarClassifierCascade
* cascade
,
63 IplImage
* small_image
= image
;
64 CvMemStorage
* storage
= cvCreateMemStorage(0);
67 static int saw_last_time
= 0;
69 /* if the flag is specified, down-scale the input image to get a
70 performance boost w/o loosing quality (perhaps) */
73 small_image
= cvCreateImage(cvSize(image
->width
/2,image
->height
/2),
75 cvPyrDown(image
, small_image
, CV_GAUSSIAN_5x5
);
79 /* use the fastest variant */
80 faces
= cvHaarDetectObjects(small_image
, cascade
, storage
, 1.2, 2,
81 CV_HAAR_DO_CANNY_PRUNING
);
85 t_current
= time(NULL
);
92 if(verbose
)printf("\t\tpoking\n");
94 system("gnome-screensaver-command --poke");
103 printf(":( no face \n");
107 /* draw all the rectangles */
108 for(i
= 0; i
< faces
->total
; i
++)
110 /* extract the rectangles only */
111 CvRect face
= *(CvRect
*)cvGetSeqElem(faces
, i
);
112 CvPoint upperLeft
= cvPoint(face
.x
* scale
, face
.y
* scale
);
113 CvPoint bottomRight
= cvPoint((face
.x
+ face
.width
) * scale
,
114 (face
.y
+ face
.height
) * scale
);
115 cvRectangle(image
, upperLeft
, bottomRight
, CV_RGB(255,0,0), 3);
118 if(small_image
!= image
)
119 cvReleaseImage(&small_image
);
120 cvReleaseMemStorage(&storage
);
123 void screensave(time_t t_current
)
125 static int last_elapse
= 0;
126 static int activated
= 0;
127 int elapse
= difftime(time(NULL
), t_current
);
129 if(elapse
> timeout
&& elapse
!= last_elapse
)
131 last_elapse
= elapse
;
134 printf("elapse = %d\n", elapse
);
138 printf("activated\n");
140 system("gnome-screensaver-command -a");
143 if(elapse
< timeout
&& activated
)
145 printf("deactivated\n");
147 system("gnome-screensaver-command -d");
151 int parse_opts(int argc
, char **argv
)
155 const char *options
= "vc:";
156 while((c
= getopt(argc
, argv
, options
)) != -1)
160 case 'v': verbose
= 1; break;
161 case 'c': cascade_filename
= string(optarg
); break;
165 printf("option -%c requires an argument.\n", optopt
);
167 else if(isprint (optopt
))
169 printf("unknown option `-%c'.\n", optopt
);
173 printf("unknown option char `\\x%x'.\n", optopt
);
180 for(index
= optind
; index
< argc
; index
++)
181 printf("Non-option arg %s\n", argv
[index
]);
183 if(cascade_filename
== "")
185 printf("you must supply a filename with -c option. example:\n");
189 "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"
193 if(verbose
) printf("SILENT ALARM ACTIVATED!!!\n");
197 // A Simple Camera Capture Framework.
198 int main(int argc
, char** argv
)
200 if(parse_opts(argc
, argv
)) return 0;
202 CvHaarClassifierCascade
* cascade
=
203 load_object_detector(cascade_filename
.c_str());
205 CvCapture
* capture
= cvCaptureFromCAM(CV_CAP_ANY
);
207 cerr
<< "ERROR: capture is NULL " << endl
;
212 // Create a window in which the captured images will be presented
213 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE
);
215 IplImage
* frame
= cvQueryFrame(capture
);
217 cerr
<< "ERROR: frame is null..." << endl
;
222 cvCreateImage(cvGetSize(frame
), frame
->depth
, frame
->nChannels
);
223 t_current
= time(NULL
);
224 // Show the image captured from the camera in the window and repeat
227 frame
= cvQueryFrame(capture
);
229 cerr
<< "ERROR: frame is null..." << endl
;
234 //flip the image so displayed right/left corresponds to physical right/left
235 cvFlip(frame
, mirrored
, MIRROR
);
237 detect_and_draw_objects(mirrored
, cascade
, 1);
238 cvShowImage("mywindow", mirrored
);
239 // Do not release the frame!
240 //screensave(t_current);
242 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
243 //remove higher bits using AND operator
244 if((cvWaitKey(100) & 255) == 27) break;
247 // Release the capture device housekeeping
248 cvReleaseCapture(&capture
);
249 cvDestroyWindow("mywindow");
250 cvReleaseImage(&mirrored
);