c710b4ee4aedab91bc30b6d8efd4e694276803d2
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/>.
43 static time_t t_last
= time(NULL
);
44 static time_t t_current
= time(NULL
);
45 static time_t t_tmp
= time(NULL
);
49 // argument for cvFlip(src, dest, FLIP_TYPE)
52 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
54 return (CvHaarClassifierCascade
*)cvLoad(cascade_path
);
57 void detect_and_draw_objects(IplImage
* image
,
58 CvHaarClassifierCascade
* cascade
,
61 IplImage
* small_image
= image
;
62 CvMemStorage
* storage
= cvCreateMemStorage(0);
66 /* if the flag is specified, down-scale the input image to get a
67 performance boost w/o loosing quality (perhaps) */
70 small_image
= cvCreateImage(cvSize(image
->width
/2,image
->height
/2),
72 cvPyrDown(image
, small_image
, CV_GAUSSIAN_5x5
);
76 /* use the fastest variant */
77 faces
= cvHaarDetectObjects(small_image
, cascade
, storage
, 1.2, 2,
78 CV_HAAR_DO_CANNY_PRUNING
);
82 t_current
= time(NULL
);
85 /* draw all the rectangles */
86 for(i
= 0; i
< faces
->total
; i
++)
88 /* extract the rectangles only */
89 CvRect face
= *(CvRect
*)cvGetSeqElem(faces
, i
);
90 CvPoint upperLeft
= cvPoint(face
.x
* scale
, face
.y
* scale
);
91 CvPoint bottomRight
= cvPoint((face
.x
+ face
.width
) * scale
,
92 (face
.y
+ face
.height
) * scale
);
93 cvRectangle(image
, upperLeft
, bottomRight
, CV_RGB(255,0,0), 3);
96 if(small_image
!= image
)
97 cvReleaseImage(&small_image
);
98 cvReleaseMemStorage(&storage
);
101 void screensave(time_t t_current
)
103 static int last_elapse
= 0;
104 static int activated
= 0;
105 int elapse
= difftime(time(NULL
), t_current
);
107 if(elapse
> timeout
&& elapse
!= last_elapse
)
109 last_elapse
= elapse
;
110 printf("elapse = %d\n", elapse
);
113 printf("activated\n");
115 system("gnome-screensaver-command -a");
118 if(elapse
< timeout
&& activated
)
120 printf("deactivated\n");
122 system("gnome-screensaver-command -d");
126 // A Simple Camera Capture Framework.
127 int main(int argc
, char** argv
)
129 CvHaarClassifierCascade
* cascade
= load_object_detector(argv
[1]);
131 CvCapture
* capture
= cvCaptureFromCAM(CV_CAP_ANY
);
133 cerr
<< "ERROR: capture is NULL " << endl
;
138 // Create a window in which the captured images will be presented
139 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE
);
141 IplImage
* frame
= cvQueryFrame(capture
);
143 cerr
<< "ERROR: frame is null..." << endl
;
148 cvCreateImage(cvGetSize(frame
), frame
->depth
, frame
->nChannels
);
149 t_current
= time(NULL
);
150 // Show the image captured from the camera in the window and repeat
153 frame
= cvQueryFrame(capture
);
155 cerr
<< "ERROR: frame is null..." << endl
;
160 //flip the image so displayed right/left corresponds to physical right/left
161 cvFlip(frame
, mirrored
, MIRROR
);
163 detect_and_draw_objects(mirrored
, cascade
, 1);
164 cvShowImage("mywindow", mirrored
);
165 // Do not release the frame!
166 screensave(t_current
);
168 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
169 //remove higher bits using AND operator
170 if((cvWaitKey(100) & 255) == 27) break;
173 // Release the capture device housekeeping
174 cvReleaseCapture(&capture
);
175 cvDestroyWindow("mywindow");
176 cvReleaseImage(&mirrored
);