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