8 #if CV_MAJOR_VERSION < 3 9 : supportVectorMachine_(
new cv::SVM)
12 #if CV_MAJOR_VERSION >= 3 13 this->supportVectorMachine_ = cv::ml::SVM::create();
15 nh_.getParam(
"dataset_path", this->dataset_path_);
16 nh_.getParam(
"object_dataset_filename", this->object_dataset_filename_);
17 nh_.param<std::string>(
"object_dataset_topic", this->object_dataset_topic_,
"/dataset/roi");
18 nh_.getParam(
"nonobject_dataset_filename", this->nonobject_dataset_filename_);
19 nh_.param<std::string>(
"nonobject_dataset_topic", this->nonobject_dataset_topic_,
"/dataset/background/roi");
20 nh_.getParam(
"classifier_name", this->trained_classifier_name_);
21 nh_.getParam(
"swindow_x", this->swindow_x_);
22 nh_.getParam(
"swindow_y", this->swindow_y_);
23 nh_.param<std::string>(
"manifest_filename", this->manifest_filename_,
"sliding_window_trainer_manifest.xml");
26 std::string pfilename = dataset_path_ + this->object_dataset_filename_;
27 std::string nfilename = dataset_path_ + this->nonobject_dataset_filename_;
28 trainObjectClassifier(pfilename, nfilename);
29 ROS_INFO(
"--Trained Successfully..");
32 std::string manifest_filename = this->manifest_filename_;
33 cv::FileStorage fs(manifest_filename, cv::FileStorage::WRITE);
34 this->writeTrainingManifestToDirectory(fs);
37 cv::destroyAllWindows();
42 std::string pfilename, std::string nfilename)
47 this->
readDataset(pfilename, topic_name, featureMD, labelMD,
true, 1);
48 ROS_INFO(
"Info: Total Object Sample: %d", featureMD.rows);
51 this->
readDataset(nfilename, topic_name, featureMD, labelMD,
true, -1);
52 ROS_INFO(
"Info: Total Training Features: %d", featureMD.rows);
58 }
catch(std::exception &e) {
59 ROS_ERROR(
"--ERROR: PLEASE CHECK YOUR DATA \n%s", e.what());
65 std::string
filename, std::string topic_name, cv::Mat &featureMD,
66 cv::Mat &labelMD,
bool is_usr_label,
const int usr_label) {
71 ROS_INFO(
"Bag Found and Opened Successfully...");
72 std::vector<std::string>
topics;
73 topics.push_back(std::string(topic_name));
77 sensor_msgs::Image>();
80 if (cv_ptr->image.data) {
81 cv::Mat image = cv_ptr->image.clone();
82 float label =
static_cast<float>(usr_label);
83 labelMD.push_back(label);
85 cv::imshow(
"image", image);
93 ROS_ERROR(
"ERROR: Bag File:%s not found..\n%s",
94 filename.c_str(), e.what());
103 cv::Mat &
img, cv::Mat &featureMD) {
104 ROS_INFO(
"--EXTRACTING IMAGE FEATURES.");
110 hsv_feature = hsv_feature.reshape(1, 1);
113 featureMD.push_back(_feature);
115 cv::imshow(
"image", img);
120 const cv::Mat &featureMD,
const cv::Mat &labelMD)
123 #if CV_MAJOR_VERSION >= 3 133 cv::TermCriteria term_crit;
134 term_crit.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
135 term_crit.maxCount = 1e6;
136 term_crit.epsilon = 1e-6;
138 cv::ml::ParamGrid paramGrid = cv::ml::ParamGrid();
139 paramGrid.minVal = 0;
140 paramGrid.maxVal = 0;
141 paramGrid.logStep = 1;
143 cv::Ptr<cv::ml::TrainData>
train = cv::ml::TrainData::create(featureMD, cv::ml::ROW_SAMPLE, labelMD, cv::Mat(), cv::Mat());
146 paramGrid, cv::ml::SVM::getDefaultGrid(cv::ml::SVM::GAMMA),
147 cv::ml::SVM::getDefaultGrid(cv::ml::SVM::P),
148 cv::ml::SVM::getDefaultGrid(cv::ml::SVM::NU),
149 cv::ml::SVM::getDefaultGrid(cv::ml::SVM::COEF),
150 cv::ml::SVM::getDefaultGrid(cv::ml::SVM::DEGREE),
153 cv::SVMParams svm_param = cv::SVMParams();
154 svm_param.svm_type = cv::SVM::NU_SVC;
155 svm_param.kernel_type = cv::SVM::RBF;
156 svm_param.degree = 0.0;
157 svm_param.gamma = 0.90;
158 svm_param.coef0 = 0.50;
162 svm_param.class_weights =
NULL;
163 svm_param.term_crit.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
164 svm_param.term_crit.max_iter = 1e6;
165 svm_param.term_crit.epsilon = 1e-6;
166 cv::ParamGrid paramGrid = cv::ParamGrid();
167 paramGrid.min_val = 0;
168 paramGrid.max_val = 0;
174 (featureMD, labelMD, cv::Mat(), cv::Mat(), svm_param, 10,
175 paramGrid, cv::SVM::get_default_grid(cv::SVM::GAMMA),
176 cv::SVM::get_default_grid(cv::SVM::P),
177 cv::SVM::get_default_grid(cv::SVM::NU),
178 cv::SVM::get_default_grid(cv::SVM::COEF),
179 cv::SVM::get_default_grid(cv::SVM::DEGREE),
185 const cv::Mat &mat_1,
const cv::Mat &mat_2,
186 cv::Mat &featureMD,
bool iscolwise)
189 featureMD = cv::Mat(mat_1.rows, (mat_1.cols + mat_2.cols), CV_32F);
190 for (
int i = 0; i < featureMD.rows; i++) {
191 for (
int j = 0; j < mat_1.cols; j++) {
192 featureMD.at<
float>(i, j) = mat_1.at<
float>(i, j);
194 for (
int j = mat_1.cols; j < featureMD.cols; j++) {
195 featureMD.at<
float>(i, j) = mat_2.at<
float>(i, j - mat_1.cols);
199 featureMD = cv::Mat((mat_1.rows + mat_2.rows), mat_1.cols, CV_32F);
200 for (
int i = 0; i < featureMD.cols; i++) {
201 for (
int j = 0; j < mat_1.rows; j++) {
202 featureMD.at<
float>(j, i) = mat_1.at<
float>(j, i);
204 for (
int j = mat_1.rows; j < featureMD.rows; j++) {
205 featureMD.at<
float>(j, i) = mat_2.at<
float>(j - mat_1.rows, i);
214 fs <<
"TrainerInfo" <<
"{";
215 fs <<
"trainer_type" <<
"cv::SVM";
219 fs <<
"FeatureInfo" <<
"{";
224 fs <<
"COLOR_HISTOGRAM" << 1;
227 fs <<
"SlidingWindowInfo" <<
"{";
232 fs <<
"TrainingDatasetDirectoryInfo" <<
"{";
240 cv::Mat &src, cv::Mat &hist,
const int hBin,
const int sBin,
bool is_norm)
246 cv::cvtColor(src, hsv, CV_BGR2HSV);
247 int histSize[] = {hBin, sBin};
248 float h_ranges[] = {0, 180};
249 float s_ranges[] = {0, 256};
250 const float* ranges[] = {h_ranges, s_ranges};
251 int channels[] = {0, 1};
253 &hsv, 1, channels, cv::Mat(), hist, 2, histSize, ranges,
true,
false);
255 cv::normalize(hist, hist, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
260 int main(
int argc,
char *argv[]) {
261 ros::init(argc, argv,
"sliding_window_object_detector_trainer_node");
262 ROS_INFO(
"RUNNING NODELET %s",
"sliding_window_object_detector_trainer");
virtual void trainBinaryClassSVM(const cv::Mat &, const cv::Mat &)
SlidingWindowObjectDetectorTrainer()
std::string dataset_path_
std::string nonobject_dataset_topic_
boost::shared_ptr< cv::SVM > supportVectorMachine_
virtual void concatenateCVMat(const cv::Mat &, const cv::Mat &, cv::Mat &, bool=true)
virtual void readDataset(std::string, std::string, cv::Mat &, cv::Mat &, bool=false, const int=0)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string nonobject_dataset_filename_
std::string object_dataset_topic_
virtual void trainObjectClassifier(std::string, std::string)
ROSCPP_DECL void spin(Spinner &spinner)
virtual cv::Mat computeHOG(const cv::Mat &)
boost::shared_ptr< T > instantiate() const
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
boost::shared_ptr< rosbag::Bag > rosbag_
virtual void extractFeatures(cv::Mat &, cv::Mat &)
std::string trained_classifier_name_
virtual void computeHSHistogram(cv::Mat &, cv::Mat &, const int=64, const int=32, bool=true)
void writeTrainingManifestToDirectory(cv::FileStorage &)
int main(int argc, char *argv[])
std::string object_dataset_filename_