Update the changelog
[opencv] / apps / haartraining / src / 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 <stdio.h>
49 #include <string.h>
50 #include <stdlib.h>
51
52 #include <cvhaartraining.h>
53
54 int main( int argc, char* argv[] )
55 {
56     int i = 0;
57     char* nullname = "(NULL)";
58     
59     char* vecname = NULL;
60     char* dirname = NULL;
61     char* bgname  = NULL;
62
63     int npos    = 2000;
64     int nneg    = 2000;
65     int nstages = 14;
66     int mem     = 200;
67     int numprecalculated = 0;
68     int nsplits = 1;
69     float minhitrate     = 0.995F;
70     float maxfalsealarm  = 0.5F;
71     float weightfraction = 0.95F;
72     int mode         = 0;
73     int symmetric    = 1;
74     int equalweights = 0;
75     int width  = 24;
76     int height = 24;
77     const char* boosttypes[] = { "DAB", "RAB", "LB", "GAB" };
78     int boosttype = 3;
79     const char* stumperrors[] = { "misclass", "gini", "entropy" };
80     int stumperror = 0;
81     int maxtreesplits = 0;
82     int minpos = 500;
83
84     if( argc == 1 )
85     {
86         printf( "Usage: %s\n  -data <dir_name>\n"
87                 "  -vec <vec_file_name>\n"
88                 "  -bg <background_file_name>\n"
89                 "  [-npos <number_of_positive_samples = %d>]\n"
90                 "  [-nneg <number_of_negative_samples = %d>]\n"
91                 "  [-nstages <number_of_stages = %d>]\n"
92                 "  [-nsplits <number_of_splits = %d>]\n"
93                 "  [-mem <memory_in_MB = %d>]\n"
94                 "  [-sym (default)] [-nonsym]\n"
95                 "  [-minhitrate <min_hit_rate = %f>]\n"
96                 "  [-maxfalsealarm <max_false_alarm_rate = %f>]\n"
97                 "  [-weighttrimming <weight_trimming = %f>]\n"
98                 "  [-eqw]\n"
99                 "  [-mode <BASIC (default) | CORE | ALL>]\n"
100                 "  [-w <sample_width = %d>]\n"
101                 "  [-h <sample_height = %d>]\n"
102                 "  [-bt <DAB | RAB | LB | GAB (default)>]\n"
103                 "  [-err <misclass (default) | gini | entropy>]\n"
104                 "  [-maxtreesplits <max_number_of_splits_in_tree_cascade = %d>]\n"
105                 "  [-minpos <min_number_of_positive_samples_per_cluster = %d>]\n",
106                 argv[0], npos, nneg, nstages, nsplits, mem,
107                 minhitrate, maxfalsealarm, weightfraction, width, height,
108                 maxtreesplits, minpos );
109         
110         return 0;
111     }
112
113     for( i = 1; i < argc; i++ )
114     {
115         if( !strcmp( argv[i], "-data" ) )
116         {
117             dirname = argv[++i];
118         }
119         else if( !strcmp( argv[i], "-vec" ) )
120         {
121             vecname = argv[++i];
122         }
123         else if( !strcmp( argv[i], "-bg" ) )
124         {
125             bgname = argv[++i];
126         }
127         else if( !strcmp( argv[i], "-npos" ) )
128         {
129             npos = atoi( argv[++i] );
130         }
131         else if( !strcmp( argv[i], "-nneg" ) )
132         {
133             nneg = atoi( argv[++i] );
134         }
135         else if( !strcmp( argv[i], "-nstages" ) )
136         {
137             nstages = atoi( argv[++i] );
138         }
139         else if( !strcmp( argv[i], "-nsplits" ) )
140         {
141             nsplits = atoi( argv[++i] );
142         }
143         else if( !strcmp( argv[i], "-mem" ) )
144         {
145             mem = atoi( argv[++i] );
146         }
147         else if( !strcmp( argv[i], "-sym" ) )
148         {
149             symmetric = 1;
150         }
151         else if( !strcmp( argv[i], "-nonsym" ) )
152         {
153             symmetric = 0;
154         }
155         else if( !strcmp( argv[i], "-minhitrate" ) )
156         {
157             minhitrate = (float) atof( argv[++i] );
158         }
159         else if( !strcmp( argv[i], "-maxfalsealarm" ) )
160         {
161             maxfalsealarm = (float) atof( argv[++i] );
162         }
163         else if( !strcmp( argv[i], "-weighttrimming" ) )
164         {
165             weightfraction = (float) atof( argv[++i] );
166         }
167         else if( !strcmp( argv[i], "-eqw" ) )
168         {
169             equalweights = 1;
170         }
171         else if( !strcmp( argv[i], "-mode" ) )
172         {
173             char* tmp = argv[++i];
174             
175             if( !strcmp( tmp, "CORE" ) )
176             {
177                 mode = 1;
178             }
179             else if( !strcmp( tmp, "ALL" ) )
180             {
181                 mode = 2;
182             }
183             else 
184             {
185                 mode = 0;
186             }
187         }
188         else if( !strcmp( argv[i], "-w" ) )
189         {
190             width = atoi( argv[++i] );
191         }
192         else if( !strcmp( argv[i], "-h" ) )
193         {
194             height = atoi( argv[++i] );
195         }
196         else if( !strcmp( argv[i], "-bt" ) )
197         {
198             i++;
199             if( !strcmp( argv[i], boosttypes[0] ) )
200             {
201                 boosttype = 0;
202             }
203             else if( !strcmp( argv[i], boosttypes[1] ) )
204             {
205                 boosttype = 1;
206             }
207             else if( !strcmp( argv[i], boosttypes[2] ) )
208             {
209                 boosttype = 2;
210             }
211             else
212             {
213                 boosttype = 3;
214             }
215         }
216         else if( !strcmp( argv[i], "-err" ) )
217         {
218             i++;
219             if( !strcmp( argv[i], stumperrors[0] ) )
220             {
221                 stumperror = 0;
222             }
223             else if( !strcmp( argv[i], stumperrors[1] ) )
224             {
225                 stumperror = 1;
226             }
227             else
228             {
229                 stumperror = 2;
230             }
231         }
232         else if( !strcmp( argv[i], "-maxtreesplits" ) )
233         {
234             maxtreesplits = atoi( argv[++i] );
235         }
236         else if( !strcmp( argv[i], "-minpos" ) )
237         {
238             minpos = atoi( argv[++i] );
239         }
240     }
241
242     numprecalculated = (int) ( ((size_t) mem) * ((size_t) 1048576) /
243         ( ((size_t) (npos + nneg)) * (sizeof( float ) + sizeof( short )) ) );
244     
245     printf( "Data dir name: %s\n", ((dirname == NULL) ? nullname : dirname ) );
246     printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) );
247     printf( "BG  file name: %s\n", ((bgname == NULL) ? nullname : bgname ) );
248     printf( "Num pos: %d\n", npos );
249     printf( "Num neg: %d\n", nneg );
250     printf( "Num stages: %d\n", nstages );
251     printf( "Num splits: %d (%s as weak classifier)\n", nsplits,
252         (nsplits == 1) ? "stump" : "tree" );
253     printf( "Mem: %d MB\n", mem );
254     printf( "Symmetric: %s\n", (symmetric) ? "TRUE" : "FALSE" );
255     printf( "Min hit rate: %f\n", minhitrate );
256     printf( "Max false alarm rate: %f\n", maxfalsealarm );
257     printf( "Weight trimming: %f\n", weightfraction );
258     printf( "Equal weights: %s\n", (equalweights) ? "TRUE" : "FALSE" );
259     printf( "Mode: %s\n", ( (mode == 0) ? "BASIC" : ( (mode == 1) ? "CORE" : "ALL") ) );
260     printf( "Width: %d\n", width );
261     printf( "Height: %d\n", height );
262     printf( "Max num of precalculated features: %d\n", numprecalculated );
263     printf( "Applied boosting algorithm: %s\n", boosttypes[boosttype] );
264     printf( "Error (valid only for Discrete and Real AdaBoost): %s\n",
265             stumperrors[stumperror] );
266
267     printf( "Max number of splits in tree cascade: %d\n", maxtreesplits );
268     printf( "Min number of positive samples per cluster: %d\n", minpos );
269
270     cvCreateTreeCascadeClassifier( dirname, vecname, bgname,
271                                npos, nneg, nstages, numprecalculated,
272                                nsplits,
273                                minhitrate, maxfalsealarm, weightfraction,
274                                mode, symmetric,
275                                equalweights, width, height,
276                                boosttype, stumperror,
277                                maxtreesplits, minpos );
278
279     return 0;
280 }