045c11442a759cdf7322363af28cb3c58111a520
[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 <iostream>
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib.h>
26
27 using namespace std;
28
29 DBusGConnection *connection;
30 GError *error;
31 DBusGProxy *proxy;
32 char **name_list;
33 char **name_list_ptr;
34
35
36 // argument for cvFlip(src, dest, FLIP_TYPE)
37 #define MIRROR 1
38
39 CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
40 {
41 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
42 }
43
44 void detect_and_draw_objects(IplImage* image,
45 CvHaarClassifierCascade* cascade,
46 int do_pyramids)
47 {
48 IplImage* small_image = image;
49 CvMemStorage* storage = cvCreateMemStorage(0);
50 CvSeq* faces;
51 int i, scale = 1;
52
53 /* if the flag is specified, down-scale the input image to get a
54 performance boost w/o loosing quality (perhaps) */
55 if(do_pyramids)
56 {
57 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
58 IPL_DEPTH_8U, 3);
59 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
60 scale = 2;
61 }
62
63 /* use the fastest variant */
64 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
65 CV_HAAR_DO_CANNY_PRUNING);
66
67 if(0 < faces->total)
68 {
69 dbus_g_proxy_call_no_reply(proxy, "SimulateUserActivity", G_TYPE_INVALID);
70 }
71
72 /* draw all the rectangles */
73 for(i = 0; i < faces->total; i++)
74 {
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);
81 }
82
83 if(small_image != image)
84 cvReleaseImage(&small_image);
85 cvReleaseMemStorage(&storage);
86 }
87
88 // A Simple Camera Capture Framework.
89 int main(int argc, char** argv)
90 {
91 g_type_init ();
92
93 error = NULL;
94 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
95 &error);
96 if (connection == NULL)
97 {
98 g_printerr ("Failed to open connection to bus: %s\n",
99 error->message);
100 g_error_free (error);
101 exit (1);
102 }
103
104 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
105
106 proxy = dbus_g_proxy_new_for_name (connection,
107 "org.gnome.ScreenSaver",
108 "/org/gnome/ScreenSaver",
109 "org.gnome.ScreenSaver");
110
111 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
112
113 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
114 if(!capture) {
115 cerr << "ERROR: capture is NULL " << endl;
116 getchar();
117 return -1;
118 }
119
120 // Create a window in which the captured images will be presented
121 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
122
123 IplImage* frame = cvQueryFrame(capture);
124 if(!frame) {
125 cerr << "ERROR: frame is null..." << endl;
126 getchar();
127 return -1;
128 }
129 IplImage* mirrored =
130 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
131 // Show the image captured from the camera in the window and repeat
132 while(1) {
133 // Get one frame
134 frame = cvQueryFrame(capture);
135 if(!frame) {
136 cerr << "ERROR: frame is null..." << endl;
137 getchar();
138 break;
139 }
140
141 //flip the image so displayed right/left corresponds to physical right/left
142 cvFlip(frame, mirrored, MIRROR);
143
144 detect_and_draw_objects(mirrored, cascade, 1);
145 cvShowImage("mywindow", mirrored);
146 // Do not release the frame!
147
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;
151 }
152
153 // Release the capture device housekeeping
154 cvReleaseCapture(&capture);
155 cvDestroyWindow("mywindow");
156 cvReleaseImage(&mirrored);
157 return 0;
158 }