face detection resets idle time and deactivates ss
[ozzloy@gmail.com/oble] / oble.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>
e0c48375 41#include <time.h>
124727d2 42#include "oble.h"
4f219a9f 43#include "idle_x11.h"
b8b01547 44
e0c48375 45using namespace std;
b7e484d5 46
0d6b136c 47// argument for cvFlip(src, dest, FLIP_TYPE)
4d640880 48#define MIRROR 1
0d6b136c 49
4f219a9f 50#define DEBUG 0
51
64fb0216 52CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
b7e484d5 53{
21323a92 54 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
b7e484d5 55}
56
9cd4e78c 57void detect_and_draw_objects(IplImage* image,
b7e484d5 58 CvHaarClassifierCascade* cascade,
9cd4e78c 59 int do_pyramids)
b7e484d5 60{
61 IplImage* small_image = image;
62 CvMemStorage* storage = cvCreateMemStorage(0);
63 CvSeq* faces;
64 int i, scale = 1;
4f219a9f 65 static int saw_last_time = 0;
b7e484d5 66
67 /* if the flag is specified, down-scale the input image to get a
68 performance boost w/o loosing quality (perhaps) */
21323a92 69 if(do_pyramids)
b7e484d5 70 {
21323a92 71 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
72 IPL_DEPTH_8U, 3);
73 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
b7e484d5 74 scale = 2;
75 }
76
77 /* use the fastest variant */
21323a92 78 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
79 CV_HAAR_DO_CANNY_PRUNING);
b7e484d5 80
4d640880 81 if(0 < faces->total)
b8b01547 82 {
e0c48375 83 t_current = time(NULL);
4f219a9f 84 if(DEBUG)
85 {
86 printf(":) face \n");
87 }
88 if(saw_last_time)
89 {
90 if(DEBUG)printf("\t\tpoking\n");
91 reset_idle_time();
92 system("gnome-screensaver-command --poke");
93 }
94 saw_last_time = 1;
95 }
96 else
97 {
98 saw_last_time = 0;
99 if(DEBUG)
100 {
101 printf(":( no face \n");
102 }
b8b01547 103 }
b8b01547 104
b7e484d5 105 /* draw all the rectangles */
21323a92 106 for(i = 0; i < faces->total; i++)
b7e484d5 107 {
108 /* extract the rectangles only */
21323a92 109 CvRect face = *(CvRect*)cvGetSeqElem(faces, i);
79292335 110 CvPoint upperLeft = cvPoint(face.x * scale, face.y * scale);
111 CvPoint bottomRight = cvPoint((face.x + face.width) * scale,
112 (face.y + face.height) * scale);
21323a92 113 cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3);
b7e484d5 114 }
115
21323a92 116 if(small_image != image)
117 cvReleaseImage(&small_image);
118 cvReleaseMemStorage(&storage);
b7e484d5 119}
120
e0c48375 121void screensave(time_t t_current)
b7e484d5 122{
e0c48375 123 static int last_elapse = 0;
124 static int activated = 0;
125 int elapse = difftime(time(NULL), t_current);
126 int timeout = 10;
127 if(elapse > timeout && elapse != last_elapse)
b8b01547 128 {
e0c48375 129 last_elapse = elapse;
4f219a9f 130 if(DEBUG)
131 {
132 printf("elapse = %d\n", elapse);
133 }
e0c48375 134 if(!activated)
135 {
136 printf("activated\n");
137 activated = 1;
138 system("gnome-screensaver-command -a");
139 }
140 }
141 if(elapse < timeout && activated)
142 {
143 printf("deactivated\n");
144 activated = 0;
145 system("gnome-screensaver-command -d");
146 }
147}
b8b01547 148
e0c48375 149// A Simple Camera Capture Framework.
150int main(int argc, char** argv)
151{
b7e484d5 152 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
153
21323a92 154 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
155 if(!capture) {
4d640880 156 cerr << "ERROR: capture is NULL " << endl;
b7e484d5 157 getchar();
158 return -1;
159 }
160
161 // Create a window in which the captured images will be presented
21323a92 162 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
b7e484d5 163
0d6b136c 164 IplImage* frame = cvQueryFrame(capture);
165 if(!frame) {
4d640880 166 cerr << "ERROR: frame is null..." << endl;
0d6b136c 167 getchar();
168 return -1;
169 }
b8b01547 170 IplImage* mirrored =
0d6b136c 171 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
e0c48375 172 t_current = time(NULL);
b7e484d5 173 // Show the image captured from the camera in the window and repeat
21323a92 174 while(1) {
b7e484d5 175 // Get one frame
0d6b136c 176 frame = cvQueryFrame(capture);
21323a92 177 if(!frame) {
4d640880 178 cerr << "ERROR: frame is null..." << endl;
b7e484d5 179 getchar();
180 break;
181 }
182
21323a92 183 //flip the image so displayed right/left corresponds to physical right/left
4d640880 184 cvFlip(frame, mirrored, MIRROR);
64fb0216 185
b8b01547 186 detect_and_draw_objects(mirrored, cascade, 1);
187 cvShowImage("mywindow", mirrored);
b7e484d5 188 // Do not release the frame!
4f219a9f 189 //screensave(t_current);
b7e484d5 190
191 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
192 //remove higher bits using AND operator
e0c48375 193 if((cvWaitKey(100) & 255) == 27) break;
b7e484d5 194 }
195
196 // Release the capture device housekeeping
21323a92 197 cvReleaseCapture(&capture);
198 cvDestroyWindow("mywindow");
b8b01547 199 cvReleaseImage(&mirrored);
b7e484d5 200 return 0;
201}