12c666e1090ef8904d23378f4da7169805f87dcb
[ozzloy@gmail.com/oble] / hello-world.cpp
1 //use the camera to aid the decision to sleep.
2 //Copyright 2009 Daniel Watson
3 /*
4 use a camera to prevent screensaver
5 Copyright (C) 2009 daniel watson, ozzloy@gmail.com
6
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.
11
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.
16
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/>.
19 */
20 /*
21 This file is part of oble.
22
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.
27
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.
32
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/>.
35
36 */
37 #include <cv.h>
38 #include <highgui.h>
39 #include <stdio.h>
40 #include <iostream>
41 //#include <dbus/dbus.h>
42 //#include <dbus/dbus-glib.h>
43
44 using namespace std;
45
46 //DBusGConnection *connection;
47 //GError *error;
48 //DBusGProxy *proxy;
49 //char **name_list;
50 //char **name_list_ptr;
51
52
53 // argument for cvFlip(src, dest, FLIP_TYPE)
54 #define MIRROR 1
55
56 CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
57 {
58 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
59 }
60
61 void detect_and_draw_objects(IplImage* image,
62 CvHaarClassifierCascade* cascade,
63 int do_pyramids)
64 {
65 IplImage* small_image = image;
66 CvMemStorage* storage = cvCreateMemStorage(0);
67 CvSeq* faces;
68 int i, scale = 1;
69
70 /* if the flag is specified, down-scale the input image to get a
71 performance boost w/o loosing quality (perhaps) */
72 if(do_pyramids)
73 {
74 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
75 IPL_DEPTH_8U, 3);
76 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
77 scale = 2;
78 }
79
80 /* use the fastest variant */
81 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
82 CV_HAAR_DO_CANNY_PRUNING);
83
84 /*
85 if(0 < faces->total)
86 {
87 dbus_g_proxy_call_no_reply(proxy, "SimulateUserActivity", G_TYPE_INVALID);
88 }
89 */
90
91 /* draw all the rectangles */
92 for(i = 0; i < faces->total; i++)
93 {
94 /* extract the rectangles only */
95 CvRect face = *(CvRect*)cvGetSeqElem(faces, i);
96 CvPoint upperLeft = cvPoint(face.x * scale, face.y * scale);
97 CvPoint bottomRight = cvPoint((face.x + face.width) * scale,
98 (face.y + face.height) * scale);
99 cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3);
100 }
101
102 if(small_image != image)
103 cvReleaseImage(&small_image);
104 cvReleaseMemStorage(&storage);
105 }
106
107 // A Simple Camera Capture Framework.
108 int main(int argc, char** argv)
109 {
110 // g_type_init ();
111
112 //error = NULL;
113 //connection = dbus_g_bus_get (DBUS_BUS_SESSION,
114 // &error);
115 /*if (connection == NULL)
116 {
117 g_printerr ("Failed to open connection to bus: %s\n",
118 error->message);
119 g_error_free (error);
120 exit (1);
121 }*/
122
123 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
124
125 /*
126 proxy = dbus_g_proxy_new_for_name (connection,
127 "org.gnome.ScreenSaver",
128 "/org/gnome/ScreenSaver",
129 "org.gnome.ScreenSaver");
130 */
131
132 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
133
134 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
135 if(!capture) {
136 cerr << "ERROR: capture is NULL " << endl;
137 getchar();
138 return -1;
139 }
140
141 // Create a window in which the captured images will be presented
142 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
143
144 IplImage* frame = cvQueryFrame(capture);
145 if(!frame) {
146 cerr << "ERROR: frame is null..." << endl;
147 getchar();
148 return -1;
149 }
150 IplImage* mirrored =
151 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
152 // Show the image captured from the camera in the window and repeat
153 while(1) {
154 // Get one frame
155 frame = cvQueryFrame(capture);
156 if(!frame) {
157 cerr << "ERROR: frame is null..." << endl;
158 getchar();
159 break;
160 }
161
162 //flip the image so displayed right/left corresponds to physical right/left
163 cvFlip(frame, mirrored, MIRROR);
164
165 detect_and_draw_objects(mirrored, cascade, 1);
166 cvShowImage("mywindow", mirrored);
167 // Do not release the frame!
168
169 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
170 //remove higher bits using AND operator
171 if((cvWaitKey(10) & 255) == 27) break;
172 }
173
174 // Release the capture device housekeeping
175 cvReleaseCapture(&capture);
176 cvDestroyWindow("mywindow");
177 cvReleaseImage(&mirrored);
178 return 0;
179 }