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