diff --git a/apps/detector_Kozitsin.cpp b/apps/detector_Kozitsin.cpp new file mode 100644 index 0000000..e9e0ecd --- /dev/null +++ b/apps/detector_Kozitsin.cpp @@ -0,0 +1,126 @@ +#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}; + +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"); + + // Load detector. + CascadeClassifier unnLogoCascade; + CascadeClassifier unnOldLogoCascade; + CascadeClassifier openCVLogoCascade; + CascadeClassifier itseezFullCascade; + CascadeClassifier itseezEyeCascade; + + // TODO: change paths!!! + if (!unnLogoCascade.load(detector_file)) + { + cout << "UNN not ready!"; + return -1; + } + /*if (!unnOldLogoCascade.load(pathToCascade)) + { + cout << "UNN old not ready!"; + return -1; + } + if (!openCVLogoCascade.load(pathToCascade)) + { + cout << "openCV not ready!"; + return -1; + } + if (!itseezFullCascade.load(pathToCascade)) + { + cout << "itseez full not ready!"; + return -1; + } + if (!itseezEyeCascade.load(pathToCascade)) + { + cout << "itseez eye not ready!"; + return -1; + }*/ + + + + + if (!image_file.empty()) + { + // TODO: Detect objects on image. + vector unnLogos; + Mat image = imread(image_file); + + unnLogoCascade.detectMultiScale(image, unnLogos); + cout << "Num of images found: " << unnLogos.size() << endl; + } + else if (!video_file.empty()) + { + // TODO: Detect objects on every frame of a video. + + } + else if (use_camera) + { + // TODO: Detect objects on a live video stream from camera. + Mat frame; + VideoCapture capture; + capture.open(0); + capture.grab(); + + while (true) + { + capture >> frame; + + vector unnLogos; + unnLogoCascade.detectMultiScale(frame, unnLogos); + cout << unnLogos.size() << endl; + + } + } + + return 0; +} + + + diff --git a/scripts/train_cascade_detector.py b/scripts/train_cascade_detector.py index dd5eb0a..1a64a90 100755 --- a/scripts/train_cascade_detector.py +++ b/scripts/train_cascade_detector.py @@ -24,23 +24,23 @@ vec_file_path = base_name + ".vec" negatives_list_file = join(project_root_dir, "images", "negatives.txt") -create_samples_call = "{0} -img {1} -bgthresh -1 -bg {2} -maxxangle 0.7 -maxyangle 0.7 -maxzangle 0.5 " \ - "-num 1000 -w {3} -h {4} -vec {5}".format(join(opencv_bin_dir, create_samples_app), - args.image, negatives_list_file, args.width, - args.height, vec_file_path) +create_samples_call = '"{0}" -img "{1}" -bgthresh -1 -bg "{2}" -maxxangle 0.7 -maxyangle 0.7 -maxzangle 0.5 ' \ + '-num 1000 -w {3} -h {4} -vec "{5}"'.format(join(opencv_bin_dir, create_samples_app), + args.image, negatives_list_file, args.width, + args.height, vec_file_path) print("call: " + create_samples_call) call(create_samples_call, shell=True) cascade_dir = base_name + "_cascade" if not isdir(cascade_dir): makedirs(cascade_dir) -train_cascade_call = "{0} -vec {1} -numPos 500 -bg {2} -numNeg 500 -feature_type {3} -maxFalseAlarmRate 0.1 " \ - "-numStages 5 -w {4} -h {5} -data {6}".format(join(opencv_bin_dir, train_cascade_app), - vec_file_path, negatives_list_file, args.features, - args.width, args.height, cascade_dir) +train_cascade_call = '"{0}" -vec "{1}" -numPos 500 -bg "{2}" -numNeg 500 -featureType {3} -maxFalseAlarmRate 0.1 ' \ + '-numStages 5 -w {4} -h {5} -data "{6}"'.format(join(opencv_bin_dir, train_cascade_app), + vec_file_path, negatives_list_file, args.features, + args.width, args.height, cascade_dir) print("call: " + train_cascade_call) call(train_cascade_call, shell=True) detector_file = join(cascade_dir, "cascade.xml") if isfile(detector_file): - print("cascade detector has been successfully trained and located at '{}'.".format(detector_file)) + print("cascade detector has been successfully trained and located at '{}'.".format(detector_file)) \ No newline at end of file