allow specifying cascade file as cli arg. Closes #7
[ozzloy@gmail.com/oble] / oble.cpp
index 0e24b9e05ff6462e3b8a095f8fc768afbcad9d83..d949cd594d9f31df2c287b943af0281472229e09 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 = "";
+
 // argument for cvFlip(src, dest, FLIP_TYPE)
 #define MIRROR 1
 
@@ -146,10 +149,47 @@ void screensave(time_t t_current)
        }
 }
 
+int parse_opts(int argc, char **argv)
+{
+       int index, c;
+       //int opterr=0, optopt=0, optind=0;
+       //char *optarg;
+       // int getopt(int argc, char **argv, const char *options);
+       opterr = 0;
+       const char *options = "c:";
+       while((c = getopt(argc, argv, options)) != -1)
+               switch(c)
+               {
+                       case 'c':
+                               cascade_filename = string(optarg);
+                               printf("option c, argument = %s\n", optarg);
+                       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);
+                               goto known_args_processed;
+                       default:
+                               abort();
+               }
+known_args_processed:
+
+       for(index = optind; index < argc; index++)
+               printf("Non-option arg %s\n", argv[index]);
+
+       return 0;
+}
+
 // A Simple Camera Capture Framework.
 int main(int argc, char** argv)
 {
-       CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
+       if(parse_opts(argc, argv)) return 0;
+
+       if(cascade_filename == "") cascade_filename = string(argv[1]);
+       CvHaarClassifierCascade* cascade =
+               load_object_detector(cascade_filename.c_str());
 
        CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
        if(!capture) {