Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions apps/detector_Kozitsin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <iostream>
#include <string>
#include <vector>

#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<Rect>& 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<bool>("help"))
{
parser.printParams();
return 0;
}

string detector_file = parser.get<string>("detector");
CV_Assert(!detector_file.empty());
string image_file = parser.get<string>("image");
string video_file = parser.get<string>("video");
bool use_camera = parser.get<bool>("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<Rect> 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<Rect> unnLogos;
unnLogoCascade.detectMultiScale(frame, unnLogos);
cout << unnLogos.size() << endl;

}
}

return 0;
}



18 changes: 9 additions & 9 deletions scripts/train_cascade_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этих изменений в ваших коммитах быть не должно.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я залил их, т.к. вручную менял скриптовый файл. Не синкая с апстримом.

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))