2fd2156d209812545257a9ae4d506899f9deb185
[ozzloy@gmail.com/oble] / hello-world.cpp
1 //use the camera to aid the decision to sleep.
2 //Copyright 2009 Daniel Watson
3 /*
4 This file is part of oble.
5
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.
10
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.
15
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/>.
18
19 */
20 #include <cv.h>
21 #include <highgui.h>
22 #include <stdio.h>
23 #include <dbus/dbus.h>
24 #include <dbus/dbus-glib.h>
25
26 DBusGConnection *connection;
27 GError *error;
28 DBusGProxy *proxy;
29 char **name_list;
30 char **name_list_ptr;
31
32
33 // argument for cvFlip(src, dest, FLIP_TYPE)
34 #define VERTICAL_FLIP 1
35
36 CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
37 {
38 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
39 }
40
41 void detect_and_draw_objects(IplImage* image,
42 CvHaarClassifierCascade* cascade,
43 int do_pyramids)
44 {
45 IplImage* small_image = image;
46 CvMemStorage* storage = cvCreateMemStorage(0);
47 CvSeq* faces;
48 int i, scale = 1;
49
50 /* if the flag is specified, down-scale the input image to get a
51 performance boost w/o loosing quality (perhaps) */
52 if(do_pyramids)
53 {
54 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
55 IPL_DEPTH_8U, 3);
56 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
57 scale = 2;
58 }
59
60 /* use the fastest variant */
61 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
62 CV_HAAR_DO_CANNY_PRUNING);
63
64 #if 0
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))
68 {
69 fprintf(stderr, "bananas");
70
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),
76 error->message);
77 else
78 g_printerr ("Error: %s\n", error->message);
79 g_error_free (error);
80 exit (1);
81 }
82 else
83 {
84 fprintf(stderr, "peaches");
85 }
86 #endif
87
88 /* draw all the rectangles */
89 for(i = 0; i < faces->total; i++)
90 {
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);
97 }
98
99 if(small_image != image)
100 cvReleaseImage(&small_image);
101 cvReleaseMemStorage(&storage);
102 }
103
104 // A Simple Camera Capture Framework.
105 int main(int argc, char** argv)
106 {
107 g_type_init ();
108
109 error = NULL;
110 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
111 &error);
112 if (connection == NULL)
113 {
114 g_printerr ("Failed to open connection to bus: %s\n",
115 error->message);
116 g_error_free (error);
117 exit (1);
118 }
119
120 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
121
122 proxy = dbus_g_proxy_new_for_name (connection,
123 "org.gnome.ScreenSaver",
124 "/org/gnome/ScreenSaver",
125 "org.gnome.ScreenSaver");
126
127 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
128
129 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
130 if(!capture) {
131 fprintf(stderr, "ERROR: capture is NULL \n");
132 getchar();
133 return -1;
134 }
135
136 // Create a window in which the captured images will be presented
137 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
138
139 IplImage* frame = cvQueryFrame(capture);
140 if(!frame) {
141 fprintf(stderr, "ERROR: frame is null...\n");
142 getchar();
143 return -1;
144 }
145 IplImage* mirrored =
146 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
147 // Show the image captured from the camera in the window and repeat
148 while(1) {
149 // Get one frame
150 frame = cvQueryFrame(capture);
151 if(!frame) {
152 fprintf(stderr, "ERROR: frame is null...\n");
153 getchar();
154 break;
155 }
156
157 //flip the image so displayed right/left corresponds to physical right/left
158 cvFlip(frame, mirrored, VERTICAL_FLIP);
159
160 detect_and_draw_objects(mirrored, cascade, 1);
161 cvShowImage("mywindow", mirrored);
162 // Do not release the frame!
163
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;
167 }
168
169 // Release the capture device housekeeping
170 cvReleaseCapture(&capture);
171 cvDestroyWindow("mywindow");
172 cvReleaseImage(&mirrored);
173 return 0;
174 }