changed hog to work with variable parameters and changed the hog sample to test it with more options

added comments and tests
This commit is contained in:
ManuelFreudenreich
2015-06-11 13:29:23 +02:00
parent 424c2bddb3
commit 89889ae8ea
4 changed files with 467 additions and 113 deletions
+99 -15
View File
@@ -22,10 +22,14 @@ public:
static Args read(int argc, char** argv);
string src;
bool src_is_folder;
bool src_is_video;
bool src_is_camera;
int camera_id;
bool svm_load;
string svm;
bool write_video;
string dst_video;
double dst_video_fps;
@@ -44,6 +48,10 @@ public:
int win_width;
int win_stride_width, win_stride_height;
int block_width;
int block_stride_width, block_stride_height;
int cell_width;
int nbins;
bool gamma_corr;
};
@@ -93,6 +101,9 @@ static void printHelp()
cout << "Histogram of Oriented Gradients descriptor and detector sample.\n"
<< "\nUsage: hog_gpu\n"
<< " (<image>|--video <vide>|--camera <camera_id>) # frames source\n"
<< " or"
<< " (--folder <folder_path>) # load images from folder\n"
<< " [--svm <file> # load svm file"
<< " [--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"
<< " [--width <int>] # resized image width\n"
@@ -100,9 +111,14 @@ static void printHelp()
<< " [--hit_threshold <double>] # classifying plane distance 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"
<< " [--win_width <int>] # width of the window\n"
<< " [--win_stride_width <int>] # distance by OX axis between neighbour wins\n"
<< " [--win_stride_height <int>] # distance by OY axis between neighbour wins\n"
<< " [--block_width <int>] # width of the block\n"
<< " [--block_stride_width <int>] # distance by 0X axis between neighbour blocks\n"
<< " [--block_stride_height <int>] # distance by 0Y axis between neighbour blocks\n"
<< " [--cell_width <int>] # width of the cell\n"
<< " [--nbins <int>] # number of bins\n"
<< " [--gr_threshold <int>] # merging similar rects constant\n"
<< " [--gamma_correct <int>] # do gamma correction or not\n"
<< " [--write_video <bool>] # write video or not\n"
@@ -142,6 +158,8 @@ Args::Args()
{
src_is_video = false;
src_is_camera = false;
src_is_folder = false;
svm_load = false;
camera_id = 0;
write_video = false;
@@ -162,6 +180,11 @@ Args::Args()
win_width = 48;
win_stride_width = 8;
win_stride_height = 8;
block_width = 16;
block_stride_width = 8;
block_stride_height = 8;
cell_width = 8;
nbins = 9;
gamma_corr = true;
}
@@ -186,6 +209,11 @@ Args Args::read(int argc, char** argv)
else if (string(argv[i]) == "--win_width") args.win_width = atoi(argv[++i]);
else if (string(argv[i]) == "--win_stride_width") args.win_stride_width = atoi(argv[++i]);
else if (string(argv[i]) == "--win_stride_height") args.win_stride_height = atoi(argv[++i]);
else if (string(argv[i]) == "--block_width") args.block_width = atoi(argv[++i]);
else if (string(argv[i]) == "--block_stride_width") args.block_stride_width = atoi(argv[++i]);
else if (string(argv[i]) == "--block_stride_height") args.block_stride_height = atoi(argv[++i]);
else if (string(argv[i]) == "--cell_width") args.cell_width = atoi(argv[++i]);
else if (string(argv[i]) == "--nbins") args.nbins = atoi(argv[++i]);
else if (string(argv[i]) == "--gr_threshold") args.gr_threshold = atoi(argv[++i]);
else if (string(argv[i]) == "--gamma_correct") args.gamma_corr = (string(argv[++i]) == "true");
else if (string(argv[i]) == "--write_video") args.write_video = (string(argv[++i]) == "true");
@@ -194,6 +222,8 @@ Args Args::read(int argc, char** argv)
else if (string(argv[i]) == "--help") printHelp();
else if (string(argv[i]) == "--video") { args.src = argv[++i]; args.src_is_video = true; }
else if (string(argv[i]) == "--camera") { args.camera_id = atoi(argv[++i]); args.src_is_camera = true; }
else if (string(argv[i]) == "--folder") { args.src = argv[++i]; args.src_is_folder = true;}
else if (string(argv[i]) == "--svm") { args.svm = argv[++i]; args.svm_load = true;}
else if (args.src.empty()) args.src = argv[i];
else throw runtime_error((string("unknown key: ") + argv[i]));
}
@@ -228,16 +258,17 @@ App::App(const Args& s)
gamma_corr = args.gamma_corr;
if (args.win_width != 64 && args.win_width != 48)
args.win_width = 64;
cout << "Scale: " << scale << endl;
if (args.resize_src)
cout << "Resized source: (" << args.width << ", " << args.height << ")\n";
cout << "Group threshold: " << gr_threshold << endl;
cout << "Levels number: " << nlevels << endl;
cout << "Win width: " << args.win_width << endl;
cout << "Win size: (" << args.win_width << ", " << args.win_width*2 << ")\n";
cout << "Win stride: (" << args.win_stride_width << ", " << args.win_stride_height << ")\n";
cout << "Block size: (" << args.block_width << ", " << args.block_width << ")\n";
cout << "Block stride: (" << args.block_stride_width << ", " << args.block_stride_height << ")\n";
cout << "Cell size: (" << args.cell_width << ", " << args.cell_width << ")\n";
cout << "Bins number: " << args.nbins << endl;
cout << "Hit threshold: " << hit_threshold << endl;
cout << "Gamma correction: " << gamma_corr << endl;
cout << endl;
@@ -249,22 +280,58 @@ void App::run()
running = true;
cv::VideoWriter video_writer;
Size win_size(args.win_width, args.win_width * 2); //(64, 128) or (48, 96)
Size win_stride(args.win_stride_width, args.win_stride_height);
Size win_size(args.win_width, args.win_width * 2);
Size block_size(args.block_width, args.block_width);
Size block_stride(args.block_stride_width, args.block_stride_height);
Size cell_size(args.cell_width, args.cell_width);
cv::Ptr<cv::cuda::HOG> gpu_hog = cv::cuda::HOG::create(win_size);
cv::HOGDescriptor cpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9);
cv::Ptr<cv::cuda::HOG> gpu_hog = cv::cuda::HOG::create(win_size, block_size, block_stride, cell_size, args.nbins);
cv::HOGDescriptor cpu_hog(win_size, block_size, block_stride, cell_size, args.nbins);
// Create HOG descriptors and detectors here
Mat detector = gpu_hog->getDefaultPeopleDetector();
if(args.svm_load) {
std::vector<float> svm_model;
const std::string model_file_name = args.svm;
FileStorage ifs(model_file_name, FileStorage::READ);
if (ifs.isOpened()) {
ifs["svm_detector"] >> svm_model;
} else {
const std::string what =
"could not load model for hog classifier from file: "
+ model_file_name;
throw std::runtime_error(what);
}
gpu_hog->setSVMDetector(detector);
cpu_hog.setSVMDetector(detector);
// check if the variables are initialized
if (svm_model.empty()) {
const std::string what =
"HoG classifier: svm model could not be loaded from file"
+ model_file_name;
throw std::runtime_error(what);
}
gpu_hog->setSVMDetector(svm_model);
cpu_hog.setSVMDetector(svm_model);
} else {
// Create HOG descriptors and detectors here
Mat detector = gpu_hog->getDefaultPeopleDetector();
gpu_hog->setSVMDetector(detector);
cpu_hog.setSVMDetector(detector);
}
cout << "gpusvmDescriptorSize : " << gpu_hog->getDescriptorSize()
<< endl;
cout << "cpusvmDescriptorSize : " << cpu_hog.getDescriptorSize()
<< endl;
while (running)
{
VideoCapture vc;
Mat frame;
vector<String> filenames;
unsigned int count = 1;
if (args.src_is_video)
{
@@ -273,6 +340,14 @@ void App::run()
throw runtime_error(string("can't open video file: " + args.src));
vc >> frame;
}
else if (args.src_is_folder) {
String folder = args.src;
cout << folder << endl;
glob(folder, filenames);
frame = imread(filenames[count]); // 0 --> .gitignore
if (!frame.data)
cerr << "Problem loading image from folder!!!" << endl;
}
else if (args.src_is_camera)
{
vc.open(args.camera_id);
@@ -327,7 +402,7 @@ void App::run()
{
cpu_hog.nlevels = nlevels;
cpu_hog.detectMultiScale(img, found, hit_threshold, win_stride,
Size(0, 0), scale, gr_threshold);
Size(0, 0), scale, gr_threshold);
}
hogWorkEnd();
@@ -342,11 +417,20 @@ void App::run()
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);
putText(img_to_show, "FPS HOG: " + 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);
if (args.src_is_video || args.src_is_camera) vc >> frame;
if (args.src_is_folder) {
count++;
if (count < filenames.size()) {
frame = imread(filenames[count]);
} else {
Mat empty;
frame = empty;
}
}
workEnd();