045c11442a759cdf7322363af28cb3c58111a520
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/>.
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib.h>
29 DBusGConnection
*connection
;
36 // argument for cvFlip(src, dest, FLIP_TYPE)
39 CvHaarClassifierCascade
* load_object_detector(const char* cascade_path
)
41 return (CvHaarClassifierCascade
*)cvLoad(cascade_path
);
44 void detect_and_draw_objects(IplImage
* image
,
45 CvHaarClassifierCascade
* cascade
,
48 IplImage
* small_image
= image
;
49 CvMemStorage
* storage
= cvCreateMemStorage(0);
53 /* if the flag is specified, down-scale the input image to get a
54 performance boost w/o loosing quality (perhaps) */
57 small_image
= cvCreateImage(cvSize(image
->width
/2,image
->height
/2),
59 cvPyrDown(image
, small_image
, CV_GAUSSIAN_5x5
);
63 /* use the fastest variant */
64 faces
= cvHaarDetectObjects(small_image
, cascade
, storage
, 1.2, 2,
65 CV_HAAR_DO_CANNY_PRUNING
);
69 dbus_g_proxy_call_no_reply(proxy
, "SimulateUserActivity", G_TYPE_INVALID
);
72 /* draw all the rectangles */
73 for(i
= 0; i
< faces
->total
; i
++)
75 /* extract the rectangles only */
76 CvRect face
= *(CvRect
*)cvGetSeqElem(faces
, i
);
77 CvPoint upperLeft
= cvPoint(face
.x
*scale
,face
.y
*scale
);
78 CvPoint bottomRight
= cvPoint((face
.x
+face
.width
)*scale
,
79 (face
.y
+face
.height
)*scale
);
80 cvRectangle(image
, upperLeft
, bottomRight
, CV_RGB(255,0,0), 3);
83 if(small_image
!= image
)
84 cvReleaseImage(&small_image
);
85 cvReleaseMemStorage(&storage
);
88 // A Simple Camera Capture Framework.
89 int main(int argc
, char** argv
)
94 connection
= dbus_g_bus_get (DBUS_BUS_SESSION
,
96 if (connection
== NULL
)
98 g_printerr ("Failed to open connection to bus: %s\n",
100 g_error_free (error
);
104 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
106 proxy
= dbus_g_proxy_new_for_name (connection
,
107 "org.gnome.ScreenSaver",
108 "/org/gnome/ScreenSaver",
109 "org.gnome.ScreenSaver");
111 CvHaarClassifierCascade
* cascade
= load_object_detector(argv
[1]);
113 CvCapture
* capture
= cvCaptureFromCAM(CV_CAP_ANY
);
115 cerr
<< "ERROR: capture is NULL " << endl
;
120 // Create a window in which the captured images will be presented
121 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE
);
123 IplImage
* frame
= cvQueryFrame(capture
);
125 cerr
<< "ERROR: frame is null..." << endl
;
130 cvCreateImage(cvGetSize(frame
), frame
->depth
, frame
->nChannels
);
131 // Show the image captured from the camera in the window and repeat
134 frame
= cvQueryFrame(capture
);
136 cerr
<< "ERROR: frame is null..." << endl
;
141 //flip the image so displayed right/left corresponds to physical right/left
142 cvFlip(frame
, mirrored
, MIRROR
);
144 detect_and_draw_objects(mirrored
, cascade
, 1);
145 cvShowImage("mywindow", mirrored
);
146 // Do not release the frame!
148 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
149 //remove higher bits using AND operator
150 if((cvWaitKey(10) & 255) == 27) break;
153 // Release the capture device housekeeping
154 cvReleaseCapture(&capture
);
155 cvDestroyWindow("mywindow");
156 cvReleaseImage(&mirrored
);