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) 2009, 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.
43 #include "test_precomp.hpp"
47 #ifdef HAVE_CVCONFIG_H
52 #include "tbb/task_scheduler_init.h"
57 const int num_detections = 3;
58 const float true_scores[3] = {-0.383931f, -0.825876f, -0.959934f};
59 const float score_thr = 0.05f;
60 const CvRect true_bounding_boxes[3] = {cvRect(0, 45, 362, 452), cvRect(304, 0, 64, 80), cvRect(236, 0, 108, 59)};
62 class CV_LatentSVMDetectorTest : public cvtest::BaseTest
66 bool isEqual(CvRect r1, CvRect r2, int eps);
69 bool CV_LatentSVMDetectorTest::isEqual(CvRect r1, CvRect r2, int eps)
71 return (std::abs(r1.x - r2.x) <= eps
72 && std::abs(r1.y - r2.y) <= eps
73 && std::abs(r1.width - r2.width) <= eps
74 && std::abs(r1.height - r2.height) <= eps);
77 void CV_LatentSVMDetectorTest::run( int /* start_from */)
79 string img_path = string(ts->get_data_path()) + "latentsvmdetector/cat.png";
80 string model_path = string(ts->get_data_path()) + "latentsvmdetector/models_VOC2007/cat.xml";
85 tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);
86 init.initialize(numThreads);
89 IplImage* image = cvLoadImage(img_path.c_str());
92 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
96 CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_path.c_str());
99 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
100 cvReleaseImage(&image);
104 CvMemStorage* storage = cvCreateMemStorage(0);
105 CvSeq* detections = 0;
106 detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
107 if (detections->total != num_detections)
109 ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
113 ts->set_failed_test_info(cvtest::TS::OK);
114 for (int i = 0; i < detections->total; i++)
116 CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
117 CvRect bounding_box = detection.rect;
118 float score = detection.score;
119 if ((!isEqual(bounding_box, true_bounding_boxes[i], 1)) || (fabs(score - true_scores[i]) > score_thr))
121 ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
129 cvReleaseMemStorage( &storage );
130 cvReleaseLatentSvmDetector( &detector );
131 cvReleaseImage( &image );
134 // Test for c++ version of Latent SVM
136 class LatentSVMDetectorTest : public cvtest::BaseTest
142 static void writeDetections( FileStorage& fs, const string& nodeName, const vector<LatentSvmDetector::ObjectDetection>& detections )
144 fs << nodeName << "[";
145 for( size_t i = 0; i < detections.size(); i++ )
147 const LatentSvmDetector::ObjectDetection& d = detections[i];
148 fs << d.rect.x << d.rect.y << d.rect.width << d.rect.height
149 << d.score << d.classID;
154 static void readDetections( FileStorage fs, const string& nodeName, vector<LatentSvmDetector::ObjectDetection>& detections )
158 FileNode fn = fs.root()[nodeName];
159 FileNodeIterator fni = fn.begin();
160 while( fni != fn.end() )
162 LatentSvmDetector::ObjectDetection d;
163 fni >> d.rect.x >> d.rect.y >> d.rect.width >> d.rect.height
164 >> d.score >> d.classID;
165 detections.push_back( d );
169 static inline bool isEqual( const LatentSvmDetector::ObjectDetection& d1, const LatentSvmDetector::ObjectDetection& d2, int eps, float threshold)
172 std::abs(d1.rect.x - d2.rect.x) <= eps
173 && std::abs(d1.rect.y - d2.rect.y) <= eps
174 && std::abs(d1.rect.width - d2.rect.width) <= eps
175 && std::abs(d1.rect.height - d2.rect.height) <= eps
176 && (d1.classID == d2.classID)
177 && std::abs(d1.score - d2.score) <= threshold
181 std::ostream& operator << (std::ostream& os, const CvRect& r)
183 return (os << "[x=" << r.x << ", y=" << r.y << ", w=" << r.width << ", h=" << r.height << "]");
186 bool compareResults( const vector<LatentSvmDetector::ObjectDetection>& calc, const vector<LatentSvmDetector::ObjectDetection>& valid, int eps, float threshold)
188 if( calc.size() != valid.size() )
191 for( size_t i = 0; i < calc.size(); i++ )
193 const LatentSvmDetector::ObjectDetection& c = calc[i];
194 const LatentSvmDetector::ObjectDetection& v = valid[i];
195 if( !isEqual(c, v, eps, threshold) )
197 std::cerr << "Expected: " << v.rect << " class=" << v.classID << " score=" << v.score << std::endl;
198 std::cerr << "Actual: " << c.rect << " class=" << c.classID << " score=" << c.score << std::endl;
205 void LatentSVMDetectorTest::run( int /* start_from */)
207 string img_path_cat = string(ts->get_data_path()) + "latentsvmdetector/cat.png";
208 string img_path_cars = string(ts->get_data_path()) + "latentsvmdetector/cars.png";
210 string model_path_cat = string(ts->get_data_path()) + "latentsvmdetector/models_VOC2007/cat.xml";
211 string model_path_car = string(ts->get_data_path()) + "latentsvmdetector/models_VOC2007/car.xml";
213 string true_res_path = string(ts->get_data_path()) + "latentsvmdetector/results.xml";
221 Mat image_cat = imread( img_path_cat );
222 Mat image_cars = imread( img_path_cars );
223 if( image_cat.empty() || image_cars.empty() )
225 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
229 // We will test 2 cases:
230 // detector1 - to test case of one class 'cat'
231 // detector12 - to test case of two (several) classes 'cat' and car
234 LatentSvmDetector detector1( vector<string>(1,model_path_cat) );
236 vector<string> models_pathes(2);
237 models_pathes[0] = model_path_cat;
238 models_pathes[1] = model_path_car;
239 LatentSvmDetector detector12( models_pathes );
241 if( detector1.empty() || detector12.empty() || detector12.getClassCount() != 2 )
243 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
247 // 1. Test method detect
249 vector<LatentSvmDetector::ObjectDetection> detections1_cat, detections12_cat, detections12_cars;
250 detector1.detect( image_cat, detections1_cat, 0.5, numThreads );
251 detector12.detect( image_cat, detections12_cat, 0.5, numThreads );
252 detector12.detect( image_cars, detections12_cars, 0.5, numThreads );
255 FileStorage fs( true_res_path, FileStorage::READ );
258 vector<LatentSvmDetector::ObjectDetection> true_detections1_cat, true_detections12_cat, true_detections12_cars;
259 readDetections( fs, "detections1_cat", true_detections1_cat );
260 readDetections( fs, "detections12_cat", true_detections12_cat );
261 readDetections( fs, "detections12_cars", true_detections12_cars );
264 if( !compareResults(detections1_cat, true_detections1_cat, 1, score_thr) )
266 std::cerr << "Results of detector1 are invalid on image cat.png" << std::endl;
267 ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
269 if( !compareResults(detections12_cat, true_detections12_cat, 1, score_thr) )
271 std::cerr << "Results of detector12 are invalid on image cat.png" << std::endl;
272 ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
274 if( !compareResults(detections12_cars, true_detections12_cars, 1, score_thr) )
276 std::cerr << "Results of detector12 are invalid on image cars.png" << std::endl;
277 ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
282 fs.open( true_res_path, FileStorage::WRITE );
285 writeDetections( fs, "detections1_cat", detections1_cat );
286 writeDetections( fs, "detections12_cat", detections12_cat );
287 writeDetections( fs, "detections12_cars", detections12_cars );
290 std::cerr << "File " << true_res_path << " cann't be opened to save test results" << std::endl;
293 // 2. Simple tests of other methods
294 if( detector1.getClassCount() != 1 || detector1.getClassNames()[0] != "cat" )
296 std::cerr << "Incorrect result of method getClassNames() or getClassCount()" << std::endl;
297 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT);
301 if( !detector1.empty() )
303 std::cerr << "There is a bug in method clear() or empty()" << std::endl;
304 ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT);
307 ts->set_failed_test_info( cvtest::TS::OK);
310 TEST(Objdetect_LatentSVMDetector_c, DISABLED_regression) { CV_LatentSVMDetectorTest test; test.safe_run(); }
311 TEST(Objdetect_LatentSVMDetector_cpp, DISABLED_regression) { LatentSVMDetectorTest test; test.safe_run(); }