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/>.
23 #include <dbus/dbus.h>
24 #include <dbus/dbus-glib.h>
26 DBusGConnection
*connection
;
33 // argument for cvFlip(src, dest, FLIP_TYPE)
34 #define VERTICAL_FLIP 1
36 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
38 return (CvHaarClassifierCascade
*)cvLoad(cascade_path
);
41 void detect_and_draw_objects(IplImage
* image
,
42 CvHaarClassifierCascade
* cascade
,
45 IplImage
* small_image
= image
;
46 CvMemStorage
* storage
= cvCreateMemStorage(0);
50 /* if the flag is specified, down-scale the input image to get a
51 performance boost w/o loosing quality (perhaps) */
54 small_image
= cvCreateImage(cvSize(image
->width
/2,image
->height
/2),
56 cvPyrDown(image
, small_image
, CV_GAUSSIAN_5x5
);
60 /* use the fastest variant */
61 faces
= cvHaarDetectObjects(small_image
, cascade
, storage
, 1.2, 2,
62 CV_HAAR_DO_CANNY_PRUNING
);
65 if ( 0 < faces
->total
&&
66 !dbus_g_proxy_call (proxy
, "SimulateUserActivity", &error
, G_TYPE_INVALID
,
67 G_TYPE_STRV
, &name_list
, G_TYPE_INVALID
))
69 fprintf(stderr
, "bananas");
71 /* Just do demonstrate remote exceptions versus regular GError */
72 if (error
->domain
== DBUS_GERROR
&&
73 error
->code
== DBUS_GERROR_REMOTE_EXCEPTION
)
74 g_printerr ("Caught remote method exception %s: %s",
75 dbus_g_error_get_name (error
),
78 g_printerr ("Error: %s\n", error
->message
);
84 fprintf(stderr
, "peaches");
88 /* draw all the rectangles */
89 for(i
= 0; i
< faces
->total
; i
++)
91 /* extract the rectangles only */
92 CvRect face
= *(CvRect
*)cvGetSeqElem(faces
, i
);
93 CvPoint upperLeft
= cvPoint(face
.x
*scale
,face
.y
*scale
);
94 CvPoint bottomRight
= cvPoint((face
.x
+face
.width
)*scale
,
95 (face
.y
+face
.height
)*scale
);
96 cvRectangle(image
, upperLeft
, bottomRight
, CV_RGB(255,0,0), 3);
99 if(small_image
!= image
)
100 cvReleaseImage(&small_image
);
101 cvReleaseMemStorage(&storage
);
104 // A Simple Camera Capture Framework.
105 int main(int argc
, char** argv
)
110 connection
= dbus_g_bus_get (DBUS_BUS_SESSION
,
112 if (connection
== NULL
)
114 g_printerr ("Failed to open connection to bus: %s\n",
116 g_error_free (error
);
120 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
122 proxy
= dbus_g_proxy_new_for_name (connection
,
123 "org.gnome.ScreenSaver",
124 "/org/gnome/ScreenSaver",
125 "org.gnome.ScreenSaver");
127 CvHaarClassifierCascade
* cascade
= load_object_detector(argv
[1]);
129 CvCapture
* capture
= cvCaptureFromCAM(CV_CAP_ANY
);
131 fprintf(stderr
, "ERROR: capture is NULL \n");
136 // Create a window in which the captured images will be presented
137 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE
);
139 IplImage
* frame
= cvQueryFrame(capture
);
141 fprintf(stderr
, "ERROR: frame is null...\n");
146 cvCreateImage(cvGetSize(frame
), frame
->depth
, frame
->nChannels
);
147 // Show the image captured from the camera in the window and repeat
150 frame
= cvQueryFrame(capture
);
152 fprintf(stderr
, "ERROR: frame is null...\n");
157 //flip the image so displayed right/left corresponds to physical right/left
158 cvFlip(frame
, mirrored
, VERTICAL_FLIP
);
160 detect_and_draw_objects(mirrored
, cascade
, 1);
161 cvShowImage("mywindow", mirrored
);
162 // Do not release the frame!
164 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
165 //remove higher bits using AND operator
166 if((cvWaitKey(10) & 255) == 27) break;
169 // Release the capture device housekeeping
170 cvReleaseCapture(&capture
);
171 cvDestroyWindow("mywindow");
172 cvReleaseImage(&mirrored
);