works, gonna start separating stuff
[ozzloy@gmail.com/oble] / hello-world.cpp
CommitLineData
a92408ab 1//use the camera to aid the decision to sleep.
2//Copyright 2009 Daniel Watson
3/*
4d640880 4 This file is part of oble.
a92408ab 5
4d640880 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.
a92408ab 10
4d640880 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.
a92408ab 15
4d640880 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/>.
a92408ab 18
4d640880 19*/
b7e484d5 20#include <cv.h>
21#include <highgui.h>
22#include <stdio.h>
4d640880 23#include <iostream>
b8b01547 24#include <dbus/dbus.h>
25#include <dbus/dbus-glib.h>
26
4d640880 27using namespace std;
28
b8b01547 29DBusGConnection *connection;
30GError *error;
31DBusGProxy *proxy;
32char **name_list;
33char **name_list_ptr;
34
b7e484d5 35
0d6b136c 36// argument for cvFlip(src, dest, FLIP_TYPE)
4d640880 37#define MIRROR 1
0d6b136c 38
64fb0216 39CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
b7e484d5 40{
21323a92 41 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
b7e484d5 42}
43
9cd4e78c 44void detect_and_draw_objects(IplImage* image,
b7e484d5 45 CvHaarClassifierCascade* cascade,
9cd4e78c 46 int do_pyramids)
b7e484d5 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) */
21323a92 55 if(do_pyramids)
b7e484d5 56 {
21323a92 57 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
58 IPL_DEPTH_8U, 3);
59 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
b7e484d5 60 scale = 2;
61 }
62
63 /* use the fastest variant */
21323a92 64 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
65 CV_HAAR_DO_CANNY_PRUNING);
b7e484d5 66
4d640880 67 if(0 < faces->total)
b8b01547 68 {
4d640880 69 dbus_g_proxy_call_no_reply(proxy, "SimulateUserActivity", G_TYPE_INVALID);
b8b01547 70 }
b8b01547 71
b7e484d5 72 /* draw all the rectangles */
21323a92 73 for(i = 0; i < faces->total; i++)
b7e484d5 74 {
75 /* extract the rectangles only */
21323a92 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);
b7e484d5 81 }
82
21323a92 83 if(small_image != image)
84 cvReleaseImage(&small_image);
85 cvReleaseMemStorage(&storage);
b7e484d5 86}
87
96972357 88// A Simple Camera Capture Framework.
21323a92 89int main(int argc, char** argv)
b7e484d5 90{
b8b01547 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
b7e484d5 111 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
112
21323a92 113 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
114 if(!capture) {
4d640880 115 cerr << "ERROR: capture is NULL " << endl;
b7e484d5 116 getchar();
117 return -1;
118 }
119
120 // Create a window in which the captured images will be presented
21323a92 121 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
b7e484d5 122
0d6b136c 123 IplImage* frame = cvQueryFrame(capture);
124 if(!frame) {
4d640880 125 cerr << "ERROR: frame is null..." << endl;
0d6b136c 126 getchar();
127 return -1;
128 }
b8b01547 129 IplImage* mirrored =
0d6b136c 130 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
b7e484d5 131 // Show the image captured from the camera in the window and repeat
21323a92 132 while(1) {
b7e484d5 133 // Get one frame
0d6b136c 134 frame = cvQueryFrame(capture);
21323a92 135 if(!frame) {
4d640880 136 cerr << "ERROR: frame is null..." << endl;
b7e484d5 137 getchar();
138 break;
139 }
140
21323a92 141 //flip the image so displayed right/left corresponds to physical right/left
4d640880 142 cvFlip(frame, mirrored, MIRROR);
64fb0216 143
b8b01547 144 detect_and_draw_objects(mirrored, cascade, 1);
145 cvShowImage("mywindow", mirrored);
b7e484d5 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
21323a92 150 if((cvWaitKey(10) & 255) == 27) break;
b7e484d5 151 }
152
153 // Release the capture device housekeeping
21323a92 154 cvReleaseCapture(&capture);
155 cvDestroyWindow("mywindow");
b8b01547 156 cvReleaseImage(&mirrored);
b7e484d5 157 return 0;
158}