79d41032ebb130f7db5416d076e0222b782047fa
[platform/upstream/opencv.git] / apps / sft / sft.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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 // Training application for Soft Cascades.
44
45 #include <sft/common.hpp>
46 #include <iostream>
47 #include <sft/dataset.hpp>
48 #include <sft/config.hpp>
49
50 #include <opencv2/core/core_c.h>
51
52 int main(int argc, char** argv)
53 {
54     using namespace sft;
55
56     const string keys =
57         "{help h usage ? |      | print this message              }"
58         "{config c       |      | path to configuration xml       }"
59     ;
60
61     cv::CommandLineParser parser(argc, argv, keys);
62     parser.about("Soft cascade training application.");
63
64     if (parser.has("help"))
65     {
66         parser.printMessage();
67         return 0;
68     }
69
70     if (!parser.check())
71     {
72         parser.printErrors();
73         return 1;
74     }
75
76     string configPath = parser.get<string>("config");
77     if (configPath.empty())
78     {
79         std::cout << "Configuration file is missing or empty. Could not start training." << std::endl;
80         return 0;
81     }
82
83     std::cout << "Read configuration from file " << configPath << std::endl;
84     cv::FileStorage fs(configPath, cv::FileStorage::READ);
85     if(!fs.isOpened())
86     {
87         std::cout << "Configuration file " << configPath << " can't be opened." << std::endl;
88         return 1;
89     }
90
91     // 1. load config
92     sft::Config cfg;
93     fs["config"] >> cfg;
94     std::cout << std::endl << "Training will be executed for configuration:" << std::endl << cfg << std::endl;
95
96     // 2. check and open output file
97     cv::FileStorage fso(cfg.outXmlPath, cv::FileStorage::WRITE);
98     if(!fso.isOpened())
99     {
100         std::cout << "Training stopped. Output classifier Xml file " << cfg.outXmlPath << " can't be opened." << std::endl;
101         return 1;
102     }
103
104     fso << cfg.cascadeName
105         << "{"
106         << "stageType"   << "BOOST"
107         << "featureType" << cfg.featureType
108         << "octavesNum"  << (int)cfg.octaves.size()
109         << "width"       << cfg.modelWinSize.width
110         << "height"      << cfg.modelWinSize.height
111         << "shrinkage"   << cfg.shrinkage
112         << "octaves"     << "[";
113
114     // 3. Train all octaves
115     for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it)
116     {
117         // a. create random feature pool
118         int nfeatures  = cfg.poolSize;
119         cv::Size model = cfg.model(it);
120         std::cout << "Model " << model << std::endl;
121
122         int nchannels = (cfg.featureType == "HOG6MagLuv") ? 10: 8;
123
124         std::cout << "number of feature channels is " << nchannels << std::endl;
125
126         cv::Ptr<cv::FeaturePool> pool = cv::FeaturePool::create(model, nfeatures, nchannels);
127         nfeatures = pool->size();
128
129
130         int npositives = cfg.positives;
131         int nnegatives = cfg.negatives;
132         int shrinkage  = cfg.shrinkage;
133         cv::Rect boundingBox = cfg.bbox(it);
134         std::cout << "Object bounding box" << boundingBox << std::endl;
135
136         typedef cv::Octave Octave;
137
138         cv::Ptr<cv::ChannelFeatureBuilder> builder = cv::ChannelFeatureBuilder::create(cfg.featureType);
139         std::cout << "Channel builder " << builder->info()->name() << std::endl;
140         cv::Ptr<Octave> boost = Octave::create(boundingBox, npositives, nnegatives, *it, shrinkage, builder);
141
142         std::string path = cfg.trainPath;
143         sft::ScaledDataset dataset(path, *it);
144
145         if (boost->train(&dataset, pool, cfg.weaks, cfg.treeDepth))
146         {
147             CvFileStorage* fout = cvOpenFileStorage(cfg.resPath(it).c_str(), 0, CV_STORAGE_WRITE);
148             boost->write(fout, cfg.cascadeName);
149
150             cvReleaseFileStorage( &fout);
151
152             cv::Mat thresholds;
153             boost->setRejectThresholds(thresholds);
154
155             boost->write(fso, pool, thresholds);
156
157             cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE);
158             tfs << "thresholds" << thresholds;
159
160             std::cout << "Octave " << *it << " was successfully trained..." << std::endl;
161         }
162     }
163
164     fso << "]" << "}";
165     fso.release();
166     std::cout << "Training complete..." << std::endl;
167     return 0;
168 }