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/>.
41 #include <dbus/dbus.h>
42 #include <dbus/dbus-glib.h>
46 DBusGConnection
*connection
;
53 // argument for cvFlip(src, dest, FLIP_TYPE)
56 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
58 return (CvHaarClassifierCascade
*)cvLoad(cascade_path
);
61 void detect_and_draw_objects(IplImage
* image
,
62 CvHaarClassifierCascade
* cascade
,
65 IplImage
* small_image
= image
;
66 CvMemStorage
* storage
= cvCreateMemStorage(0);
70 /* if the flag is specified, down-scale the input image to get a
71 performance boost w/o loosing quality (perhaps) */
74 small_image
= cvCreateImage(cvSize(image
->width
/2,image
->height
/2),
76 cvPyrDown(image
, small_image
, CV_GAUSSIAN_5x5
);
80 /* use the fastest variant */
81 faces
= cvHaarDetectObjects(small_image
, cascade
, storage
, 1.2, 2,
82 CV_HAAR_DO_CANNY_PRUNING
);
86 dbus_g_proxy_call_no_reply(proxy
, "SimulateUserActivity", G_TYPE_INVALID
);
89 /* draw all the rectangles */
90 for(i
= 0; i
< faces
->total
; i
++)
92 /* extract the rectangles only */
93 CvRect face
= *(CvRect
*)cvGetSeqElem(faces
, i
);
94 CvPoint upperLeft
= cvPoint(face
.x
*scale
,face
.y
*scale
);
95 CvPoint bottomRight
= cvPoint((face
.x
+face
.width
)*scale
,
96 (face
.y
+face
.height
)*scale
);
97 cvRectangle(image
, upperLeft
, bottomRight
, CV_RGB(255,0,0), 3);
100 if(small_image
!= image
)
101 cvReleaseImage(&small_image
);
102 cvReleaseMemStorage(&storage
);
105 // A Simple Camera Capture Framework.
106 int main(int argc
, char** argv
)
111 connection
= dbus_g_bus_get (DBUS_BUS_SESSION
,
113 if (connection
== NULL
)
115 g_printerr ("Failed to open connection to bus: %s\n",
117 g_error_free (error
);
121 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
123 proxy
= dbus_g_proxy_new_for_name (connection
,
124 "org.gnome.ScreenSaver",
125 "/org/gnome/ScreenSaver",
126 "org.gnome.ScreenSaver");
128 CvHaarClassifierCascade
* cascade
= load_object_detector(argv
[1]);
130 CvCapture
* capture
= cvCaptureFromCAM(CV_CAP_ANY
);
132 cerr
<< "ERROR: capture is NULL " << endl
;
137 // Create a window in which the captured images will be presented
138 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE
);
140 IplImage
* frame
= cvQueryFrame(capture
);
142 cerr
<< "ERROR: frame is null..." << endl
;
147 cvCreateImage(cvGetSize(frame
), frame
->depth
, frame
->nChannels
);
148 // Show the image captured from the camera in the window and repeat
151 frame
= cvQueryFrame(capture
);
153 cerr
<< "ERROR: frame is null..." << endl
;
158 //flip the image so displayed right/left corresponds to physical right/left
159 cvFlip(frame
, mirrored
, MIRROR
);
161 detect_and_draw_objects(mirrored
, cascade
, 1);
162 cvShowImage("mywindow", mirrored
);
163 // Do not release the frame!
165 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
166 //remove higher bits using AND operator
167 if((cvWaitKey(10) & 255) == 27) break;
170 // Release the capture device housekeeping
171 cvReleaseCapture(&capture
);
172 cvDestroyWindow("mywindow");
173 cvReleaseImage(&mirrored
);