diff --git a/apps/detector_Shchedrin.cpp b/apps/detector_Shchedrin.cpp new file mode 100644 index 0000000..e7cbe82 --- /dev/null +++ b/apps/detector_Shchedrin.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + +#include "opencv2/core/core.hpp" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/objdetect/objdetect.hpp" + +using namespace std; +using namespace cv; + +const char* params = + "{ h | help | false | print usage }" + "{ | detector | | XML file with a cascade detector }" + "{ | image | | image to detect objects on }" + "{ | video | | video file to detect on }" + "{ | camera | false | whether to detect on video stream from camera }"; + + +void drawDetections(const vector& detections, + const Scalar& color, + Mat& image) +{ + for (size_t i = 0; i < detections.size(); ++i) + { + rectangle(image, detections[i], color, 2); + } +} + +const Scalar red(0, 0, 255); +const Scalar green(0, 255, 0); +const Scalar blue(255, 0, 0); +const Scalar colors[] = {red, green, blue}; + +void detectOnImage(CascadeClassifier &classif, const Mat &image, Mat &res){ + res = image.clone(); + vector found; + classif.detectMultiScale(image, found); + for(int i = 0; i < found.size(); i++){ + rectangle(res, found[i],Scalar(100,200,0),2); + } +} + +int main(int argc, char** argv) +{ + // Parse command line arguments. + CommandLineParser parser(argc, argv, params); + // If help flag is present, print help message and exit. + if (parser.get("help")) + { + parser.printParams(); + return 0; + } + + string detector_file = parser.get("detector"); + CV_Assert(!detector_file.empty()); + string image_file = parser.get("image"); + string video_file = parser.get("video"); + bool use_camera = parser.get("camera"); + + // TODO: Load detector. + CascadeClassifier classif; + + if(!classif.load(detector_file)){ + cerr<<"Can not load detector"<> image; + if(image.empty()){ + continue; + } + detectOnImage(classif, image, res); + imshow("detect", res); + key = waitKey(1); + }while(key != 27); + + } + else if (use_camera) + { + int key = 0; + VideoCapture cap(0); + if(!cap.isOpened()){ + cerr<<"Can not connect to camera"<> image; + if(image.empty()){ + continue; + } + detectOnImage(classif, image, res); + imshow("detect", res); + key = waitKey(1); + }while(key != 27); + } + else + { + cout << "Declare a source of images to detect on." << endl; + } + + return 0; +} + + +