video writing via cmd args in hog sample added
authorAlexey Spizhevoy <no@email>
Thu, 30 Dec 2010 07:19:32 +0000 (07:19 +0000)
committerAlexey Spizhevoy <no@email>
Thu, 30 Dec 2010 07:19:32 +0000 (07:19 +0000)
samples/gpu/hog.cpp

index 493aa6e..d0ff6b3 100644 (file)
@@ -10,8 +10,6 @@
 using namespace std;\r
 using namespace cv;\r
 \r
-//#define WRITE_VIDEO\r
-\r
 class Args\r
 {\r
 public:\r
@@ -23,15 +21,21 @@ public:
     bool src_is_camera;\r
     int camera_id;\r
 \r
+    bool write_video;\r
+    string dst_video;\r
+    double dst_video_fps;\r
+\r
     bool make_gray;\r
 \r
     bool resize_src;\r
-    int resized_width, resized_height;\r
+    int width, height;\r
 \r
     double scale;\r
     int nlevels;\r
     int gr_threshold;\r
+\r
     double hit_threshold;\r
+    bool hit_threshold_auto;\r
 \r
     int win_width;\r
     int win_stride_width, win_stride_height;\r
@@ -93,8 +97,8 @@ int main(int argc, char** argv)
                 << "  [--src-is-camera <true/false>] # says to interpretate src as camera\n"\r
                 << "  [--make-gray <true/false>] # convert image to gray one or not\n"\r
                 << "  [--resize-src <true/false>] # do resize of the source image or not\n"\r
-                << "  [--src-width <int>] # resized image width\n"\r
-                << "  [--src-height <int>] # resized image height\n"\r
+                << "  [--width <int>] # resized image width\n"\r
+                << "  [--height <int>] # resized image height\n"\r
                 << "  [--hit-threshold <double>] # classifying plane distance threshold (0.0 usually)\n"\r
                 << "  [--scale <double>] # HOG window scale factor\n"\r
                 << "  [--nlevels <int>] # max number of HOG window scales\n"\r
@@ -102,15 +106,18 @@ int main(int argc, char** argv)
                 << "  [--win-stride-width <int>] # distance by OX axis between neighbour wins\n"\r
                 << "  [--win-stride-height <int>] # distance by OY axis between neighbour wins\n"\r
                 << "  [--gr-threshold <int>] # merging similar rects constant\n"\r
-                << "  [--gamma-correct <int>] # do gamma correction or not\n";\r
+                << "  [--gamma-correct <int>] # do gamma correction or not\n"\r
+                << "  [--write-video <bool>] # write video or not\n"\r
+                << "  [--dst-video <path>] # output video path\n"\r
+                << "  [--dst-video-fps <double>] # output video fps\n";\r
             return 1;\r
         }\r
         App app(Args::read(argc, argv));\r
         app.run();\r
     }\r
-    catch (const Exception& e) { return cout << "Error: "  << e.what() << endl, 1; }\r
-    catch (const exception& e) { return cout << "Error: "  << e.what() << endl, 1; }\r
-    catch(...) { return cout << "Unknown exception" << endl, 1; }\r
+    catch (const Exception& e) { return cout << "error: "  << e.what() << endl, 1; }\r
+    catch (const exception& e) { return cout << "error: "  << e.what() << endl, 1; }\r
+    catch(...) { return cout << "unknown exception" << endl, 1; }\r
     return 0;\r
 }\r
 \r
@@ -121,16 +128,20 @@ Args::Args()
     src_is_camera = false;\r
     camera_id = 0;\r
 \r
+    write_video = false;\r
+    dst_video_fps = 24.;\r
+\r
     make_gray = false;\r
 \r
     resize_src = false;\r
-    resized_width = 640;\r
-    resized_height = 480;\r
+    width = 640;\r
+    height = 480;\r
 \r
     scale = 1.05;\r
     nlevels = 13;\r
     gr_threshold = 8;\r
     hit_threshold = 1.4;\r
+    hit_threshold_auto = true;\r
 \r
     win_width = 48;\r
     win_stride_width = 8;\r
@@ -153,16 +164,23 @@ Args Args::read(int argc, char** argv)
         else if (key == "--camera-id") args.camera_id = atoi(val.c_str());\r
         else if (key == "--make-gray") args.make_gray = (val == "true");\r
         else if (key == "--resize-src") args.resize_src = (val == "true");\r
