X-Git-Url: http://challenge-bot.com/repos/?a=blobdiff_plain;f=oble.cpp;h=3f351ae574eb9af94e5ba1cdc37be0d08d1e586f;hb=80a156cdf7a279d65e76c22588283b249af618ce;hp=c710b4ee4aedab91bc30b6d8efd4e694276803d2;hpb=e0c483758ee48b8b1b5dc2d17884bb07c7ddae4e;p=ozzloy%40gmail.com%2Foble diff --git a/oble.cpp b/oble.cpp index c710b4e..3f351ae 100644 --- a/oble.cpp +++ b/oble.cpp @@ -39,13 +39,15 @@ #include #include #include - -static time_t t_last = time(NULL); -static time_t t_current = time(NULL); -static time_t t_tmp = time(NULL); +#include +#include "oble.h" +#include "idle_x11.h" using namespace std; +static string cascade_filename = ""; +static int verbose = 0; + // argument for cvFlip(src, dest, FLIP_TYPE) #define MIRROR 1 @@ -77,10 +79,7 @@ void detect_and_draw_objects(IplImage* image, faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING); - if(0 < faces->total) - { - t_current = time(NULL); - } + update_idle(faces->total); /* draw all the rectangles */ for(i = 0; i < faces->total; i++) @@ -98,6 +97,36 @@ void detect_and_draw_objects(IplImage* image, cvReleaseMemStorage(&storage); } +void update_idle(int faces_total) +{ + static int saw_last_time = 0; + + if(0 < faces_total) + { + t_current = time(NULL); + if(verbose) + { + printf(":) face \n"); + } + if(saw_last_time) + { + if(verbose)printf("\t\tpoking\n"); + reset_idle_time(); + system("gnome-screensaver-command --poke"); + } + saw_last_time = 1; + } + else + { + saw_last_time = 0; + if(verbose) + { + printf(":( no face \n"); + } + } + +} + void screensave(time_t t_current) { static int last_elapse = 0; @@ -107,7 +136,10 @@ void screensave(time_t t_current) if(elapse > timeout && elapse != last_elapse) { last_elapse = elapse; - printf("elapse = %d\n", elapse); + if(verbose) + { + printf("elapse = %d\n", elapse); + } if(!activated) { printf("activated\n"); @@ -123,39 +155,63 @@ void screensave(time_t t_current) } } -// A Simple Camera Capture Framework. -int main(int argc, char** argv) +int parse_opts(int argc, char **argv) { - CvHaarClassifierCascade* cascade = load_object_detector(argv[1]); - - CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); - if(!capture) { - cerr << "ERROR: capture is NULL " << endl; - getchar(); - return -1; + int index, c; + opterr = 0; + const char *options = "vc:\x0"; + while((c = getopt(argc, argv, options)) != -1) + { + switch(c) + { + case 'v': verbose = 1; break; + case 'c': cascade_filename = string(optarg); break; + case '?': + if(optopt == 'c') + { + printf("option -%c requires an argument.\n", optopt); + } + else if(isprint (optopt)) + { + printf("unknown option `-%c'.\n", optopt); + } + else + { + printf("unknown option char `\\x%x'.\n", optopt); + } + break; + case '\x0': break; //ignore. not sure why this shows up. maybe zsh? + default: abort(); + } } - // Create a window in which the captured images will be presented - cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); + for(index = optind; index < argc; index++) + printf("Non-option arg %s\n", argv[index]); - IplImage* frame = cvQueryFrame(capture); - if(!frame) { - cerr << "ERROR: frame is null..." << endl; - getchar(); - return -1; + if(cascade_filename == "") + { + printf("you must supply a filename with -c option. example:\n"); + printf( + "%s -c %s\n", + argv[0], + "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml" + ); + return 1; } + if(verbose) printf("SILENT ALARM ACTIVATED!!!\n"); + return 0; +} + +void get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade) +{ + IplImage* frame = NULL; + get_one_frame(capture, frame); + IplImage* mirrored = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels); - t_current = time(NULL); // Show the image captured from the camera in the window and repeat while(1) { - // Get one frame - frame = cvQueryFrame(capture); - if(!frame) { - cerr << "ERROR: frame is null..." << endl; - getchar(); - break; - } + if(get_one_frame(capture, frame)) break; //flip the image so displayed right/left corresponds to physical right/left cvFlip(frame, mirrored, MIRROR); @@ -163,16 +219,53 @@ int main(int argc, char** argv) detect_and_draw_objects(mirrored, cascade, 1); cvShowImage("mywindow", mirrored); // Do not release the frame! - screensave(t_current); + //screensave(t_current); //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator if((cvWaitKey(100) & 255) == 27) break; } + cvReleaseImage(&mirrored); +} + +int do_capture() +{ + CvHaarClassifierCascade* cascade = + load_object_detector(cascade_filename.c_str()); + + CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); + if(!capture) { + cerr << "ERROR: capture is NULL " << endl; + getchar(); + return -1; + } + + // Create a window in which the captured images will be presented + cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); + t_current = time(NULL); + get_frames(capture, cascade); // Release the capture device housekeeping cvReleaseCapture(&capture); cvDestroyWindow("mywindow"); - cvReleaseImage(&mirrored); return 0; } + +int get_one_frame(CvCapture* capture, IplImage* &frame) +{ + frame = cvQueryFrame(capture); + if(!frame) { + cerr << "ERROR: frame is null..." << endl; + getchar(); + return 1; + } + return 0; +} + +// A Simple Camera Capture Framework. +int main(int argc, char** argv) +{ + if(parse_opts(argc, argv)) return 0; + + return do_capture(); +}