added gpu HOG sample
authorAlexey Spizhevoy <no@email>
Wed, 17 Nov 2010 12:49:53 +0000 (12:49 +0000)
committerAlexey Spizhevoy <no@email>
Wed, 17 Nov 2010 12:49:53 +0000 (12:49 +0000)
samples/CMakeLists.txt
samples/gpu/CMakeLists.txt [new file with mode: 0644]
samples/gpu/gpu_hog.cpp [new file with mode: 0644]
samples/gpu/road.png [new file with mode: 0644]

index fa7556b..f5735e9 100644 (file)
@@ -7,3 +7,4 @@ add_subdirectory(c)
 add_subdirectory(cpp)
 add_subdirectory(swig_python)
 add_subdirectory(octave)
+#add_subdirectory(gpu)
diff --git a/samples/gpu/CMakeLists.txt b/samples/gpu/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c658e8c
--- /dev/null
@@ -0,0 +1,59 @@
+if (BUILD_EXAMPLES)\r
+    project(gpu_samples)\r
+\r
+    include_directories(\r
+        "${CMAKE_SOURCE_DIR}/modules/core/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/flann/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/imgproc/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/video/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/highgui/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/ml/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/calib3d/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/features2d/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/objdetect/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/legacy/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/contrib/include"\r
+        "${CMAKE_SOURCE_DIR}/modules/gpu/include"\r
+    )   \r
+\r
+    if(CMAKE_COMPILER_IS_GNUCXX)\r
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")\r
+    endif()\r
+    \r
+    # ---------------------------------------------\r
+    #      Define executable targets\r
+    # ---------------------------------------------\r
+    MACRO(MY_DEFINE_EXAMPLE name srcs)\r
+        set(the_target "example_${name}")\r
+       add_executable(${the_target} ${srcs})\r
+               set_target_properties(${the_target} PROPERTIES\r
+                   OUTPUT_NAME "${name}"\r
+                   PROJECT_LABEL "(EXAMPLE) ${name}")\r
+       add_dependencies(${the_target} opencv_core opencv_flann opencv_imgproc opencv_highgui\r
+           opencv_ml opencv_video opencv_objdetect opencv_features2d\r
+           opencv_calib3d opencv_legacy opencv_contrib opencv_gpu)\r
+       target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} opencv_core\r
+           opencv_flann opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_objdetect\r
+           opencv_features2d opencv_calib3d opencv_legacy opencv_contrib opencv_gpu)\r
+\r
+        if(WIN32)\r
+            install(TARGETS ${the_target}\r
+                RUNTIME DESTINATION "samples/gpu" COMPONENT main)\r
+        endif()\r
+    ENDMACRO(MY_DEFINE_EXAMPLE)\r
+    \r
+    file(GLOB gpu_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)\r
+    \r
+    foreach(sample_filename ${gpu_samples})\r
+        get_filename_component(sample ${sample_filename} NAME_WE)\r
+        MY_DEFINE_EXAMPLE(${sample}  ${sample_filename})\r
+    endforeach()\r
+endif(BUILD_EXAMPLES)\r
+\r
+if (NOT WIN32)\r
+    file(GLOB GPU_FILES *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd )\r
+    install(FILES ${GPU_FILES}\r
+            DESTINATION share/opencv/samples/gpu\r
+            PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)\r
+endif ()\r
+\r
diff --git a/samples/gpu/gpu_hog.cpp b/samples/gpu/gpu_hog.cpp
new file mode 100644 (file)
index 0000000..0fd4d94
--- /dev/null
@@ -0,0 +1,373 @@
+#include <iostream>\r
+#include <fstream>\r
+#include <string>\r
+#include <sstream>\r
+#include <iomanip>\r
+#include "opencv2/gpu/gpu.hpp"\r
+#include "opencv2/highgui/highgui.hpp"\r
+\r
+using namespace std;\r
+using namespace cv;\r
+\r
+\r
+/** Contains all properties of application (including those which can be\r
+changed by user in runtime) */\r
+class Settings\r
+{\r
+public:\r
+    /** Sets default values */\r
+    Settings();\r
+\r
+    /** Reads settings from command args */\r
+    static Settings Read(int argc, char** argv);\r
+\r
+    string src;\r
+    bool src_is_video;\r
+    bool make_gray;\r
+    bool resize_src;\r
+    double resize_src_scale;\r
+    double scale;\r
+    int nlevels;\r
+    int gr_threshold;\r
+    double hit_threshold;\r
+    int win_width;\r
+};\r
+\r
+\r
+/** Describes aplication logic */\r
+class App\r
+{\r
+public:    \r
+    /** Initializes application */\r
+    App(const Settings& s);\r
+\r
+    /** Runs demo using OpenCV highgui module for GUI building */\r
+    void RunOpencvGui();\r
+\r
+    /** Processes user keybord input */\r
+    void HandleKey(char key);\r
+\r
+    void HogWorkBegin();\r
+    void HogWorkEnd();\r
+    double HogWorkFps() const;\r
+\r
+    void WorkBegin();\r
+    void WorkEnd();\r
+    double WorkFps() const;\r
+\r
+    const string GetPerformanceSummary() const;\r
+\r
+private:\r
+    App operator=(App&);\r
+\r
+    Settings settings;\r
+    bool running;\r
+\r
+    bool use_gpu;\r
+    bool make_gray;\r
+    double scale;\r
+    int gr_threshold;\r
+    int nlevels;\r
+    double hit_threshold;\r
+\r
+    int64 hog_work_begin;\r
+    double hog_work_fps;\r
+\r
+    int64 work_begin;\r
+    double work_fps;\r
+};\r
+\r
+\r
+int main(int argc, char** argv)\r
+{\r
+    try\r
+    {\r
+        if (argc < 2)\r
+        {\r
+            cout << "Usage:\nsample_hog\n" \r
+                << "  -src <path_to_the_source>\n"\r
+                << "  [-src_is_video <true/false>] # says to interp. src as img or as video\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
+                << "  [-resize_src_scale <double>] # preprocessing image scale factor\n"\r
+                << "  [-hit_threshold <double>] # classifying plane dist. threshold (0.0 usually)\n"\r
+                << "  [-scale <double>] # HOG window scale factor\n"\r
+                << "  [-nlevels <int>] # max number of HOG window scales\n"\r
+                << "  [-win_width <int>] # width of the window (48 or 64)\n"\r
+                << "  [-gr_threshold <int>] # merging similar rects constant\n";\r
+            return 1;\r
+        }\r
+        App app(Settings::Read(argc, argv));\r
+        app.RunOpencvGui();\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
+    return 0;\r
+}\r
+\r
+\r
+Settings::Settings()\r
+{\r
+    src_is_video = false;\r
+    make_gray = false;\r
+    resize_src = true;\r
+    resize_src_scale = 1.5;\r
+    scale = 1.05;\r
+    nlevels = 13;\r
+    gr_threshold = 8;\r
+    hit_threshold = 1.4;\r
+    win_width = 48;\r
+}\r
+\r
+\r
+Settings Settings::Read(int argc, char** argv)\r
+{\r
+    cout << "Parsing command args" << endl;\r
+\r
+    Settings settings;\r
+    for (int i = 1; i < argc - 1; i += 2)\r
+    {\r
+        string key = argv[i];\r
+        string val = argv[i + 1];\r
+        if (key == "-src") settings.src = val;\r
+        else if (key == "-src_is_video") settings.src_is_video = (val == "true");\r
+        else if (key == "-make_gray") settings.make_gray = (val == "true");\r
+        else if (key == "-resize_src") settings.resize_src = (val == "true");\r
+        else if (key == "-resize_src_scale") settings.resize_src_scale = atof(val.c_str());\r
+        else if (key == "-hit_threshold") settings.hit_threshold = atof(val.c_str());\r
+        else if (key == "-scale") settings.scale = atof(val.c_str());\r
+        else if (key == "-nlevels") settings.nlevels = atoi(val.c_str());\r
+        else if (key == "-win_width") settings.win_width = atoi(val.c_str());\r
+        else if (key == "-gr_threshold") settings.gr_threshold = atoi(val.c_str());\r
+        else throw exception((string("Unknown key: ") + key).c_str());\r
+    }\r
+\r
+    cout << "Command args are parsed\n";\r
+    return settings;\r
+}\r
+\r
+\r
+\r
+App::App(const Settings &s)\r
+{\r
+    settings = s;\r
+    cout << "\nControls:\n"\r
+         << "ESC - exit\n"\r
+         << "m - change mode GPU <-> CPU\n"\r
+         << "g - convert image to gray or not\n"\r
+         << "1/q - increase/decrease HOG scale\n"\r
+         << "2/w - increase/decrease levels count\n"\r
+         << "3/e - increase/decrease HOG group threshold\n"\r
+         << "4/r - increase/decrease hit threshold\n"\r
+         << endl;\r
+\r
+    use_gpu = true;\r
+    make_gray = settings.make_gray;\r
+    scale = settings.scale;\r
+    gr_threshold = settings.gr_threshold;\r
+    nlevels = settings.nlevels;\r
+    hit_threshold = settings.hit_threshold;\r
+\r
+    if (settings.win_width != 64 && settings.win_width != 48)\r
+        settings.win_width = 64;\r
+\r
+    cout << endl << "Scale: " << scale << endl;\r
+    cout << "Group threshold: " << gr_threshold << endl;\r
+    cout << "Levels number: " << nlevels << endl;\r
+    cout << "Win width: " << settings.win_width << endl;\r
+    cout << "Hit threshold: " << hit_threshold << endl;\r
+    cout << endl;\r
+}\r
+\r
+void App::RunOpencvGui()\r
+{\r
+    running = true;\r
+\r
+    Size win_width(settings.win_width, settings.win_width * 2); //(64, 128) or (48, 96)\r
+\r
+    vector<float> detector;\r
+\r
+    if (win_width == Size(64,128))\r
+        detector = cv::gpu::HOGDescriptor::getPeopleDetector_64x128();\r
+    else\r
+        detector = cv::gpu::HOGDescriptor::getPeopleDetector_48x96();\r
+\r
+    // GPU's HOG classifier\r
+    cv::gpu::HOGDescriptor gpu_hog(win_width);\r
+    gpu_hog.setSVMDetector(detector);\r
+\r
+    // CPU's HOG classifier\r
+    cv::HOGDescriptor cpu_hog(win_width, Size(16,16), Size(8,8), Size(8,8), 9, 1, -1, HOGDescriptor::L2Hys, 0.2, true, HOGDescriptor::DEFAULT_NLEVELS);\r
+    cpu_hog.setSVMDetector(detector);\r
+\r
+    // Make endless cycle from video (if src is video)\r
+    while (running)\r
+    {\r
+        VideoCapture vc;\r
+        Mat frame;\r
+        \r
+        if (settings.src_is_video)\r
+        {\r
+            vc.open(settings.src.c_str());\r
+            if (!vc.isOpened())\r
+                throw exception(string("Can't open video file: " + settings.src).c_str());\r
+            vc >> frame;\r
+        }\r
+        else\r
+            frame = imread(settings.src);\r
+\r
+        Mat img_aux, img, img_to_show;\r
+        gpu::GpuMat gpu_img;\r
+\r
+        // Iterate over all frames\r
+        while (running && !frame.empty())\r
+        {\r
+            WorkBegin();\r
+\r
+            vector<Rect> found;\r
+\r
+            // Change format of the image (input must be 8UC3)\r
+            if (make_gray)\r
+                cvtColor(frame, img_aux, CV_BGR2GRAY);\r
+            else if (use_gpu)\r
+                cvtColor(frame, img_aux, CV_BGR2BGRA);\r
+            else\r
+                img_aux = frame;\r
+\r
+            // Resize image\r
+            if (settings.resize_src)\r
+                resize(img_aux, img, Size(int(frame.cols * settings.resize_src_scale), int(frame.rows * settings.resize_src_scale)));\r
+            else\r
+                img = img_aux;\r
+            img_to_show = img;\r
+\r
+            gpu_hog.nlevels = nlevels;\r
+            cpu_hog.nlevels = nlevels;\r
+\r
+            // Perform HOG classification\r
+            HogWorkBegin();\r
+            if (use_gpu)\r
+            {\r
+                gpu_img = img;\r
+                gpu_hog.detectMultiScale(gpu_img, found, hit_threshold, Size(8, 8), Size(0, 0), scale, gr_threshold);\r
+            }\r
+            else\r
+                cpu_hog.detectMultiScale(img, found, hit_threshold, Size(8, 8), Size(0, 0), scale, gr_threshold);\r
+            HogWorkEnd();\r
+\r
+            // Draw positive classified windows\r
+            for (size_t i = 0; i < found.size(); i++)\r
+            {\r
+                Rect r = found[i];\r
+                rectangle(img_to_show, r.tl(), r.br(), CV_RGB(0, 255, 0), 3);\r
+            }\r
+\r
+            WorkEnd();\r
+\r
+            // Show results\r
+            putText(img_to_show, GetPerformanceSummary(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2);\r
+            imshow("opencv_gpu_hog", img_to_show);\r
+            HandleKey((char)waitKey(1));\r
+\r
+            if (settings.src_is_video)\r
+            {\r
+                vc >> frame;\r
+            }\r
+        }\r
+    }\r
+}\r
+\r
+\r
+void App::HandleKey(char key)\r
+{\r
+    switch (key)\r
+    {\r
+    case 27:\r
+        running = false;\r
+        break;\r
+    case 'm':\r
+    case 'M':\r
+        use_gpu = !use_gpu;\r
+        cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";\r
+        break;\r
+    case 'g':\r
+    case 'G':\r
+        make_gray = !make_gray;\r
+        cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;\r
+        break;\r
+    case '1':\r
+        scale *= 1.05;\r
+        cout << "Scale: " << scale << endl;\r
+        break;\r
+    case 'q':\r
+    case 'Q':\r
+        scale /= 1.05;\r
+        cout << "Scale: " << scale << endl;\r
+        break;\r
+    case '2':\r
+        nlevels++;\r
+        cout << "Levels number: " << nlevels << endl;\r
+        break;\r
+    case 'w':\r
+    case 'W':\r
+        nlevels = max(nlevels - 1, 1);\r
+        cout << "Levels number: " << nlevels << endl;\r
+        break;\r
+    case '3':\r
+        gr_threshold++;\r
+        cout << "Group threshold: " << gr_threshold << endl;\r
+        break;\r
+    case 'e':\r
+    case 'E':\r
+        gr_threshold = max(0, gr_threshold - 1);\r
+        cout << "Group threshold: " << gr_threshold << endl;\r
+        break;\r
+    case '4':\r
+        hit_threshold+=0.25;\r
+        cout << "Hit threshold: " << hit_threshold << endl;\r
+        break;\r
+    case 'r':\r
+    case 'R':\r
+        hit_threshold = max(0.0, hit_threshold - 0.25);\r
+        cout << "Hit threshold: " << hit_threshold << endl;\r
+        break;\r
+    }\r
+}\r
+\r
+\r
+inline void App::HogWorkBegin() { hog_work_begin = getTickCount(); }\r
+\r
+\r
+inline void App::HogWorkEnd() \r
+{\r
+    int64 delta = getTickCount() - hog_work_begin;\r
+    double freq = getTickFrequency();\r
+    hog_work_fps = freq / delta;\r
+}\r
+\r
+\r
+inline double App::HogWorkFps() const { return hog_work_fps; }\r
+\r
+\r
+inline void App::WorkBegin() { work_begin = getTickCount(); }\r
+\r
+\r
+inline void App::WorkEnd() \r
+{\r
+    int64 delta = getTickCount() - work_begin;\r
+    double freq = getTickFrequency();\r
+    work_fps = freq / delta;\r
+}\r
+\r
+\r
+inline double App::WorkFps() const { return work_fps; }\r
+\r
+\r
+inline const string App::GetPerformanceSummary() const \r
+{ \r
+    stringstream ss;\r
+    ss << (use_gpu ? "GPU" : "CPU") << " HOG FPS: " << setiosflags(ios::left) << setprecision(4) <<\r
+       setw(7) << HogWorkFps() << " Total FPS: " << setprecision(4) << setw(7) << WorkFps();\r
+    return ss.str();\r
+}
\ No newline at end of file
diff --git a/samples/gpu/road.png b/samples/gpu/road.png
new file mode 100644 (file)
index 0000000..a52c87c
Binary files /dev/null and b/samples/gpu/road.png differ