f85a3c1f5ad75457fcdc89323b5826ad4edf5878
[platform/upstream/opencv.git] / apps / haartraining / haartraining.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 /*
43  * haartraining.cpp
44  *
45  * Train cascade classifier
46  */
47
48 #include <cstdio>
49 #include <cstring>
50 #include <cstdlib>
51
52 using namespace std;
53
54 #include "cvhaartraining.h"
55
56 int main( int argc, char* argv[] )
57 {
58     int i = 0;
59     char* nullname = (char*)"(NULL)";
60
61     char* vecname = NULL;
62     char* dirname = NULL;
63     char* bgname  = NULL;
64
65     bool bg_vecfile = false;
66     int npos    = 2000;
67     int nneg    = 2000;
68     int nstages = 14;
69     int mem     = 200;
70     int nsplits = 1;
71     float minhitrate     = 0.995F;
72     float maxfalsealarm  = 0.5F;
73     float weightfraction = 0.95F;
74     int mode         = 0;
75     int symmetric    = 1;
76     int equalweights = 0;
77     int width  = 24;
78     int height = 24;
79     const char* boosttypes[] = { "DAB", "RAB", "LB", "GAB" };
80     int boosttype = 3;
81     const char* stumperrors[] = { "misclass", "gini", "entropy" };
82     int stumperror = 0;
83     int maxtreesplits = 0;
84     int minpos = 500;
85
86     if( argc == 1 )
87     {
88         printf( "Usage: %s\n  -data <dir_name>\n"
89                 "  -vec <vec_file_name>\n"
90                 "  -bg <background_file_name>\n"
91                 "  [-bg-vecfile]\n"
92                 "  [-npos <number_of_positive_samples = %d>]\n"
93                 "  [-nneg <number_of_negative_samples = %d>]\n"
94                 "  [-nstages <number_of_stages = %d>]\n"
95                 "  [-nsplits <number_of_splits = %d>]\n"
96                 "  [-mem <memory_in_MB = %d>]\n"
97                 "  [-sym (default)] [-nonsym]\n"
98                 "  [-minhitrate <min_hit_rate = %f>]\n"
99                 "  [-maxfalsealarm <max_false_alarm_rate = %f>]\n"
100                 "  [-weighttrimming <weight_trimming = %f>]\n"
101                 "  [-eqw]\n"
102                 "  [-mode <BASIC (default) | CORE | ALL>]\n"
103                 "  [-w <sample_width = %d>]\n"
104                 "  [-h <sample_height = %d>]\n"
105                 "  [-bt <DAB | RAB | LB | GAB (default)>]\n"
106                 "  [-err <misclass (default) | gini | entropy>]\n"
107                 "  [-maxtreesplits <max_number_of_splits_in_tree_cascade = %d>]\n"
108                 "  [-minpos <min_number_of_positive_samples_per_cluster = %d>]\n",
109                 argv[0], npos, nneg, nstages, nsplits, mem,
110                 minhitrate, maxfalsealarm, weightfraction, width, height,
111                 maxtreesplits, minpos );
112
113         return 0;
114     }
115
116     for( i = 1; i < argc; i++ )
117     {
118         if( !strcmp( argv[i], "-data" ) )
119         {
120             dirname = argv[++i];
121         }
122         else if( !strcmp( argv[i], "-vec" ) )
123         {
124             vecname = argv[++i];
125         }
126         else if( !strcmp( argv[i], "-bg" ) )
127         {
128             bgname = argv[++i];
129         }
130         else if( !strcmp( argv[i], "-bg-vecfile" ) )
131         {
132             bg_vecfile = true;
133         }
134         else if( !strcmp( argv[i], "-npos" ) )
135         {
136             npos = atoi( argv[++i] );
137         }
138         else if( !strcmp( argv[i], "-nneg" ) )
139         {
140             nneg = atoi( argv[++i] );
141         }
142         else if( !strcmp( argv[i], "-nstages" ) )
143         {
144             nstages = atoi( argv[++i] );
145         }
146         else if( !strcmp( argv[i], "-nsplits" ) )
147         {
148             nsplits = atoi( argv[++i] );
149         }
150         else if( !strcmp( argv[i], "-mem" ) )
151         {
152             mem = atoi( argv[++i] );
153         }
154         else if( !strcmp( argv[i], "-sym" ) )
155         {
156             symmetric = 1;
157         }
158         else if( !strcmp( argv[i], "-nonsym" ) )
159         {
160             symmetric = 0;
161         }
162         else if( !strcmp( argv[i], "-minhitrate" ) )
163         {
164             minhitrate = (float) atof( argv[++i] );
165         }
166         else if( !strcmp( argv[i], "-maxfalsealarm" ) )
167         {
168             maxfalsealarm = (float) atof( argv[++i] );
169         }
170         else if( !strcmp( argv[i], "-weighttrimming" ) )
171         {
172             weightfraction = (float) atof( argv[++i] );
173         }
174         else if( !strcmp( argv[i], "-eqw" ) )
175         {
176             equalweights = 1;
177         }
178         else if( !strcmp( argv[i], "-mode" ) )
179         {
180             char* tmp = argv[++i];
181
182             if( !strcmp( tmp, "CORE" ) )
183             {
184                 mode = 1;
185             }
186             else if( !strcmp( tmp, "ALL" ) )
187             {
188                 mode = 2;
189             }
190             else
191             {
192                 mode = 0;
193             }
194         }
195         else if( !strcmp( argv[i], "-w" ) )
196         {
197             width = atoi( argv[++i] );
198         }
199         else if( !strcmp( argv[i], "-h" ) )
200         {
201             height = atoi( argv[++i] );
202         }
203         else if( !strcmp( argv[i], "-bt" ) )
204         {
205             i++;
206             if( !strcmp( argv[i], boosttypes[0] ) )
207             {
208                 boosttype = 0;
209             }
210             else if( !strcmp( argv[i], boosttypes[1] ) )
211             {
212                 boosttype = 1;
213             }
214             else if( !strcmp( argv[i], boosttypes[2] ) )
215             {
216                 boosttype = 2;
217             }
218             else
219             {
220                 boosttype = 3;
221             }
222         }
223         else if( !strcmp( argv[i], "-err" ) )
224         {
225             i++;
226             if( !strcmp( argv[i], stumperrors[0] ) )
227             {
228                 stumperror = 0;
229             }
230             else if( !strcmp( argv[i], stumperrors[1] ) )
231             {
232                 stumperror = 1;
233             }
234             else
235             {
236                 stumperror = 2;
237             }
238         }
239         else if( !strcmp( argv[i], "-maxtreesplits" ) )
240         {
241             maxtreesplits = atoi( argv[++i] );
242         }
243         else if( !strcmp( argv[i], "-minpos" ) )
244         {
245             minpos = atoi( argv[++i] );
246         }
247     }
248
249     printf( "Data dir name: %s\n", ((dirname == NULL) ? nullname : dirname ) );
250     printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) );
251     printf( "BG  file name: %s, is a vecfile: %s\n", ((bgname == NULL) ? nullname : bgname ), bg_vecfile ? "yes" : "no" );
252     printf( "Num pos: %d\n", npos );
253     printf( "Num neg: %d\n", nneg );
254     printf( "Num stages: %d\n", nstages );
255     printf( "Num splits: %d (%s as weak classifier)\n", nsplits,
256         (nsplits == 1) ? "stump" : "tree" );
257     printf( "Mem: %d MB\n", mem );
258     printf( "Symmetric: %s\n", (symmetric) ? "TRUE" : "FALSE" );
259     printf( "Min hit rate: %f\n", minhitrate );
260     printf( "Max false alarm rate: %f\n", maxfalsealarm );
261     printf( "Weight trimming: %f\n", weightfraction );
262     printf( "Equal weights: %s\n", (equalweights) ? "TRUE" : "FALSE" );
263     printf( "Mode: %s\n", ( (mode == 0) ? "BASIC" : ( (mode == 1) ? "CORE" : "ALL") ) );
264     printf( "Width: %d\n", width );
265     printf( "Height: %d\n", height );
266     //printf( "Max num of precalculated features: %d\n", numprecalculated );
267     printf( "Applied boosting algorithm: %s\n", boosttypes[boosttype] );
268     printf( "Error (valid only for Discrete and Real AdaBoost): %s\n",
269             stumperrors[stumperror] );
270
271     printf( "Max number of splits in tree cascade: %d\n", maxtreesplits );
272     printf( "Min number of positive samples per cluster: %d\n", minpos );
273
274     cvCreateTreeCascadeClassifier( dirname, vecname, bgname,
275                                npos, nneg, nstages, mem,
276                                nsplits,
277                                minhitrate, maxfalsealarm, weightfraction,
278                                mode, symmetric,
279                                equalweights, width, height,
280                                boosttype, stumperror,
281                                maxtreesplits, minpos, bg_vecfile );
282
283     return 0;
284 }