1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2013, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
45 #include <test_precomp.hpp>
54 using namespace cv::softcascade;
56 typedef vector<cv::String> svector;
57 class ScaledDataset : public Dataset
60 ScaledDataset(const string& path, const int octave);
62 virtual cv::Mat get(SampleType type, int idx) const;
63 virtual int available(SampleType type) const;
64 virtual ~ScaledDataset();
71 ScaledDataset::ScaledDataset(const string& path, const int oct)
73 cv::glob(path + cv::format("/octave_%d/*.png", oct), pos);
74 cv::glob(path + "/*.png", neg);
76 // Check: files not empty
77 CV_Assert(pos.size() != size_t(0));
78 CV_Assert(neg.size() != size_t(0));
81 cv::Mat ScaledDataset::get(SampleType type, int idx) const
83 const std::string& src = (type == POSITIVE)? pos[idx]: neg[idx];
84 return cv::imread(src);
87 int ScaledDataset::available(SampleType type) const
89 return (int)((type == POSITIVE)? pos.size():neg.size());
92 ScaledDataset::~ScaledDataset(){}
96 TEST(SoftCascade, training)
98 // // 2. check and open output file
99 string outXmlPath = cv::tempfile(".xml");
100 cv::FileStorage fso(outXmlPath, cv::FileStorage::WRITE);
102 ASSERT_TRUE(fso.isOpened());
104 std::vector<int> octaves;
106 octaves.push_back(-1);
107 octaves.push_back(0);
110 fso << "regression-cascade"
112 << "stageType" << "BOOST"
113 << "featureType" << "ICF"
120 for (std::vector<int>::const_iterator it = octaves.begin(); it != octaves.end(); ++it)
124 float octave = powf(2.f, (float)(*it));
125 cv::Size model = cv::Size( cvRound(64 * octave) / shrinkage, cvRound(128 * octave) / shrinkage );
127 cv::Ptr<FeaturePool> pool = FeaturePool::create(model, nfeatures, 10);
128 nfeatures = pool->size();
132 cv::Rect boundingBox = cv::Rect( cvRound(20 * octave), cvRound(20 * octave),
133 cvRound(64 * octave), cvRound(128 * octave));
135 cv::Ptr<ChannelFeatureBuilder> builder = ChannelFeatureBuilder::create("HOG6MagLuv");
136 cv::Ptr<Octave> boost = Octave::create(boundingBox, npositives, nnegatives, *it, shrinkage, builder);
138 std::string path = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/sample_training_set";
139 ScaledDataset dataset(path, *it);
141 if (boost->train(&dataset, pool, 3, 2))
144 boost->setRejectThresholds(thresholds);
145 boost->write(fso, pool, thresholds);
153 cv::FileStorage actual(outXmlPath, cv::FileStorage::READ);
154 cv::FileNode root = actual.getFirstTopLevelNode();
156 cv::FileNode fn = root["octaves"];
157 ASSERT_FALSE(fn.empty());