-        else if (key == "--src-width") args.resized_width = atoi(val.c_str());\r
-        else if (key == "--src-height") args.resized_height = atoi(val.c_str());\r
-        else if (key == "--hit-threshold") args.hit_threshold = atof(val.c_str());\r
+        else if (key == "--width") args.width = atoi(val.c_str());\r
+        else if (key == "--height") args.height = atoi(val.c_str());\r
+        else if (key == "--hit-threshold") \r
+        { \r
+            args.hit_threshold = atof(val.c_str()); \r
+            args.hit_threshold_auto = false; \r
+        }\r
         else if (key == "--scale") args.scale = atof(val.c_str());\r
         else if (key == "--nlevels") args.nlevels = atoi(val.c_str());\r
         else if (key == "--win-width") args.win_width = atoi(val.c_str());\r
         else if (key == "--win-stride-width") args.win_stride_width = atoi(val.c_str());\r
         else if (key == "--win-stride-height") args.win_stride_height = atoi(val.c_str());\r
         else if (key == "--gr-threshold") args.gr_threshold = atoi(val.c_str());\r
-        else if (key == "--gamma-correct") args.gamma_corr = atoi(val.c_str()) != 0;\r
+        else if (key == "--gamma-correct") args.gamma_corr = (val == "true");\r
+        else if (key == "--write-video") args.write_video = (val == "true");\r
+        else if (key == "--dst-video") args.dst_video = val;\r
+        else if (key == "--dst-video-fps") args.dst_video_fps= atof(val.c_str());\r
         else throw runtime_error((string("unknown key: ") + key));\r
     }\r
     return args;\r
@@ -187,7 +205,11 @@ App::App(const Args& s)
     scale = args.scale;\r
     gr_threshold = args.gr_threshold;\r
     nlevels = args.nlevels;\r
+\r
+    if (args.hit_threshold_auto)\r
+        args.hit_threshold = args.win_width == 48 ? 1.4 : 0.;\r
     hit_threshold = args.hit_threshold;\r
+\r
     gamma_corr = args.gamma_corr;\r
 \r
     if (args.win_width != 64 && args.win_width != 48)\r
@@ -195,7 +217,7 @@ App::App(const Args& s)
 \r
     cout << "Scale: " << scale << endl;\r
     if (args.resize_src)\r
-        cout << "Source size: (" << args.resized_width << ", " << args.resized_height << ")\n";\r
+        cout << "Resized source: (" << args.width << ", " << args.height << ")\n";\r
     cout << "Group threshold: " << gr_threshold << endl;\r
     cout << "Levels number: " << nlevels << endl;\r
     cout << "Win width: " << args.win_width << endl;\r
@@ -209,10 +231,12 @@ App::App(const Args& s)
 void App::run()\r
 {\r
     running = true;\r
+    cv::VideoWriter video_writer;\r
 \r
     Size win_size(args.win_width, args.win_width * 2); //(64, 128) or (48, 96)\r
     Size win_stride(args.win_stride_width, args.win_stride_height);\r
 \r
+    // Create HOG descriptors and detectors here\r
     vector<float> detector;\r
     if (win_size == Size(64, 128)) \r
         detector = cv::gpu::HOGDescriptor::getPeopleDetector_64x128();\r
@@ -227,13 +251,6 @@ void App::run()
     gpu_hog.setSVMDetector(detector);\r
     cpu_hog.setSVMDetector(detector);\r
 \r
-#ifdef WRITE_VIDEO\r
-    cv::VideoWriter video_writer;\r
-    video_writer.open("output.avi", CV_FOURCC('x','v','i','d'), 24., cv::Size(640, 480), true);\r
-    if (!video_writer.isOpened())\r
-        throw std::runtime_error("can't create video writer");\r
-#endif\r
-\r
     while (running)\r
     {\r
         VideoCapture vc;\r
@@ -274,7 +291,7 @@ void App::run()
             else img_aux = frame;\r
 \r
             // Resize image\r
-            if (args.resize_src) resize(img_aux, img, Size(args.resized_width, args.resized_height));\r
+            if (args.resize_src) resize(img_aux, img, Size(args.width, args.height));\r
             else img = img_aux;\r
             img_to_show = img;\r
 \r
@@ -315,10 +332,18 @@ void App::run()
 \r
             workEnd();\r
 \r
-#ifdef WRITE_VIDEO\r
-            cvtColor(img_to_show, img, CV_BGRA2BGR);\r
-            video_writer << img;\r
-#endif\r
+            if (args.write_video)\r
+            {\r
+                if (!video_writer.isOpened())\r
+                {\r
+                    video_writer.open(args.dst_video, CV_FOURCC('x','v','i','d'), args.dst_video_fps, \r
+                                      img_to_show.size(), true);\r
+                    if (!video_writer.isOpened())\r
+                        throw std::runtime_error("can't create video writer");\r
+                }\r
+                cvtColor(img_to_show, img, CV_BGRA2BGR);\r
+                video_writer << img;\r
+            }\r
         }\r
     }\r
 }\r
@@ -418,3 +443,4 @@ inline string App::workFps() const
     ss << work_fps;\r
     return ss.str();\r
 }\r
+\r