in progress, need libxss-dev for screensaverpoking
[ozzloy@gmail.com/oble] / oble.cpp
index ab7d509341105b2a942a45dd1558d27c371ba232..3f351ae574eb9af94e5ba1cdc37be0d08d1e586f 100644 (file)
--- a/oble.cpp
+++ b/oble.cpp
 #include <stdio.h>
 #include <iostream>
 #include <time.h>
+#include <unistd.h>
 #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
 
@@ -74,11 +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);
-               //system("./gss-poke.py");
-       }
+       update_idle(faces->total);
 
        /* draw all the rectangles */
        for(i = 0; i < faces->total; i++)
@@ -96,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;
@@ -105,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");
@@ -121,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);
@@ -161,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();
+}