From aaa335b617123309a89a38d74c1c71d97e8ee22c Mon Sep 17 00:00:00 2001 From: catree Date: Sun, 4 Sep 2016 04:17:15 +0200 Subject: [PATCH] Add the possibility to use a video stream with the peopledetect.cpp sample. Fixed video input argument. --- samples/cpp/peopledetect.cpp | 116 +++++++++++++++++++++++++++-------- 1 file changed, 89 insertions(+), 27 deletions(-) diff --git a/samples/cpp/peopledetect.cpp b/samples/cpp/peopledetect.cpp index e14d07c1b1..aca05fd0ca 100644 --- a/samples/cpp/peopledetect.cpp +++ b/samples/cpp/peopledetect.cpp @@ -1,27 +1,23 @@ +#include +#include #include #include #include -#include +#include +#include +#include using namespace cv; using namespace std; -static void help() -{ - cout << "\nThis program demonstrates the use of the HoG descriptor using\n" - " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n" - "Usage:\n" - "./peopledetect --i= | --d=\n\n" - "During execution:\n\tHit q or ESC key to quit.\n" - "\tUsing OpenCV version " << CV_VERSION << "\n" << endl; -} - const char* keys = { - "{help h||}" - "{image i| ../data/basketball2.png|input image name}" - "{directory d||images directory}" + "{ help h | | print help message }" + "{ image i | | specify input image}" + "{ camera c | | enable camera capturing }" + "{ video v | ../data/768x576.avi | use video as input }" + "{ directory d | | images directory}" }; static void detectAndDraw(const HOGDescriptor &hog, Mat &img) @@ -69,7 +65,12 @@ int main(int argc, char** argv) if (parser.has("help")) { - help(); + cout << "\nThis program demonstrates the use of the HoG descriptor using\n" + " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"; + parser.printMessage(); + cout << "During execution:\n\tHit q or ESC key to quit.\n" + "\tUsing OpenCV version " << CV_VERSION << "\n" + "Note: camera device number must be different from -1.\n" << endl; return 0; } @@ -78,6 +79,8 @@ int main(int argc, char** argv) namedWindow("people detector", 1); string pattern_glob = ""; + string video_filename = "../data/768x576.avi"; + int camera_id = -1; if (parser.has("directory")) { pattern_glob = parser.get("directory"); @@ -86,26 +89,85 @@ int main(int argc, char** argv) { pattern_glob = parser.get("image"); } + else if (parser.has("camera")) + { + camera_id = parser.get("camera"); + } + else if (parser.has("video")) + { + video_filename = parser.get("video"); + } - if (!pattern_glob.empty()) + if (!pattern_glob.empty() || camera_id != -1 || !video_filename.empty()) { + //Read from input image files vector filenames; - String folder(pattern_glob); - glob(folder, filenames); + //Read from video file + VideoCapture vc; + Mat frame; - for (vector::const_iterator it = filenames.begin(); it != filenames.end(); ++it) + if (!pattern_glob.empty()) { - cout << "\nRead: " << *it << endl; - // Read current image - Mat current_image = imread(*it); + String folder(pattern_glob); + glob(folder, filenames); + } + else if (camera_id != -1) + { + vc.open(camera_id); + if (!vc.isOpened()) + { + stringstream msg; + msg << "can't open camera: " << camera_id; + throw runtime_error(msg.str()); + } + } + else + { + vc.open(video_filename.c_str()); + if (!vc.isOpened()) + throw runtime_error(string("can't open video file: " + video_filename)); + } + + vector::const_iterator it_image = filenames.begin(); - if (current_image.empty()) - continue; + for (;;) + { + if (!pattern_glob.empty()) + { + bool read_image_ok = false; + for (; it_image != filenames.end(); ++it_image) + { + cout << "\nRead: " << *it_image << endl; + // Read current image + frame = imread(*it_image); + + if (!frame.empty()) + { + ++it_image; + read_image_ok = true; + break; + } + } + + //No more valid images + if (!read_image_ok) + { + //Release the image in order to exit the while loop + frame.release(); + } + } + else + { + vc >> frame; + } + + if (frame.empty()) + break; - detectAndDraw(hog, current_image); + detectAndDraw(hog, frame); - imshow("people detector", current_image); - int c = waitKey(0) & 255; + imshow("people detector", frame); + int c = waitKey( vc.isOpened() ? 30 : 0 ) & 255; if ( c == 'q' || c == 'Q' || c == 27) break; }