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