#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
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++)
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;
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");
}
}
-// 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);
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();
+}