2010-11-17 20:49:53 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2010-11-23 00:46:49 +08:00
|
|
|
#include <stdexcept>
|
2010-11-17 20:49:53 +08:00
|
|
|
#include "opencv2/gpu/gpu.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
|
|
|
|
/** Contains all properties of application (including those which can be
|
|
|
|
changed by user in runtime) */
|
|
|
|
class Settings
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Sets default values */
|
|
|
|
Settings();
|
|
|
|
|
|
|
|
/** Reads settings from command args */
|
|
|
|
static Settings Read(int argc, char** argv);
|
|
|
|
|
|
|
|
string src;
|
|
|
|
bool src_is_video;
|
|
|
|
bool make_gray;
|
|
|
|
bool resize_src;
|
|
|
|
double resize_src_scale;
|
|
|
|
double scale;
|
|
|
|
int nlevels;
|
|
|
|
int gr_threshold;
|
|
|
|
double hit_threshold;
|
|
|
|
int win_width;
|
2010-11-17 22:11:30 +08:00
|
|
|
int win_stride_width;
|
|
|
|
int win_stride_height;
|
2010-11-17 20:49:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Describes aplication logic */
|
|
|
|
class App
|
|
|
|
{
|
2010-11-23 00:46:49 +08:00
|
|
|
public:
|
2010-11-17 20:49:53 +08:00
|
|
|
/** Initializes application */
|
|
|
|
App(const Settings& s);
|
|
|
|
|
|
|
|
/** Runs demo using OpenCV highgui module for GUI building */
|
|
|
|
void RunOpencvGui();
|
|
|
|
|
|
|
|
/** Processes user keybord input */
|
|
|
|
void HandleKey(char key);
|
|
|
|
|
|
|
|
void HogWorkBegin();
|
|
|
|
void HogWorkEnd();
|
|
|
|
double HogWorkFps() const;
|
|
|
|
|
|
|
|
void WorkBegin();
|
|
|
|
void WorkEnd();
|
|
|
|
double WorkFps() const;
|
|
|
|
|
|
|
|
const string GetPerformanceSummary() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
App operator=(App&);
|
|
|
|
|
|
|
|
Settings settings;
|
|
|
|
bool running;
|
|
|
|
|
|
|
|
bool use_gpu;
|
|
|
|
bool make_gray;
|
|
|
|
double scale;
|
|
|
|
int gr_threshold;
|
|
|
|
int nlevels;
|
|
|
|
double hit_threshold;
|
|
|
|
|
|
|
|
int64 hog_work_begin;
|
|
|
|
double hog_work_fps;
|
|
|
|
|
|
|
|
int64 work_begin;
|
|
|
|
double work_fps;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
2010-11-23 00:46:49 +08:00
|
|
|
cout << "Usage:\nsample_hog\n"
|
2010-11-17 20:49:53 +08:00
|
|
|
<< " -src <path_to_the_source>\n"
|
|
|
|
<< " [-src_is_video <true/false>] # says to interp. src as img or as video\n"
|
|
|
|
<< " [-make_gray <true/false>] # convert image to gray one or not\n"
|
|
|
|
<< " [-resize_src <true/false>] # do resize of the source image or not\n"
|
|
|
|
<< " [-resize_src_scale <double>] # preprocessing image scale factor\n"
|
|
|
|
<< " [-hit_threshold <double>] # classifying plane dist. threshold (0.0 usually)\n"
|
|
|
|
<< " [-scale <double>] # HOG window scale factor\n"
|
|
|
|
<< " [-nlevels <int>] # max number of HOG window scales\n"
|
|
|
|
<< " [-win_width <int>] # width of the window (48 or 64)\n"
|
2010-11-17 22:11:30 +08:00
|
|
|
<< " [-win_stride_width <int>] # distance by OX axis between neighbour wins\n"
|
|
|
|
<< " [-win_stride_height <int>] # distance by OY axis between neighbour wins\n"
|
2010-11-17 20:49:53 +08:00
|
|
|
<< " [-gr_threshold <int>] # merging similar rects constant\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
App app(Settings::Read(argc, argv));
|
|
|
|
app.RunOpencvGui();
|
|
|
|
}
|
|
|
|
catch (const Exception& e) { return cout << "Error: " << e.what() << endl, 1; }
|
|
|
|
catch (const exception& e) { return cout << "Error: " << e.what() << endl, 1; }
|
|
|
|
catch(...) { return cout << "Unknown exception" << endl, 1; }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Settings::Settings()
|
|
|
|
{
|
|
|
|
src_is_video = false;
|
|
|
|
make_gray = false;
|
|
|
|
resize_src = true;
|
|
|
|
resize_src_scale = 1.5;
|
|
|
|
scale = 1.05;
|
|
|
|
nlevels = 13;
|
|
|
|
gr_threshold = 8;
|
|
|
|
hit_threshold = 1.4;
|
|
|
|
win_width = 48;
|
2010-11-17 22:11:30 +08:00
|
|
|
win_stride_width = 8;
|
|
|
|
win_stride_height = 8;
|
2010-11-17 20:49:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Settings Settings::Read(int argc, char** argv)
|
|
|
|
{
|
|
|
|
cout << "Parsing command args" << endl;
|
|
|
|
|
|
|
|
Settings settings;
|
|
|
|
for (int i = 1; i < argc - 1; i += 2)
|
|
|
|
{
|
|
|
|
string key = argv[i];
|
|
|
|
string val = argv[i + 1];
|
|
|
|
if (key == "-src") settings.src = val;
|
|
|
|
else if (key == "-src_is_video") settings.src_is_video = (val == "true");
|
|
|
|
else if (key == "-make_gray") settings.make_gray = (val == "true");
|
|
|
|
else if (key == "-resize_src") settings.resize_src = (val == "true");
|
|
|
|
else if (key == "-resize_src_scale") settings.resize_src_scale = atof(val.c_str());
|
|
|
|
else if (key == "-hit_threshold") settings.hit_threshold = atof(val.c_str());
|
|
|
|
else if (key == "-scale") settings.scale = atof(val.c_str());
|
|
|
|
else if (key == "-nlevels") settings.nlevels = atoi(val.c_str());
|
|
|
|
else if (key == "-win_width") settings.win_width = atoi(val.c_str());
|
2010-11-17 22:11:30 +08:00
|
|
|
else if (key == "-win_stride_width") settings.win_stride_width = atoi(val.c_str());
|
|
|
|
else if (key == "-win_stride_height") settings.win_stride_height = atoi(val.c_str());
|
2010-11-17 20:49:53 +08:00
|
|
|
else if (key == "-gr_threshold") settings.gr_threshold = atoi(val.c_str());
|
2010-11-23 00:21:49 +08:00
|
|
|
else throw runtime_error((string("Unknown key: ") + key));
|
2010-11-17 20:49:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Command args are parsed\n";
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
App::App(const Settings &s)
|
|
|
|
{
|
|
|
|
settings = s;
|
|
|
|
cout << "\nControls:\n"
|
2010-11-17 22:11:30 +08:00
|
|
|
<< "\tESC - exit\n"
|
|
|
|
<< "\tm - change mode GPU <-> CPU\n"
|
|
|
|
<< "\tg - convert image to gray or not\n"
|
|
|
|
<< "\t1/q - increase/decrease HOG scale\n"
|
|
|
|
<< "\t2/w - increase/decrease levels count\n"
|
|
|
|
<< "\t3/e - increase/decrease HOG group threshold\n"
|
|
|
|
<< "\t4/r - increase/decrease hit threshold\n"
|
2010-11-17 20:49:53 +08:00
|
|
|
<< endl;
|
|
|
|
|
|
|
|
use_gpu = true;
|
|
|
|
make_gray = settings.make_gray;
|
|
|
|
scale = settings.scale;
|
|
|
|
gr_threshold = settings.gr_threshold;
|
|
|
|
nlevels = settings.nlevels;
|
|
|
|
hit_threshold = settings.hit_threshold;
|
|
|
|
|
|
|
|
if (settings.win_width != 64 && settings.win_width != 48)
|
|
|
|
settings.win_width = 64;
|
|
|
|
|
2010-11-17 22:11:30 +08:00
|
|
|
cout << "Scale: " << scale << endl;
|
2010-11-17 20:49:53 +08:00
|
|
|
cout << "Group threshold: " << gr_threshold << endl;
|
|
|
|
cout << "Levels number: " << nlevels << endl;
|
|
|
|
cout << "Win width: " << settings.win_width << endl;
|
2010-11-17 22:11:30 +08:00
|
|
|
cout << "Win stride: (" << settings.win_stride_width << ", " << settings.win_stride_height << ")\n";
|
2010-11-17 20:49:53 +08:00
|
|
|
cout << "Hit threshold: " << hit_threshold << endl;
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
|
2010-11-17 21:02:31 +08:00
|
|
|
|
2010-11-17 20:49:53 +08:00
|
|
|
void App::RunOpencvGui()
|
|
|
|
{
|
|
|
|
running = true;
|
|
|
|
|
2010-11-17 21:02:31 +08:00
|
|
|
Size win_size(settings.win_width, settings.win_width * 2); //(64, 128) or (48, 96)
|
2010-11-17 22:11:30 +08:00
|
|
|
Size win_stride(settings.win_stride_width, settings.win_stride_height);
|
2010-11-17 20:49:53 +08:00
|
|
|
|
|
|
|
vector<float> detector;
|
|
|
|
|
2010-11-17 22:11:30 +08:00
|
|
|
if (win_size == Size(64, 128))
|
2010-11-17 20:49:53 +08:00
|
|
|
detector = cv::gpu::HOGDescriptor::getPeopleDetector_64x128();
|
|
|
|
else
|
|
|
|
detector = cv::gpu::HOGDescriptor::getPeopleDetector_48x96();
|
|
|
|
|
|
|
|
// GPU's HOG classifier
|
2010-11-17 21:02:31 +08:00
|
|
|
cv::gpu::HOGDescriptor gpu_hog(win_size);
|
2010-11-17 20:49:53 +08:00
|
|
|
gpu_hog.setSVMDetector(detector);
|
|
|
|
|
|
|
|
// CPU's HOG classifier
|
2010-11-17 22:11:30 +08:00
|
|
|
cv::HOGDescriptor cpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9, 1, -1, HOGDescriptor::L2Hys, 0.2, true, HOGDescriptor::DEFAULT_NLEVELS);
|
2010-11-17 20:49:53 +08:00
|
|
|
cpu_hog.setSVMDetector(detector);
|
|
|
|
|
|
|
|
// Make endless cycle from video (if src is video)
|
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
VideoCapture vc;
|
|
|
|
Mat frame;
|
2010-11-23 00:46:49 +08:00
|
|
|
|
2010-11-17 20:49:53 +08:00
|
|
|
if (settings.src_is_video)
|
|
|
|
{
|
|
|
|
vc.open(settings.src.c_str());
|
|
|
|
if (!vc.isOpened())
|
2010-11-23 00:21:49 +08:00
|
|
|
throw runtime_error(string("Can't open video file: " + settings.src));
|
2010-11-17 20:49:53 +08:00
|
|
|
vc >> frame;
|
|
|
|
}
|
|
|
|
else
|
2010-11-18 17:22:23 +08:00
|
|
|
{
|
2010-11-17 20:49:53 +08:00
|
|
|
frame = imread(settings.src);
|
2010-11-18 17:22:23 +08:00
|
|
|
if (frame.empty())
|
2010-11-23 00:21:49 +08:00
|
|
|
throw runtime_error(string("Can't open image file: " + settings.src));
|
2010-11-18 17:22:23 +08:00
|
|
|
}
|
2010-11-17 20:49:53 +08:00
|
|
|
|
|
|
|
Mat img_aux, img, img_to_show;
|
|
|
|
gpu::GpuMat gpu_img;
|
|
|
|
|
|
|
|
// Iterate over all frames
|
|
|
|
while (running && !frame.empty())
|
|
|
|
{
|
|
|
|
WorkBegin();
|
|
|
|
|
|
|
|
vector<Rect> found;
|
|
|
|
|
|
|
|
// Change format of the image (input must be 8UC3)
|
|
|
|
if (make_gray)
|
|
|
|
cvtColor(frame, img_aux, CV_BGR2GRAY);
|
|
|
|
else if (use_gpu)
|
|
|
|
cvtColor(frame, img_aux, CV_BGR2BGRA);
|
|
|
|
else
|
|
|
|
img_aux = frame;
|
|
|
|
|
|
|
|
// Resize image
|
|
|
|
if (settings.resize_src)
|
|
|
|
resize(img_aux, img, Size(int(frame.cols * settings.resize_src_scale), int(frame.rows * settings.resize_src_scale)));
|
|
|
|
else
|
|
|
|
img = img_aux;
|
|
|
|
img_to_show = img;
|
|
|
|
|
|
|
|
gpu_hog.nlevels = nlevels;
|
|
|
|
cpu_hog.nlevels = nlevels;
|
|
|
|
|
|
|
|
// Perform HOG classification
|
|
|
|
HogWorkBegin();
|
|
|
|
if (use_gpu)
|
|
|
|
{
|
|
|
|
gpu_img = img;
|
2010-11-17 22:11:30 +08:00
|
|
|
gpu_hog.detectMultiScale(gpu_img, found, hit_threshold, win_stride, Size(0, 0), scale, gr_threshold);
|
2010-11-17 20:49:53 +08:00
|
|
|
}
|
|
|
|
else
|
2010-11-17 22:11:30 +08:00
|
|
|
cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride, Size(0, 0), scale, gr_threshold);
|
2010-11-17 20:49:53 +08:00
|
|
|
HogWorkEnd();
|
|
|
|
|
|
|
|
// Draw positive classified windows
|
|
|
|
for (size_t i = 0; i < found.size(); i++)
|
|
|
|
{
|
|
|
|
Rect r = found[i];
|
|
|
|
rectangle(img_to_show, r.tl(), r.br(), CV_RGB(0, 255, 0), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkEnd();
|
|
|
|
|
|
|
|
// Show results
|
|
|
|
putText(img_to_show, GetPerformanceSummary(), Point(5, 25), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2);
|
|
|
|
imshow("opencv_gpu_hog", img_to_show);
|
2010-11-24 17:43:17 +08:00
|
|
|
HandleKey((char)waitKey(3));
|
2010-11-17 20:49:53 +08:00
|
|
|
|
|
|
|
if (settings.src_is_video)
|
|
|
|
{
|
|
|
|
vc >> frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void App::HandleKey(char key)
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 27:
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
use_gpu = !use_gpu;
|
|
|
|
cout << "Switched to " << (use_gpu ? "CUDA" : "CPU") << " mode\n";
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
make_gray = !make_gray;
|
|
|
|
cout << "Convert image to gray: " << (make_gray ? "YES" : "NO") << endl;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
scale *= 1.05;
|
|
|
|
cout << "Scale: " << scale << endl;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
scale /= 1.05;
|
|
|
|
cout << "Scale: " << scale << endl;
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
nlevels++;
|
|
|
|
cout << "Levels number: " << nlevels << endl;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
nlevels = max(nlevels - 1, 1);
|
|
|
|
cout << "Levels number: " << nlevels << endl;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
gr_threshold++;
|
|
|
|
cout << "Group threshold: " << gr_threshold << endl;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
gr_threshold = max(0, gr_threshold - 1);
|
|
|
|
cout << "Group threshold: " << gr_threshold << endl;
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
hit_threshold+=0.25;
|
|
|
|
cout << "Hit threshold: " << hit_threshold << endl;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
hit_threshold = max(0.0, hit_threshold - 0.25);
|
|
|
|
cout << "Hit threshold: " << hit_threshold << endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void App::HogWorkBegin() { hog_work_begin = getTickCount(); }
|
|
|
|
|
|
|
|
|
2010-11-23 00:46:49 +08:00
|
|
|
inline void App::HogWorkEnd()
|
2010-11-17 20:49:53 +08:00
|
|
|
{
|
|
|
|
int64 delta = getTickCount() - hog_work_begin;
|
|
|
|
double freq = getTickFrequency();
|
|
|
|
hog_work_fps = freq / delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline double App::HogWorkFps() const { return hog_work_fps; }
|
|
|
|
|
|
|
|
|
|
|
|
inline void App::WorkBegin() { work_begin = getTickCount(); }
|
|
|
|
|
|
|
|
|
2010-11-23 00:46:49 +08:00
|
|
|
inline void App::WorkEnd()
|
2010-11-17 20:49:53 +08:00
|
|
|
{
|
|
|
|
int64 delta = getTickCount() - work_begin;
|
|
|
|
double freq = getTickFrequency();
|
|
|
|
work_fps = freq / delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline double App::WorkFps() const { return work_fps; }
|
|
|
|
|
|
|
|
|
2010-11-23 00:46:49 +08:00
|
|
|
inline const string App::GetPerformanceSummary() const
|
|
|
|
{
|
2010-11-17 20:49:53 +08:00
|
|
|
stringstream ss;
|
|
|
|
ss << (use_gpu ? "GPU" : "CPU") << " HOG FPS: " << setiosflags(ios::left) << setprecision(4) <<
|
|
|
|
setw(7) << HogWorkFps() << " Total FPS: " << setprecision(4) << setw(7) << WorkFps();
|
|
|
|
return ss.str();
|
2010-11-23 00:46:49 +08:00
|
|
|
}
|