5de3abe301275852c999d03c00c15458c108a1a6
[ozzloy@gmail.com/oble] / oble.cpp
1 //use the camera to aid the decision to sleep.
2 //Copyright 2009 Daniel Watson
3 /*
4 use a camera to prevent screensaver
5 Copyright (C) 2009 daniel watson, ozzloy@gmail.com
6
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.
11
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.
16
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/>.
19 */
20 /*
21 This file is part of oble.
22
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.
27
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.
32
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/>.
35
36 */
37 #include <cv.h>
38 #include <highgui.h>
39 #include <stdio.h>
40 #include <iostream>
41 #include <time.h>
42 #include <unistd.h>
43 #include "oble.h"
44 #include "idle_x11.h"
45
46 using namespace std;
47
48 static string cascade_filename = "";
49 static int verbose = 0;
50
51 // argument for cvFlip(src, dest, FLIP_TYPE)
52 #define MIRROR 1
53
54 CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
55 {
56 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
57 }
58
59 void detect_and_draw_objects(IplImage* image,
60 CvHaarClassifierCascade* cascade,
61 int do_pyramids)
62 {
63 IplImage* small_image = image;
64 CvMemStorage* storage = cvCreateMemStorage(0);
65 CvSeq* faces;
66 int i, scale = 1;
67 static int saw_last_time = 0;
68
69 /* if the flag is specified, down-scale the input image to get a
70 performance boost w/o loosing quality (perhaps) */
71 if(do_pyramids)
72 {
73 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
74 IPL_DEPTH_8U, 3);
75 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
76 scale = 2;
77 }
78
79 /* use the fastest variant */
80 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
81 CV_HAAR_DO_CANNY_PRUNING);
82
83 if(0 < faces->total)
84 {
85 t_current = time(NULL);
86 if(verbose)
87 {
88 printf(":) face \n");
89 }
90 if(saw_last_time)
91 {
92 if(verbose)printf("\t\tpoking\n");
93 reset_idle_time();
94 system("gnome-screensaver-command --poke");
95 }
96 saw_last_time = 1;
97 }
98 else
99 {
100 saw_last_time = 0;
101 if(verbose)
102 {
103 printf(":( no face \n");
104 }
105 }
106
107 /* draw all the rectangles */
108 for(i = 0; i < faces->total; i++)
109 {
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);
116 }
117
118 if(small_image != image)
119 cvReleaseImage(&small_image);
120 cvReleaseMemStorage(&storage);
121 }
122
123 void screensave(time_t t_current)
124 {
125 static int last_elapse = 0;
126 static int activated = 0;
127 int elapse = difftime(time(NULL), t_current);
128 int timeout = 10;
129 if(elapse > timeout && elapse != last_elapse)
130 {
131 last_elapse = elapse;
132 if(verbose)
133 {
134 printf("elapse = %d\n", elapse);
135 }
136 if(!activated)
137 {
138 printf("activated\n");
139 activated = 1;
140 system("gnome-screensaver-command -a");
141 }
142 }
143 if(elapse < timeout && activated)
144 {
145 printf("deactivated\n");
146 activated = 0;
147 system("gnome-screensaver-command -d");
148 }
149 }
150
151 int parse_opts(int argc, char **argv)
152 {
153 int index, c;
154 opterr = 0;
155 const char *options = "vc:\x0";
156 while((c = getopt(argc, argv, options)) != -1)
157 {
158 switch(c)
159 {
160 case 'v': verbose = 1; break;
161 case 'c': cascade_filename = string(optarg); break;
162 case '?':
163 if(optopt == 'c')
164 {
165 printf("option -%c requires an argument.\n", optopt);
166 }
167 else if(isprint (optopt))
168 {
169 printf("unknown option `-%c'.\n", optopt);
170 }
171 else
172 {
173 printf("unknown option char `\\x%x'.\n", optopt);
174 }
175 break;
176 case '\x0': break; //ignore. not sure why this shows up. maybe zsh?
177 default: abort();
178 }
179 }
180
181 for(index = optind; index < argc; index++)
182 printf("Non-option arg %s\n", argv[index]);
183
184 if(cascade_filename == "")
185 {
186 printf("you must supply a filename with -c option. example:\n");
187 printf(
188 "%s -c %s\n",
189 argv[0],
190 "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"
191 );
192 return 1;
193 }
194 if(verbose) printf("SILENT ALARM ACTIVATED!!!\n");
195 return 0;
196 }
197
198 // A Simple Camera Capture Framework.
199 int main(int argc, char** argv)
200 {
201 if(parse_opts(argc, argv)) return 0;
202
203 CvHaarClassifierCascade* cascade =
204 load_object_detector(cascade_filename.c_str());
205
206 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
207 if(!capture) {
208 cerr << "ERROR: capture is NULL " << endl;
209 getchar();
210 return -1;
211 }
212
213 // Create a window in which the captured images will be presented
214 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
215
216 IplImage* frame = cvQueryFrame(capture);
217 if(!frame) {
218 cerr << "ERROR: frame is null..." << endl;
219 getchar();
220 return -1;
221 }
222 IplImage* mirrored =
223 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
224 t_current = time(NULL);
225 // Show the image captured from the camera in the window and repeat
226 while(1) {
227 // Get one frame
228 frame = cvQueryFrame(capture);
229 if(!frame) {
230 cerr << "ERROR: frame is null..." << endl;
231 getchar();
232 break;
233 }
234
235 //flip the image so displayed right/left corresponds to physical right/left
236 cvFlip(frame, mirrored, MIRROR);
237
238 detect_and_draw_objects(mirrored, cascade, 1);
239 cvShowImage("mywindow", mirrored);
240 // Do not release the frame!
241 //screensave(t_current);
242
243 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
244 //remove higher bits using AND operator
245 if((cvWaitKey(100) & 255) == 27) break;
246 }
247
248 // Release the capture device housekeeping
249 cvReleaseCapture(&capture);
250 cvDestroyWindow("mywindow");
251 cvReleaseImage(&mirrored);
252 return 0;
253 }