@@ -26,9 +26,14 @@ using cv::imread;
2626#include < opencv2/core/utility.hpp>
2727using cv::CommandLineParser;
2828using cv::FileStorage;
29+ #include < opencv2/core/utility.hpp>
30+
31+ #include < ctime> // std::time
32+ #include < cstdlib> // std::rand, std::srand
2933
3034#include < opencv2/xobjdetect.hpp>
3135
36+
3237using cv::xobjdetect::ICFDetectorParams;
3338using cv::xobjdetect::ICFDetector;
3439using cv::xobjdetect::WaldBoost;
@@ -46,8 +51,11 @@ static bool read_model_size(const char *str, int *rows, int *cols)
4651 return true ;
4752}
4853
54+ static int randomPred (int i) { return std::rand ()%i;}
55+
4956int main (int argc, char *argv[])
5057{
58+
5159 const string keys =
5260 " {help | | print this message}"
5361 " {pos_path | pos | path to training object samples}"
@@ -57,8 +65,15 @@ int main(int argc, char *argv[])
5765 " {weak_count | 100 | number of weak classifiers in cascade}"
5866 " {model_size | 40x40 | model size in pixels}"
5967 " {model_filename | model.xml | filename for saving model}"
68+ " {features_type | icf | features type, \" icf\" or \" acf\" }"
69+ " {alpha | 0.02 | alpha value}"
70+ " {is_grayscale | false | read the image as grayscale}"
71+ " {use_fast_log | false | use fast log function}"
72+ " {limit_ps | -1 | limit to positive samples (-1 means all)}"
73+ " {limit_bg | -1 | limit to negative samples (-1 means all)}"
6074 ;
6175
76+
6277 CommandLineParser parser (argc, argv, keys);
6378 parser.about (" FCW trainer" );
6479
@@ -76,15 +91,22 @@ int main(int argc, char *argv[])
7691 params.feature_count = parser.get <int >(" feature_count" );
7792 params.weak_count = parser.get <int >(" weak_count" );
7893 params.bg_per_image = parser.get <int >(" bg_per_image" );
79-
94+ params.features_type = parser.get <string>(" features_type" );
95+ params.alpha = parser.get <float >(" alpha" );
96+ params.is_grayscale = parser.get <bool >(" is_grayscale" );
97+ params.use_fast_log = parser.get <bool >(" use_fast_log" );
98+
99+ int limit_ps = parser.get <int >(" limit_ps" );
100+ int limit_bg = parser.get <int >(" limit_bg" );
101+
80102 string model_size = parser.get <string>(" model_size" );
81103 if ( !read_model_size (model_size.c_str (), ¶ms.model_n_rows ,
82104 ¶ms.model_n_cols ) )
83105 {
84106 cerr << " Error reading model size from `" << model_size << " `" << endl;
85107 return 1 ;
86108 }
87-
109+
88110 if ( params.feature_count <= 0 )
89111 {
90112 cerr << " feature_count must be positive number" << endl;
@@ -97,20 +119,67 @@ int main(int argc, char *argv[])
97119 return 1 ;
98120 }
99121
100- if ( params.bg_per_image <= 0 )
122+ if ( params.features_type != " icf " && params. features_type != " acf " )
101123 {
102- cerr << " bg_per_image must be positive number" << endl;
124+ cerr << " features_type must be \" icf\" or \" acf\" " << endl;
125+ return 1 ;
126+ }
127+ if ( params.alpha <= 0 )
128+ {
129+ cerr << " alpha must be positive float number" << endl;
103130 return 1 ;
104131 }
105-
106132 if ( !parser.check () )
107133 {
108134 parser.printErrors ();
109135 return 1 ;
110136 }
137+
138+ std::vector<cv::String> pos_filenames;
139+ glob (pos_path, pos_filenames);
140+
141+ std::vector<cv::String> bg_filenames;
142+ glob (bg_path, bg_filenames);
143+
144+ if (limit_ps != -1 && (int )pos_filenames.size () > limit_ps)
145+ pos_filenames.erase (pos_filenames.begin ()+limit_ps, pos_filenames.end ());
146+ if (limit_bg != -1 && (int )bg_filenames.size () > limit_bg)
147+ bg_filenames.erase (bg_filenames.begin ()+limit_bg, bg_filenames.end ());
148+
149+ // random pick input images
150+ bool random_shuffle = false ;
151+ if (random_shuffle)
152+ {
153+ std::srand ( unsigned ( std::time (0 ) ) );
154+ std::random_shuffle ( pos_filenames.begin (), pos_filenames.end (), randomPred );
155+ std::random_shuffle ( bg_filenames.begin (), bg_filenames.end (), randomPred );
156+ }
157+
158+ int samples_size = (int )((params.bg_per_image * bg_filenames.size ()) + pos_filenames.size ());
159+ int features_size = params.feature_count ;
160+ int max_features_allowed = (int )(INT_MAX/(sizeof (int )* samples_size));
161+ int max_samples_allowed = (int )(INT_MAX/(sizeof (int )* features_size));
162+ int total_samples = (int )((params.bg_per_image * bg_filenames.size ()) + pos_filenames.size ());
163+
164+
165+ if (total_samples >max_samples_allowed)
166+ {
167+ CV_Error_ (1 , (" exceeded maximum number of samples. Maximum number of samples with %d features is %d, you have %d (%d positive samples + (%d bg * %d bg_per_image))\n " ,features_size,max_samples_allowed,total_samples,pos_filenames.size (),bg_filenames.size (),params.bg_per_image ));
168+ }
169+
170+ if (params.feature_count >max_features_allowed)
171+ {
172+ CV_Error_ (1 , (" exceeded maximum number of features. Maximum number of features with %d samples is %d, you have %d\n " ,samples_size,max_features_allowed, features_size ));
173+ }
174+
175+ std::cout<<pos_filenames.size ()<<std::endl;
176+ std::cout<<bg_filenames.size ()<<std::endl;
177+
178+ ICFDetector detector;
179+
180+
181+ detector.train (pos_filenames, bg_filenames, params);
111182
112- ICFDetector detector;
113- detector.train (pos_path, bg_path, params);
114183 FileStorage fs (model_filename, FileStorage::WRITE);
115184 fs << " icfdetector" ;
116185 detector.write (fs);
0 commit comments