38 #include <opencv/cxcore.h> 39 #include <opencv/cv.h> 40 #include <opencv/ml.h> 42 #include <people_msgs/PositionMeasurement.h> 43 #include <sensor_msgs/LaserScan.h> 78 printf(
"Loading positive training data from file: %s\n", file);
81 printf(
"Loading negative training data from file: %s\n", file);
84 printf(
"Loading test data from file: %s\n", file);
90 ros::record::Player p;
120 std::vector< std::vector<float> >* data = (std::vector< std::vector<float> >*)(n);
122 if (mask_count_++ < 20)
132 for (std::list<laser_processor::SampleSet*>::iterator i = processor.
getClusters().begin();
141 int sample_size = pos_data_.size() + neg_data_.size();
142 feat_count_ = pos_data_[0].size();
144 CvMat* cv_data = cvCreateMat(sample_size, feat_count_, CV_32FC1);
145 CvMat* cv_resp = cvCreateMat(sample_size, 1, CV_32S);
149 for (std::vector< std::vector<float> >::iterator i = pos_data_.begin();
150 i != pos_data_.end();
153 float* data_row =
reinterpret_cast<float*
>(cv_data->data.ptr + cv_data->step * j);
155 data_row[k] = (*i)[k];
157 cv_resp->data.i[j] = 1;
162 for (std::vector< std::vector<float> >::iterator i = neg_data_.begin();
163 i != neg_data_.end();
166 float* data_row =
reinterpret_cast<float*
>(cv_data->data.ptr + cv_data->step * j);
168 data_row[k] = (*i)[k];
170 cv_resp->data.i[j] = -1;
174 CvMat* var_type = cvCreateMat(1, feat_count_ + 1, CV_8U);
175 cvSet(var_type, cvScalarAll(CV_VAR_ORDERED));
176 cvSetReal1D(var_type, feat_count_, CV_VAR_CATEGORICAL);
178 float priors[] = {1.0, 1.0};
180 CvRTParams fparam(8, 20, 0,
false, 10, priors,
false, 5, 50, 0.001
f, CV_TERMCRIT_ITER);
181 fparam.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 0.1);
183 forest.train(cv_data, CV_ROW_SAMPLE, cv_resp, 0, 0, var_type, 0,
187 cvReleaseMat(&cv_data);
188 cvReleaseMat(&cv_resp);
189 cvReleaseMat(&var_type);
194 CvMat* tmp_mat = cvCreateMat(1, feat_count_, CV_32FC1);
198 for (std::vector< std::vector<float> >::iterator i = pos_data_.begin();
199 i != pos_data_.end();
203 tmp_mat->data.fl[k] = static_cast<float>((*i)[k]);
204 if (forest.predict(tmp_mat) > 0)
211 for (std::vector< std::vector<float> >::iterator i = neg_data_.begin();
212 i != neg_data_.end();
216 tmp_mat->data.fl[k] = static_cast<float>((*i)[k]);
217 if (forest.predict(tmp_mat) < 0)
224 for (std::vector< std::vector<float> >::iterator i = test_data_.begin();
225 i != test_data_.end();
229 tmp_mat->data.fl[k] = static_cast<float>((*i)[k]);
230 if (forest.predict(tmp_mat) > 0)
235 printf(
" Pos train set: %d/%d %g\n", pos_right, pos_total, static_cast<float>(pos_right) / pos_total);
236 printf(
" Neg train set: %d/%d %g\n", neg_right, neg_total, static_cast<float>(neg_right) / neg_total);
237 printf(
" Test set: %d/%d %g\n", test_right, test_total, static_cast<float>(test_right) / test_total);
239 cvReleaseMat(&tmp_mat);
248 int main(
int argc,
char **argv)
257 printf(
"Loading data...\n");
258 for (
int i = 1; i < argc; i++)
260 if (!strcmp(argv[i],
"--train"))
262 else if (!strcmp(argv[i],
"--neg"))
264 else if (!strcmp(argv[i],
"--test"))
266 else if (!strcmp(argv[i],
"--save"))
269 strncpy(save_file, argv[i], 100);
276 printf(
"Training classifier...\n");
279 printf(
"Evlauating classifier...\n");
282 if (strlen(save_file) > 0)
284 printf(
"Saving classifier as: %s\n", save_file);
std::vector< std::vector< float > > test_data_
std::vector< std::vector< float > > neg_data_
std::vector< float > calcLegFeatures(laser_processor::SampleSet *cluster, const sensor_msgs::LaserScan &scan)
void loadCb(std::string name, sensor_msgs::LaserScan *scan, ros::Time t, ros::Time t_no_use, void *n)
void splitConnected(float thresh)
laser_processor::ScanMask mask_
std::list< SampleSet * > & getClusters()
int main(int argc, char **argv)
void loadData(LoadType load, char *file)
void removeLessThan(uint32_t num)
A mask for filtering out Samples based on range.
void addScan(sensor_msgs::LaserScan &scan)
std::vector< std::vector< float > > pos_data_