opencv/samples/ocl/hog.cpp

443 lines
13 KiB
C++
Raw Normal View History

2012-08-08 17:12:53 +08:00
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <opencv2/core/utility.hpp>
#include "opencv2/ocl.hpp"
#include "opencv2/highgui.hpp"
2012-08-08 17:12:53 +08:00
using namespace std;
using namespace cv;
class App
{
public:
2013-06-19 16:36:23 +08:00
App(CommandLineParser& cmd);
2012-08-08 17:12:53 +08:00
void run();
void handleKey(char key);
void hogWorkBegin();
void hogWorkEnd();
string hogWorkFps() const;
void workBegin();
void workEnd();
string workFps() const;
string message() const;
2013-06-19 16:36:23 +08:00
// This function test if gpu_rst matches cpu_rst.
// If the two vectors are not equal, it will return the difference in vector size
2013-06-19 16:36:23 +08:00
// Else if will return
// (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels)
2013-06-19 16:36:23 +08:00
double checkRectSimilarity(Size sz,
std::vector<Rect>& cpu_rst,
std::vector<Rect>& gpu_rst);
2012-08-08 17:12:53 +08:00
private:
App operator=(App&);
2013-06-19 16:36:23 +08:00
//Args args;
2012-08-08 17:12:53 +08:00
bool running;
bool use_gpu;
bool make_gray;
double scale;
2013-06-19 16:36:23 +08:00
double resize_scale;
int win_width;
int win_stride_width, win_stride_height;
2012-08-08 17:12:53 +08:00
int gr_threshold;
int nlevels;
double hit_threshold;
bool gamma_corr;
int64 hog_work_begin;
double hog_work_fps;
int64 work_begin;
double work_fps;
2013-06-19 16:36:23 +08:00
string img_source;
string vdo_source;
string output;
int camera_id;
2013-06-28 15:08:39 +08:00
bool write_once;
2013-06-19 16:36:23 +08:00
};
2012-08-08 17:12:53 +08:00
int main(int argc, char** argv)
{
2013-06-19 16:36:23 +08:00
const char* keys =
"{ h | help | false | print help message }"
"{ i | input | | specify input image}"
"{ c | camera | -1 | enable camera capturing }"
"{ v | video | | use video as input }"
"{ g | gray | false | convert image to gray one or not}"
"{ s | scale | 1.0 | resize the image before detect}"
"{ l |larger_win| false | use 64x128 window}"
"{ o | output | | specify output path when input is images}";
CommandLineParser cmd(argc, argv, keys);
App app(cmd);
2012-08-08 17:12:53 +08:00
try
{
app.run();
}
2013-06-19 16:36:23 +08:00
catch (const Exception& e)
2012-08-08 17:12:53 +08:00
{
2013-06-19 16:36:23 +08:00
return cout << "error: " << e.what() << endl, 1;
2012-08-08 17:12:53 +08:00
}
2013-06-19 16:36:23 +08:00
catch (const exception& e)
{
return cout << "error: " << e.what() << endl, 1;
2012-08-08 17:12:53 +08:00
}
2013-06-19 16:36:23 +08:00
catch(...)
{
return cout << "unknown exception" << endl, 1;
}
return 0;
2012-08-08 17:12:53 +08:00
}
2013-06-19 16:36:23 +08:00
App::App(CommandLineParser& cmd)
2012-08-08 17:12:53 +08:00
{
cout << "\nControls:\n"
<< "\tESC - exit\n"
<< "\tm - change mode GPU <-> CPU\n"
<< "\tg - convert image to gray or not\n"
2013-06-28 15:08:39 +08:00
<< "\to - save output image once, or switch on/off video save\n"
2012-08-08 17:12:53 +08:00
<< "\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;
2013-06-19 16:36:23 +08:00
use_gpu = true;
make_gray = cmd.get<bool>("g");
resize_scale = cmd.get<double>("s");
win_width = cmd.get<bool>("l") == true ? 64 : 48;
vdo_source = cmd.get<string>("v");
img_source = cmd.get<string>("i");
output = cmd.get<string>("o");
camera_id = cmd.get<int>("c");
2012-08-08 17:12:53 +08:00
2013-06-19 16:36:23 +08:00
win_stride_width = 8;
win_stride_height = 8;
gr_threshold = 8;
nlevels = 13;
hit_threshold = win_width == 48 ? 1.4 : 0.;
scale = 1.05;
gamma_corr = true;
2013-06-28 15:08:39 +08:00
write_once = false;
2012-08-08 17:12:53 +08:00
cout << "Group threshold: " << gr_threshold << endl;
cout << "Levels number: " << nlevels << endl;
2013-06-19 16:36:23 +08:00
cout << "Win width: " << win_width << endl;
cout << "Win stride: (" << win_stride_width << ", " << win_stride_height << ")\n";
2012-08-08 17:12:53 +08:00
cout << "Hit threshold: " << hit_threshold << endl;
cout << "Gamma correction: " << gamma_corr << endl;
cout << endl;
}
void App::run()
{
2013-06-19 16:36:23 +08:00
vector<ocl::Info> oclinfo;
2012-10-17 15:12:04 +08:00
ocl::getDevice(oclinfo);
2012-08-08 17:12:53 +08:00
running = true;
2013-06-19 16:36:23 +08:00
VideoWriter video_writer;
2012-08-08 17:12:53 +08:00
2013-06-19 16:36:23 +08:00
Size win_size(win_width, win_width * 2);
Size win_stride(win_stride_width, win_stride_height);
2012-08-08 17:12:53 +08:00
// Create HOG descriptors and detectors here
vector<float> detector;
if (win_size == Size(64, 128))
2013-06-19 16:36:23 +08:00
detector = ocl::HOGDescriptor::getPeopleDetector64x128();
2012-08-08 17:12:53 +08:00
else
2013-06-19 16:36:23 +08:00
detector = ocl::HOGDescriptor::getPeopleDetector48x96();
2012-08-08 17:12:53 +08:00
2013-06-19 16:36:23 +08:00
ocl::HOGDescriptor gpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9,
ocl::HOGDescriptor::DEFAULT_WIN_SIGMA, 0.2, gamma_corr,
ocl::HOGDescriptor::DEFAULT_NLEVELS);
HOGDescriptor cpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9, 1, -1,
HOGDescriptor::L2Hys, 0.2, gamma_corr, cv::HOGDescriptor::DEFAULT_NLEVELS);
2012-08-08 17:12:53 +08:00
gpu_hog.setSVMDetector(detector);
cpu_hog.setSVMDetector(detector);
while (running)
{
VideoCapture vc;
Mat frame;
2013-06-19 16:36:23 +08:00
if (vdo_source!="")
2012-08-08 17:12:53 +08:00
{
2013-06-19 16:36:23 +08:00
vc.open(vdo_source.c_str());
2012-08-08 17:12:53 +08:00
if (!vc.isOpened())
2013-06-19 16:36:23 +08:00
throw runtime_error(string("can't open video file: " + vdo_source));
2012-08-08 17:12:53 +08:00
vc >> frame;
}
2013-06-19 16:36:23 +08:00
else if (camera_id != -1)
2012-08-08 17:12:53 +08:00
{
2013-06-19 16:36:23 +08:00
vc.open(camera_id);
2012-08-08 17:12:53 +08:00
if (!vc.isOpened())
{
stringstream msg;
2013-06-19 16:36:23 +08:00
msg << "can't open camera: " << camera_id;
2012-08-08 17:12:53 +08:00
throw runtime_error(msg.str());
}
vc >> frame;
}
else
{
2013-06-19 16:36:23 +08:00
frame = imread(img_source);
2012-08-08 17:12:53 +08:00
if (frame.empty())
2013-06-19 16:36:23 +08:00
throw runtime_error(string("can't open image file: " + img_source));
2012-08-08 17:12:53 +08:00
}
Mat img_aux, img, img_to_show;
ocl::oclMat gpu_img;
// Iterate over all frames
bool verify = false;
2012-08-08 17:12:53 +08:00
while (running && !frame.empty())
{
workBegin();
// Change format of the image
if (make_gray) cvtColor(frame, img_aux, COLOR_BGR2GRAY);
else if (use_gpu) cvtColor(frame, img_aux, COLOR_BGR2BGRA);
2012-08-08 17:12:53 +08:00
else frame.copyTo(img_aux);
// Resize image
2013-06-19 16:36:23 +08:00
if (abs(scale-1.0)>0.001)
{
Size sz((int)((double)img_aux.cols/resize_scale), (int)((double)img_aux.rows/resize_scale));
resize(img_aux, img, sz);
}
2012-08-08 17:12:53 +08:00
else img = img_aux;
img_to_show = img;
gpu_hog.nlevels = nlevels;
cpu_hog.nlevels = nlevels;
vector<Rect> found;
// Perform HOG classification
hogWorkBegin();
if (use_gpu)
{
gpu_img.upload(img);
gpu_hog.detectMultiScale(gpu_img, found, hit_threshold, win_stride,
Size(0, 0), scale, gr_threshold);
if (!verify)
{
// verify if GPU output same objects with CPU at 1st run
verify = true;
vector<Rect> ref_rst;
Merge remote-tracking branch 'origin/2.4' Pull requests: #943 from jet47:cuda-5.5-support #944 from jet47:cmake-2.8.11-cuda-fix #912 from SpecLad:contributing #934 from SpecLad:parallel-for #931 from jet47:gpu-test-fixes #932 from bitwangyaoyao:2.4_fixBFM #918 from bitwangyaoyao:2.4_samples #924 from pengx17:2.4_arithm_fix #925 from pengx17:2.4_canny_tmp_fix #927 from bitwangyaoyao:2.4_perf #930 from pengx17:2.4_haar_ext #928 from apavlenko:bugfix_3027 #920 from asmorkalov:android_move #910 from pengx17:2.4_oclgfft #913 from janm399:2.4 #916 from bitwangyaoyao:2.4_fixPyrLK #919 from abidrahmank:2.4 #923 from pengx17:2.4_macfix Conflicts: modules/calib3d/src/stereobm.cpp modules/features2d/src/detectors.cpp modules/gpu/src/error.cpp modules/gpu/src/precomp.hpp modules/imgproc/src/distransform.cpp modules/imgproc/src/morph.cpp modules/ocl/include/opencv2/ocl/ocl.hpp modules/ocl/perf/perf_color.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/precomp.cpp modules/ocl/perf/precomp.hpp modules/ocl/src/arithm.cpp modules/ocl/src/canny.cpp modules/ocl/src/filtering.cpp modules/ocl/src/haar.cpp modules/ocl/src/hog.cpp modules/ocl/src/imgproc.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/ocl/src/pyrlk.cpp modules/video/src/bgfg_gaussmix2.cpp modules/video/src/lkpyramid.cpp platforms/linux/scripts/cmake_arm_gnueabi_hardfp.sh platforms/linux/scripts/cmake_arm_gnueabi_softfp.sh platforms/scripts/ABI_compat_generator.py samples/ocl/facedetect.cpp
2013-06-04 22:31:51 +08:00
cvtColor(img, img, COLOR_BGRA2BGR);
cpu_hog.detectMultiScale(img, ref_rst, hit_threshold, win_stride,
2013-06-19 16:36:23 +08:00
Size(0, 0), scale, gr_threshold-2);
double accuracy = checkRectSimilarity(img.size(), ref_rst, found);
2013-06-19 16:36:23 +08:00
cout << "\naccuracy value: " << accuracy << endl;
}
}
2012-08-08 17:12:53 +08:00
else cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
2013-06-19 16:36:23 +08:00
Size(0, 0), scale, gr_threshold);
2012-08-08 17:12:53 +08:00
hogWorkEnd();
2013-06-19 16:36:23 +08:00
2012-08-08 17:12:53 +08:00
// Draw positive classified windows
for (size_t i = 0; i < found.size(); i++)
{
Rect r = found[i];
2013-04-12 18:12:12 +08:00
rectangle(img_to_show, r.tl(), r.br(), Scalar(0, 255, 0), 3);
2012-08-08 17:12:53 +08:00
}
if (use_gpu)
putText(img_to_show, "Mode: GPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
else
putText(img_to_show, "Mode: CPU", Point(5, 25), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
putText(img_to_show, "FPS (HOG only): " + hogWorkFps(), Point(5, 65), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
putText(img_to_show, "FPS (total): " + workFps(), Point(5, 105), FONT_HERSHEY_SIMPLEX, 1., Scalar(255, 100, 0), 2);
imshow("opencv_gpu_hog", img_to_show);
2013-06-19 16:36:23 +08:00
if (vdo_source!="" || camera_id!=-1) vc >> frame;
2012-08-08 17:12:53 +08:00
workEnd();
2013-06-28 15:08:39 +08:00
if (output!="" && write_once)
2012-08-08 17:12:53 +08:00
{
2013-06-19 16:36:23 +08:00
if (img_source!="") // wirte image
2012-08-08 17:12:53 +08:00
{
2013-06-28 15:08:39 +08:00
write_once = false;
2013-06-19 16:36:23 +08:00
imwrite(output, img_to_show);
2012-08-08 17:12:53 +08:00
}
2013-06-19 16:36:23 +08:00
else //write video
{
if (!video_writer.isOpened())
{
video_writer.open(output, VideoWriter::fourcc('x','v','i','d'), 24,
2013-06-19 16:36:23 +08:00
img_to_show.size(), true);
if (!video_writer.isOpened())
throw std::runtime_error("can't create video writer");
}
2012-08-08 17:12:53 +08:00
if (make_gray) cvtColor(img_to_show, img, COLOR_GRAY2BGR);
else cvtColor(img_to_show, img, COLOR_BGRA2BGR);
2012-08-08 17:12:53 +08:00
2013-06-19 16:36:23 +08:00
video_writer << img;
}
2012-08-08 17:12:53 +08:00
}
handleKey((char)waitKey(3));
}
}
}
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;
case 'c':
case 'C':
gamma_corr = !gamma_corr;
cout << "Gamma correction: " << gamma_corr << endl;
break;
2013-06-28 15:08:39 +08:00
case 'o':
case 'O':
write_once = !write_once;
break;
2012-08-08 17:12:53 +08:00
}
}
2013-06-19 16:36:23 +08:00
inline void App::hogWorkBegin()
{
hog_work_begin = getTickCount();
}
2012-08-08 17:12:53 +08:00
inline void App::hogWorkEnd()
{
int64 delta = getTickCount() - hog_work_begin;
double freq = getTickFrequency();
hog_work_fps = freq / delta;
}
inline string App::hogWorkFps() const
{
stringstream ss;
ss << hog_work_fps;
return ss.str();
}
2013-06-19 16:36:23 +08:00
inline void App::workBegin()
{
work_begin = getTickCount();
}
2012-08-08 17:12:53 +08:00
inline void App::workEnd()
{
int64 delta = getTickCount() - work_begin;
double freq = getTickFrequency();
work_fps = freq / delta;
}
inline string App::workFps() const
{
stringstream ss;
ss << work_fps;
return ss.str();
}
2013-06-19 16:36:23 +08:00
double App::checkRectSimilarity(Size sz,
std::vector<Rect>& ob1,
std::vector<Rect>& ob2)
{
double final_test_result = 0.0;
size_t sz1 = ob1.size();
size_t sz2 = ob2.size();
if(sz1 != sz2)
2013-06-19 16:36:23 +08:00
{
return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
2013-06-19 16:36:23 +08:00
}
else
{
2013-06-19 16:36:23 +08:00
if(sz1==0 && sz2==0)
return 0;
cv::Mat cpu_result(sz, CV_8UC1);
cpu_result.setTo(0);
2013-06-19 16:36:23 +08:00
for(vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
2013-06-19 16:36:23 +08:00
{
cv::Mat cpu_result_roi(cpu_result, *r);
cpu_result_roi.setTo(1);
cpu_result.copyTo(cpu_result);
}
int cpu_area = cv::countNonZero(cpu_result > 0);
2013-06-19 16:36:23 +08:00
cv::Mat gpu_result(sz, CV_8UC1);
gpu_result.setTo(0);
for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
{
cv::Mat gpu_result_roi(gpu_result, *r2);
gpu_result_roi.setTo(1);
gpu_result.copyTo(gpu_result);
}
cv::Mat result_;
multiply(cpu_result, gpu_result, result_);
int result = cv::countNonZero(result_ > 0);
2013-06-19 16:36:23 +08:00
if(cpu_area!=0 && result!=0)
final_test_result = 1.0 - (double)result/(double)cpu_area;
else if(cpu_area==0 && result!=0)
final_test_result = -1;
}
return final_test_result;
}