Merge pull request #40 from shahurik/adas
ADAS: multiple fixes, fcw_train and fcw_detect applicationspull/73/head
commit
47f61f1c51
13 changed files with 919 additions and 449 deletions
@ -1,33 +1,2 @@ |
||||
set(name fcw_train) |
||||
set(the_target opencv_${name}) |
||||
set(the_module opencv_adas) |
||||
|
||||
ocv_check_dependencies(${OPENCV_MODULE_${the_module}_DEPS}) |
||||
|
||||
if(NOT OCV_DEPENDENCIES_FOUND) |
||||
return() |
||||
endif() |
||||
|
||||
project(${the_target}) |
||||
|
||||
ocv_include_directories("${OpenCV_SOURCE_DIR}/include/opencv") |
||||
ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS}) |
||||
|
||||
file(GLOB ${the_target}_SOURCES ${CMAKE_CURRENT_LIST_DIR}/*.cpp) |
||||
|
||||
add_executable(${the_target} ${${the_target}_SOURCES}) |
||||
|
||||
ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS}) |
||||
|
||||
set_target_properties(${the_target} PROPERTIES |
||||
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" |
||||
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} |
||||
RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} |
||||
INSTALL_NAME_DIR lib |
||||
OUTPUT_NAME ${the_target}) |
||||
|
||||
if(ENABLE_SOLUTION_FOLDERS) |
||||
set_target_properties(${the_target} PROPERTIES FOLDER "applications") |
||||
endif() |
||||
|
||||
install(TARGETS ${the_target} RUNTIME DESTINATION bin COMPONENT main) |
||||
add_subdirectory(fcw_train) |
||||
add_subdirectory(fcw_detect) |
||||
|
@ -0,0 +1,35 @@ |
||||
set(name fcw_detect) |
||||
set(the_target opencv_${name}) |
||||
|
||||
set(OPENCV_${the_target}_DEPS opencv_core opencv_imgcodecs opencv_videoio |
||||
opencv_highgui opencv_xobjdetect) |
||||
|
||||
ocv_check_dependencies(${OPENCV_${the_target}_DEPS}) |
||||
|
||||
if(NOT OCV_DEPENDENCIES_FOUND) |
||||
return() |
||||
endif() |
||||
|
||||
project(${the_target}) |
||||
|
||||
ocv_include_directories("${OpenCV_SOURCE_DIR}/include/opencv") |
||||
ocv_include_modules(${OPENCV_${the_target}_DEPS}) |
||||
|
||||
file(GLOB ${the_target}_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) |
||||
|
||||
add_executable(${the_target} ${${the_target}_SOURCES}) |
||||
|
||||
target_link_libraries(${the_target} ${OPENCV_${the_target}_DEPS}) |
||||
|
||||
set_target_properties(${the_target} PROPERTIES |
||||
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" |
||||
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} |
||||
RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} |
||||
INSTALL_NAME_DIR lib |
||||
OUTPUT_NAME ${the_target}) |
||||
|
||||
if(ENABLE_SOLUTION_FOLDERS) |
||||
set_target_properties(${the_target} PROPERTIES FOLDER "applications") |
||||
endif() |
||||
|
||||
install(TARGETS ${the_target} RUNTIME DESTINATION bin COMPONENT main) |
@ -0,0 +1,92 @@ |
||||
#include <string> |
||||
using std::string; |
||||
|
||||
#include <vector> |
||||
using std::vector; |
||||
|
||||
#include <iostream> |
||||
using std::cerr; |
||||
using std::endl; |
||||
|
||||
#include <opencv2/core.hpp> |
||||
using cv::Rect; |
||||
using cv::Size; |
||||
using cv::Mat; |
||||
using cv::Mat_; |
||||
using cv::Vec3b; |
||||
|
||||
#include <opencv2/highgui.hpp> |
||||
using cv::imread; |
||||
using cv::imwrite; |
||||
|
||||
#include <opencv2/core/utility.hpp> |
||||
using cv::CommandLineParser; |
||||
using cv::FileStorage; |
||||
|
||||
#include <opencv2/xobjdetect.hpp> |
||||
using cv::xobjdetect::ICFDetector; |
||||
|
||||
static Mat visualize(const Mat &image, const vector<Rect> &objects) |
||||
{ |
||||
CV_Assert(image.type() == CV_8UC3); |
||||
Mat_<Vec3b> img = image.clone(); |
||||
for( size_t j = 0; j < objects.size(); ++j ) |
||||
{ |
||||
Rect obj = objects[j]; |
||||
int x = obj.x; |
||||
int y = obj.y; |
||||
int width = obj.width; |
||||
int height = obj.height; |
||||
for( int i = y; i <= y + height; ++i ) { |
||||
img(i, x) = Vec3b(255, 0, 0); |
||||
img(i, x + width) = Vec3b(255, 0, 0); |
||||
} |
||||
for( int i = x; i <= x + width; ++i) { |
||||
img(y, i) = Vec3b(255, 0, 0); |
||||
img(y + height, i) = Vec3b(255, 0, 0); |
||||
} |
||||
} |
||||
return img; |
||||
} |
||||
|
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
const string keys = |
||||
"{help | | print this message}" |
||||
"{model_filename | model.xml | filename for reading model}" |
||||
"{image_path | test.png | path to image for detection}" |
||||
"{out_image_path | out.png | path to image for output}" |
||||
"{threshold | 0.0 | threshold for cascade}" |
||||
; |
||||
|
||||
CommandLineParser parser(argc, argv, keys); |
||||
parser.about("FCW detection"); |
||||
|
||||
if( parser.has("help") || argc == 1) |
||||
{ |
||||
parser.printMessage(); |
||||
return 0; |
||||
} |
||||
|
||||
string model_filename = parser.get<string>("model_filename"); |
||||
string image_path = parser.get<string>("image_path"); |
||||
string out_image_path = parser.get<string>("out_image_path"); |
||||
float threshold = parser.get<float>("threshold"); |
||||
|
||||
|
||||
if( !parser.check() ) |
||||
{ |
||||
parser.printErrors(); |
||||
return 1; |
||||
} |
||||
|
||||
ICFDetector detector; |
||||
FileStorage fs(model_filename, FileStorage::READ); |
||||
detector.read(fs["icfdetector"]); |
||||
fs.release(); |
||||
vector<Rect> objects; |
||||
Mat img = imread(image_path); |
||||
detector.detect(img, objects, 1.1f, Size(40, 40), |
||||
Size(300, 300), threshold); |
||||
imwrite(out_image_path, visualize(img, objects)); |
||||
} |
@ -1,195 +0,0 @@ |
||||
#include <cstdio> |
||||
#include <cstring> |
||||
|
||||
#include <string> |
||||
using std::string; |
||||
|
||||
#include <vector> |
||||
using std::vector; |
||||
|
||||
#include <fstream> |
||||
using std::ifstream; |
||||
using std::getline; |
||||
|
||||
#include <sstream> |
||||
using std::stringstream; |
||||
|
||||
#include <iostream> |
||||
using std::cerr; |
||||
using std::endl; |
||||
|
||||
#include <opencv2/core.hpp> |
||||
using cv::Rect; |
||||
|
||||
#include <opencv2/xobjdetect.hpp> |
||||
|
||||
using cv::xobjdetect::ICFDetectorParams; |
||||
using cv::xobjdetect::ICFDetector; |
||||
using cv::xobjdetect::WaldBoost; |
||||
using cv::xobjdetect::WaldBoostParams; |
||||
using cv::Mat; |
||||
|
||||
static bool read_pos_int(const char *str, int *n) |
||||
{ |
||||
int pos = 0; |
||||
if( sscanf(str, "%d%n", n, &pos) != 1 || str[pos] != '\0' || *n <= 0 ) |
||||
{ |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
static bool read_model_size(char *str, int *rows, int *cols) |
||||
{ |
||||
int pos = 0; |
||||
if( sscanf(str, "%dx%d%n", rows, cols, &pos) != 2 || str[pos] != '\0' || |
||||
*rows <= 0 || *cols <= 0) |
||||
{ |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
static bool read_overlap(const char *str, double *overlap) |
||||
{ |
||||
int pos = 0; |
||||
if( sscanf(str, "%lf%n", overlap, &pos) != 1 || str[pos] != '\0' || |
||||
*overlap < 0 || *overlap > 1) |
||||
{ |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
static bool read_labels(const string& path, |
||||
vector<string>& filenames, vector< vector<Rect> >& labels) |
||||
{ |
||||
string labels_path = path + "/gt.txt"; |
||||
string filename, line; |
||||
int x1, y1, x2, y2; |
||||
char delim; |
||||
ifstream ifs(labels_path.c_str()); |
||||
if( !ifs.good() ) |
||||
return false; |
||||
|
||||
while( getline(ifs, line) ) |
||||
{ |
||||
stringstream stream(line); |
||||
stream >> filename; |
||||
filenames.push_back(path + "/" + filename); |
||||
vector<Rect> filename_labels; |
||||
while( stream >> x1 >> y1 >> x2 >> y2 >> delim ) |
||||
{ |
||||
filename_labels.push_back(Rect(x1, y1, x2, y2)); |
||||
} |
||||
labels.push_back(filename_labels); |
||||
filename_labels.clear(); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
|
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
if( argc == 1 ) |
||||
{ |
||||
printf("Usage: %s OPTIONS, where OPTIONS are:\n" |
||||
"\n" |
||||
"--path <path> - path to dir with data and labels\n" |
||||
" (labels should have name gt.txt)\n" |
||||
"\n" |
||||
"--feature_count <count> - number of features to generate\n" |
||||
"\n" |
||||
"--weak_count <count> - number of weak classifiers in cascade\n" |
||||
"\n" |
||||
"--model_size <rowsxcols> - model size in pixels\n" |
||||
"\n" |
||||
"--overlap <measure> - number from [0, 1], means maximum\n" |
||||
" overlap with objects while sampling background\n" |
||||
"\n" |
||||
"--model_filename <path> - filename for saving model\n", |
||||
argv[0]); |
||||
return 0; |
||||
} |
||||
|
||||
string path, model_path; |
||||
ICFDetectorParams params; |
||||
for( int i = 1; i < argc; ++i ) |
||||
{ |
||||
if( !strcmp("--path", argv[i]) ) |
||||
{ |
||||
i += 1; |
||||
path = argv[i]; |
||||
} |
||||
else if( !strcmp("--feature_count", argv[i]) ) |
||||
{ |
||||
i += 1; |
||||
if( !read_pos_int(argv[i], ¶ms.feature_count) ) |
||||
{ |
||||
fprintf(stderr, "Error reading feature count from `%s`\n", |
||||
argv[i]); |
||||
return 1; |
||||
} |
||||
} |
||||
else if( !strcmp("--weak_count", argv[i]) ) |
||||
{ |
||||
i += 1; |
||||
if( !read_pos_int(argv[i], ¶ms.weak_count) ) |
||||
{ |
||||
fprintf(stderr, "Error reading weak count from `%s`\n", |
||||
argv[i]); |
||||
return 1; |
||||
} |
||||
} |
||||
else if( !strcmp("--model_size", argv[i]) ) |
||||
{ |
||||
i += 1; |
||||
if( !read_model_size(argv[i], ¶ms.model_n_rows, |
||||
¶ms.model_n_cols) ) |
||||
{ |
||||
fprintf(stderr, "Error reading model size from `%s`\n", |
||||
argv[i]); |
||||
return 1; |
||||
} |
||||
} |
||||
else if( !strcmp("--overlap", argv[i]) ) |
||||
{ |
||||
i += 1; |
||||
if( !read_overlap(argv[i], ¶ms.overlap) ) |
||||
{ |
||||
fprintf(stderr, "Error reading overlap from `%s`\n", |
||||
argv[i]); |
||||
return 1; |
||||
} |
||||
} |
||||
else if( !strcmp("--model_filename", argv[i]) ) |
||||
{ |
||||
i += 1; |
||||
model_path = argv[i]; |
||||
} |
||||
else |
||||
{ |
||||
fprintf(stderr, "Error: unknown argument `%s`\n", argv[i]); |
||||
return 1; |
||||
} |
||||
|
||||
} |
||||
|
||||
try |
||||
{ |
||||
ICFDetector detector; |
||||
vector<string> filenames; |
||||
vector< vector<Rect> > labels; |
||||
read_labels(path, filenames, labels); |
||||
|
||||
detector.train(filenames, labels, params); |
||||
} |
||||
catch( const char *err ) |
||||
{ |
||||
cerr << err << endl; |
||||
} |
||||
catch( ... ) |
||||
{ |
||||
cerr << "Unknown error\n" << endl; |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
set(name fcw_train) |
||||
set(the_target opencv_${name}) |
||||
|
||||
set(OPENCV_${the_target}_DEPS opencv_core opencv_imgcodecs opencv_videoio |
||||
opencv_highgui opencv_xobjdetect) |
||||
|
||||
ocv_check_dependencies(${OPENCV_${the_target}_DEPS}) |
||||
|
||||
if(NOT OCV_DEPENDENCIES_FOUND) |
||||
return() |
||||
endif() |
||||
|
||||
project(${the_target}) |
||||
|
||||
ocv_include_directories("${OpenCV_SOURCE_DIR}/include/opencv") |
||||
ocv_include_modules(${OPENCV_${the_target}_DEPS}) |
||||
|
||||
file(GLOB ${the_target}_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) |
||||
|
||||
add_executable(${the_target} ${${the_target}_SOURCES}) |
||||
|
||||
target_link_libraries(${the_target} ${OPENCV_${the_target}_DEPS}) |
||||
|
||||
set_target_properties(${the_target} PROPERTIES |
||||
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}" |
||||
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH} |
||||
RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} |
||||
INSTALL_NAME_DIR lib |
||||
OUTPUT_NAME ${the_target}) |
||||
|
||||
if(ENABLE_SOLUTION_FOLDERS) |
||||
set_target_properties(${the_target} PROPERTIES FOLDER "applications") |
||||
endif() |
||||
|
||||
install(TARGETS ${the_target} RUNTIME DESTINATION bin COMPONENT main) |
@ -0,0 +1,118 @@ |
||||
#include <cstdio> |
||||
#include <cstring> |
||||
|
||||
#include <string> |
||||
using std::string; |
||||
|
||||
#include <vector> |
||||
using std::vector; |
||||
|
||||
#include <fstream> |
||||
using std::ifstream; |
||||
using std::getline; |
||||
|
||||
#include <sstream> |
||||
using std::stringstream; |
||||
|
||||
#include <iostream> |
||||
using std::cerr; |
||||
using std::endl; |
||||
|
||||
#include <opencv2/core.hpp> |
||||
using cv::Rect; |
||||
using cv::Size; |
||||
#include <opencv2/highgui.hpp> |
||||
using cv::imread; |
||||
#include <opencv2/core/utility.hpp> |
||||
using cv::CommandLineParser; |
||||
using cv::FileStorage; |
||||
|
||||
#include <opencv2/xobjdetect.hpp> |
||||
|
||||
using cv::xobjdetect::ICFDetectorParams; |
||||
using cv::xobjdetect::ICFDetector; |
||||
using cv::xobjdetect::WaldBoost; |
||||
using cv::xobjdetect::WaldBoostParams; |
||||
using cv::Mat; |
||||
|
||||
static bool read_model_size(const char *str, int *rows, int *cols) |
||||
{ |
||||
int pos = 0; |
||||
if( sscanf(str, "%dx%d%n", rows, cols, &pos) != 2 || str[pos] != '\0' || |
||||
*rows <= 0 || *cols <= 0) |
||||
{ |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
const string keys = |
||||
"{help | | print this message}" |
||||
"{pos_path | pos | path to training object samples}" |
||||
"{bg_path | bg | path to background images}" |
||||
"{bg_per_image | 5 | number of windows to sample per bg image}" |
||||
"{feature_count | 10000 | number of features to generate}" |
||||
"{weak_count | 100 | number of weak classifiers in cascade}" |
||||
"{model_size | 40x40 | model size in pixels}" |
||||
"{model_filename | model.xml | filename for saving model}" |
||||
; |
||||
|
||||
CommandLineParser parser(argc, argv, keys); |
||||
parser.about("FCW trainer"); |
||||
|
||||
if( parser.has("help") || argc == 1) |
||||
{ |
||||
parser.printMessage(); |
||||
return 0; |
||||
} |
||||
|
||||
string pos_path = parser.get<string>("pos_path"); |
||||
string bg_path = parser.get<string>("bg_path"); |
||||
string model_filename = parser.get<string>("model_filename"); |
||||
|
||||
ICFDetectorParams params; |
||||
params.feature_count = parser.get<int>("feature_count"); |
||||
params.weak_count = parser.get<int>("weak_count"); |
||||
params.bg_per_image = parser.get<int>("bg_per_image"); |
||||
|
||||
string model_size = parser.get<string>("model_size"); |
||||
if( !read_model_size(model_size.c_str(), ¶ms.model_n_rows, |
||||
¶ms.model_n_cols) ) |
||||
{ |
||||
cerr << "Error reading model size from `" << model_size << "`" << endl; |
||||
return 1; |
||||
} |
||||
|
||||
if( params.feature_count <= 0 ) |
||||
{ |
||||
cerr << "feature_count must be positive number" << endl; |
||||
return 1; |
||||
} |
||||
|
||||
if( params.weak_count <= 0 ) |
||||
{ |
||||
cerr << "weak_count must be positive number" << endl; |
||||
return 1; |
||||
} |
||||
|
||||
if( params.bg_per_image <= 0 ) |
||||
{ |
||||
cerr << "bg_per_image must be positive number" << endl; |
||||
return 1; |
||||
} |
||||
|
||||
if( !parser.check() ) |
||||
{ |
||||
parser.printErrors(); |
||||
return 1; |
||||
} |
||||
|
||||
ICFDetector detector; |
||||
detector.train(pos_path, bg_path, params); |
||||
FileStorage fs(model_filename, FileStorage::WRITE); |
||||
fs << "icfdetector"; |
||||
detector.write(fs); |
||||
fs.release(); |
||||
} |
Loading…
Reference in new issue