#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
class App
{
public:
void run();
void handleKey(char key);
void hogWorkBegin();
void hogWorkEnd();
string hogWorkFps() const;
void workBegin();
void workEnd();
string workFps() const;
private:
App operator=(App&);
bool running;
bool make_gray;
double resize_scale;
int win_width;
int win_stride_width, win_stride_height;
int gr_threshold;
int nlevels;
double hit_threshold;
bool gamma_corr;
double hog_work_fps;
double work_fps;
string img_source;
string vdo_source;
string output;
int camera_id;
bool write_once;
};
int main(int argc, char** argv)
{
const char* keys =
"{ h help | | print help message }"
"{ i input | | specify input image}"
"{ c camera | -1 | enable camera capturing }"
"{ v video | vtest.avi | use video as input }"
"{ g gray | | convert image to gray one or not}"
"{ s scale | 1.0 | resize the image before detect}"
"{ o output | output.avi | specify output path when input is images}";
if (cmd.has("help"))
{
cmd.printMessage();
return EXIT_SUCCESS;
}
App app(cmd);
try
{
app.run();
}
{
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 EXIT_SUCCESS;
}
{
cout << "\nControls:\n"
<< "\tESC - exit\n"
<< "\tm - change mode GPU <-> CPU\n"
<< "\tg - convert image to gray or not\n"
<< "\to - save output image once, or switch on/off video save\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"
<< endl;
make_gray = cmd.
has(
"gray");
resize_scale = cmd.
get<
double>(
"s");
img_source = cmd.
get<
string>(
"i");
output = cmd.
get<
string>(
"o");
camera_id = cmd.
get<
int>(
"c");
win_width = 48;
win_stride_width = 8;
win_stride_height = 8;
gr_threshold = 8;
nlevels = 13;
hit_threshold = 1.4;
gamma_corr = true;
write_once = false;
cout << "Group threshold: " << gr_threshold << endl;
cout << "Levels number: " << nlevels << endl;
cout << "Win width: " << win_width << endl;
cout << "Win stride: (" << win_stride_width << ", " << win_stride_height << ")\n";
cout << "Hit threshold: " << hit_threshold << endl;
cout << "Gamma correction: " << gamma_corr << endl;
cout << endl;
}
void App::run()
{
running = true;
Size win_size(win_width, win_width * 2);
Size win_stride(win_stride_width, win_stride_height);
HOGDescriptor hog(win_size,
Size(16, 16),
Size(8, 8),
Size(8, 8), 9, 1, -1,
hog.setSVMDetector( HOGDescriptor::getDaimlerPeopleDetector() );
while (running)
{
if (vdo_source!="")
{
vc.
open(vdo_source.c_str());
throw runtime_error(string("can't open video file: " + vdo_source));
vc >> frame;
}
else if (camera_id != -1)
{
{
stringstream msg;
msg << "can't open camera: " << camera_id;
throw runtime_error(msg.str());
}
vc >> frame;
}
else
{
throw runtime_error(string("can't open image file: " + img_source));
}
UMat img_aux, img, img_to_show;
while (running && !frame.
empty())
{
workBegin();
{
Size sz((
int)((
double)img_aux.
cols/resize_scale), (
int)((
double)img_aux.
rows/resize_scale));
}
else img = img_aux;
hog.nlevels = nlevels;
vector<Rect> found;
hogWorkBegin();
hog.detectMultiScale(img, found, hit_threshold, win_stride,
hogWorkEnd();
for (size_t i = 0; i < found.size(); i++)
{
}
imshow(
"opencv_hog", img_to_show);
if (vdo_source!="" || camera_id!=-1) vc >> frame;
workEnd();
if (output!="" && write_once)
{
if (img_source!="")
{
write_once = false;
}
else
{
{
video_writer.
open(output, VideoWriter::fourcc(
'x',
'v',
'i',
'd'), 24,
img_to_show.
size(),
true);
throw std::runtime_error("can't create video writer");
}
video_writer << img;
}
}
}
}
}
void App::handleKey(char key)
{
switch (key)
{
case 27:
running = false;
break;
case 'm':
case 'M':
cout <<
"Switched to " << (
ocl::useOpenCL() ?
"OpenCL enabled" :
"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':
cout <<
"Scale: " <<
scale << endl;
break;
case 'q':
case 'Q':
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;
case 'c':
case 'C':
gamma_corr = !gamma_corr;
cout << "Gamma correction: " << gamma_corr << endl;
break;
case 'o':
case 'O':
write_once = !write_once;
break;
}
}
inline void App::hogWorkBegin()
{
}
inline void App::hogWorkEnd()
{
hog_work_fps = freq / delta;
}
inline string App::hogWorkFps() const
{
stringstream ss;
ss << hog_work_fps;
return ss.str();
}
inline void App::workBegin()
{
}
inline void App::workEnd()
{
work_fps = freq / delta;
}
inline string App::workFps() const
{
stringstream ss;
ss << work_fps;
return ss.str();
}
Designed for command line parsing.
Definition: utility.hpp:818
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition: utility.hpp:884
bool has(const String &name) const
Check if field was provided in the command line.
Class passed to an error.
Definition: core.hpp:119
virtual const char * what() const CV_OVERRIDE
MatExpr abs(const Mat &m)
Calculates an absolute value of each matrix element.
MatExpr max(const Mat &a, const Mat &b)
void copyTo(OutputArray m) const
Copies the matrix to another one.
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:330
int cols
number of columns in the matrix; -1 when the matrix has more than 2 dimensions
Definition: mat.hpp:2601
MatSize size
dimensional size of the matrix; accessible in various formats
Definition: mat.hpp:2622
int rows
number of rows in the matrix; -1 when the matrix has more than 2 dimensions
Definition: mat.hpp:2598
bool empty() const
returns true if matrix data is NULL
void copyTo(OutputArray m) const
copies the matrix content to "m".
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:683
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
Video writer class.
Definition: videoio.hpp:961
virtual bool open(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
Initializes or reinitializes video writer.
virtual bool isOpened() const
Returns true if video writer has been successfully initialized.
Point2i Point
Definition: types.hpp:204
Size2i Size
Definition: types.hpp:365
Scalar_< double > Scalar
Definition: types.hpp:691
int64_t int64
Definition: interface.h:61
void setUseOpenCL(bool flag)
cv::String findFileOrKeep(const cv::String &relative_path, bool silentMode=false)
Definition: utility.hpp:1185
double getTickFrequency()
Returns the number of ticks per second.
int64 getTickCount()
Returns the number of ticks.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >())
Saves an image to a specified file.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:551
@ COLOR_BGRA2BGR
remove alpha channel from RGB or BGR image
Definition: imgproc.hpp:536
@ COLOR_GRAY2BGR
Definition: imgproc.hpp:553
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
@ FONT_HERSHEY_SIMPLEX
normal size sans-serif font
Definition: imgproc.hpp:868
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106
Implementation of HOG (Histogram of Oriented Gradients) descriptor and object detector.
Definition: objdetect.hpp:378
@ DEFAULT_NLEVELS
Default nlevels value.
Definition: objdetect.hpp:382