PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/source/3rd_party/opencv/sources/modules/objdetect/test/test_latentsvmdetector.cpp

https://gitlab.com/Ruggero/SparkEngine_Desktop
C++ | 309 lines | 219 code | 40 blank | 50 comment | 38 complexity | 3be515876fa4a893709b0c1d086aadc3 MD5 | raw file
  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) 2009, 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. #include "test_precomp.hpp"
  43. #include <string>
  44. #include "cvconfig.h"
  45. #ifdef HAVE_TBB
  46. #include "tbb/task_scheduler_init.h"
  47. #endif
  48. using namespace cv;
  49. const int num_detections = 3;
  50. const float true_scores[3] = {-0.383931f, -0.825876f, -0.959934f};
  51. const float score_thr = 0.05f;
  52. const CvRect true_bounding_boxes[3] = {cvRect(0, 45, 362, 452), cvRect(304, 0, 64, 80), cvRect(236, 0, 108, 59)};
  53. class CV_LatentSVMDetectorTest : public cvtest::BaseTest
  54. {
  55. protected:
  56. void run(int);
  57. bool isEqual(CvRect r1, CvRect r2, int eps);
  58. };
  59. bool CV_LatentSVMDetectorTest::isEqual(CvRect r1, CvRect r2, int eps)
  60. {
  61. return (std::abs(r1.x - r2.x) <= eps
  62. && std::abs(r1.y - r2.y) <= eps
  63. && std::abs(r1.width - r2.width) <= eps
  64. && std::abs(r1.height - r2.height) <= eps);
  65. }
  66. void CV_LatentSVMDetectorTest::run( int /* start_from */)
  67. {
  68. string img_path = string(ts->get_data_path()) + "latentsvmdetector/cat.png";
  69. string model_path = string(ts->get_data_path()) + "latentsvmdetector/models_VOC2007/cat.xml";
  70. int numThreads = -1;
  71. #ifdef HAVE_TBB
  72. numThreads = 2;
  73. tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);
  74. init.initialize(numThreads);
  75. #endif
  76. IplImage* image = cvLoadImage(img_path.c_str());
  77. if (!image)
  78. {
  79. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  80. return;
  81. }
  82. CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_path.c_str());
  83. if (!detector)
  84. {
  85. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  86. cvReleaseImage(&image);
  87. return;
  88. }
  89. CvMemStorage* storage = cvCreateMemStorage(0);
  90. CvSeq* detections = 0;
  91. detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);
  92. if (detections->total != num_detections)
  93. {
  94. ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
  95. }
  96. else
  97. {
  98. ts->set_failed_test_info(cvtest::TS::OK);
  99. for (int i = 0; i < detections->total; i++)
  100. {
  101. CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );
  102. CvRect bounding_box = detection.rect;
  103. float score = detection.score;
  104. if ((!isEqual(bounding_box, true_bounding_boxes[i], 1)) || (fabs(score - true_scores[i]) > score_thr))
  105. {
  106. ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
  107. break;
  108. }
  109. }
  110. }
  111. #ifdef HAVE_TBB
  112. init.terminate();
  113. #endif
  114. cvReleaseMemStorage( &storage );
  115. cvReleaseLatentSvmDetector( &detector );
  116. cvReleaseImage( &image );
  117. }
  118. // Test for c++ version of Latent SVM
  119. class LatentSVMDetectorTest : public cvtest::BaseTest
  120. {
  121. protected:
  122. void run(int);
  123. };
  124. static void writeDetections( FileStorage& fs, const string& nodeName, const vector<LatentSvmDetector::ObjectDetection>& detections )
  125. {
  126. fs << nodeName << "[";
  127. for( size_t i = 0; i < detections.size(); i++ )
  128. {
  129. const LatentSvmDetector::ObjectDetection& d = detections[i];
  130. fs << d.rect.x << d.rect.y << d.rect.width << d.rect.height
  131. << d.score << d.classID;
  132. }
  133. fs << "]";
  134. }
  135. static void readDetections( FileStorage fs, const string& nodeName, vector<LatentSvmDetector::ObjectDetection>& detections )
  136. {
  137. detections.clear();
  138. FileNode fn = fs.root()[nodeName];
  139. FileNodeIterator fni = fn.begin();
  140. while( fni != fn.end() )
  141. {
  142. LatentSvmDetector::ObjectDetection d;
  143. fni >> d.rect.x >> d.rect.y >> d.rect.width >> d.rect.height
  144. >> d.score >> d.classID;
  145. detections.push_back( d );
  146. }
  147. }
  148. static inline bool isEqual( const LatentSvmDetector::ObjectDetection& d1, const LatentSvmDetector::ObjectDetection& d2, int eps, float threshold)
  149. {
  150. return (
  151. std::abs(d1.rect.x - d2.rect.x) <= eps
  152. && std::abs(d1.rect.y - d2.rect.y) <= eps
  153. && std::abs(d1.rect.width - d2.rect.width) <= eps
  154. && std::abs(d1.rect.height - d2.rect.height) <= eps
  155. && (d1.classID == d2.classID)
  156. && std::abs(d1.score - d2.score) <= threshold
  157. );
  158. }
  159. std::ostream& operator << (std::ostream& os, const CvRect& r)
  160. {
  161. return (os << "[x=" << r.x << ", y=" << r.y << ", w=" << r.width << ", h=" << r.height << "]");
  162. }
  163. bool compareResults( const vector<LatentSvmDetector::ObjectDetection>& calc, const vector<LatentSvmDetector::ObjectDetection>& valid, int eps, float threshold)
  164. {
  165. if( calc.size() != valid.size() )
  166. return false;
  167. for( size_t i = 0; i < calc.size(); i++ )
  168. {
  169. const LatentSvmDetector::ObjectDetection& c = calc[i];
  170. const LatentSvmDetector::ObjectDetection& v = valid[i];
  171. if( !isEqual(c, v, eps, threshold) )
  172. {
  173. std::cerr << "Expected: " << v.rect << " class=" << v.classID << " score=" << v.score << std::endl;
  174. std::cerr << "Actual: " << c.rect << " class=" << c.classID << " score=" << c.score << std::endl;
  175. return false;
  176. }
  177. }
  178. return true;
  179. }
  180. void LatentSVMDetectorTest::run( int /* start_from */)
  181. {
  182. string img_path_cat = string(ts->get_data_path()) + "latentsvmdetector/cat.png";
  183. string img_path_cars = string(ts->get_data_path()) + "latentsvmdetector/cars.png";
  184. string model_path_cat = string(ts->get_data_path()) + "latentsvmdetector/models_VOC2007/cat.xml";
  185. string model_path_car = string(ts->get_data_path()) + "latentsvmdetector/models_VOC2007/car.xml";
  186. string true_res_path = string(ts->get_data_path()) + "latentsvmdetector/results.xml";
  187. int numThreads = 1;
  188. #ifdef HAVE_TBB
  189. numThreads = 2;
  190. #endif
  191. Mat image_cat = imread( img_path_cat );
  192. Mat image_cars = imread( img_path_cars );
  193. if( image_cat.empty() || image_cars.empty() )
  194. {
  195. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  196. return;
  197. }
  198. // We will test 2 cases:
  199. // detector1 - to test case of one class 'cat'
  200. // detector12 - to test case of two (several) classes 'cat' and car
  201. // Load detectors
  202. LatentSvmDetector detector1( vector<string>(1,model_path_cat) );
  203. vector<string> models_pathes(2);
  204. models_pathes[0] = model_path_cat;
  205. models_pathes[1] = model_path_car;
  206. LatentSvmDetector detector12( models_pathes );
  207. if( detector1.empty() || detector12.empty() || detector12.getClassCount() != 2 )
  208. {
  209. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
  210. return;
  211. }
  212. // 1. Test method detect
  213. // Run detectors
  214. vector<LatentSvmDetector::ObjectDetection> detections1_cat, detections12_cat, detections12_cars;
  215. detector1.detect( image_cat, detections1_cat, 0.5, numThreads );
  216. detector12.detect( image_cat, detections12_cat, 0.5, numThreads );
  217. detector12.detect( image_cars, detections12_cars, 0.5, numThreads );
  218. // Load true results
  219. FileStorage fs( true_res_path, FileStorage::READ );
  220. if( fs.isOpened() )
  221. {
  222. vector<LatentSvmDetector::ObjectDetection> true_detections1_cat, true_detections12_cat, true_detections12_cars;
  223. readDetections( fs, "detections1_cat", true_detections1_cat );
  224. readDetections( fs, "detections12_cat", true_detections12_cat );
  225. readDetections( fs, "detections12_cars", true_detections12_cars );
  226. if( !compareResults(detections1_cat, true_detections1_cat, 1, score_thr) )
  227. {
  228. std::cerr << "Results of detector1 are invalid on image cat.png" << std::endl;
  229. ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
  230. }
  231. if( !compareResults(detections12_cat, true_detections12_cat, 1, score_thr) )
  232. {
  233. std::cerr << "Results of detector12 are invalid on image cat.png" << std::endl;
  234. ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
  235. }
  236. if( !compareResults(detections12_cars, true_detections12_cars, 1, score_thr) )
  237. {
  238. std::cerr << "Results of detector12 are invalid on image cars.png" << std::endl;
  239. ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
  240. }
  241. }
  242. else
  243. {
  244. fs.open( true_res_path, FileStorage::WRITE );
  245. if( fs.isOpened() )
  246. {
  247. writeDetections( fs, "detections1_cat", detections1_cat );
  248. writeDetections( fs, "detections12_cat", detections12_cat );
  249. writeDetections( fs, "detections12_cars", detections12_cars );
  250. }
  251. else
  252. std::cerr << "File " << true_res_path << " cann't be opened to save test results" << std::endl;
  253. }
  254. // 2. Simple tests of other methods
  255. if( detector1.getClassCount() != 1 || detector1.getClassNames()[0] != "cat" )
  256. {
  257. std::cerr << "Incorrect result of method getClassNames() or getClassCount()" << std::endl;
  258. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT);
  259. }
  260. detector1.clear();
  261. if( !detector1.empty() )
  262. {
  263. std::cerr << "There is a bug in method clear() or empty()" << std::endl;
  264. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT);
  265. }
  266. ts->set_failed_test_info( cvtest::TS::OK);
  267. }
  268. TEST(Objdetect_LatentSVMDetector_c, DISABLED_regression) { CV_LatentSVMDetectorTest test; test.safe_run(); }
  269. TEST(Objdetect_LatentSVMDetector_cpp, DISABLED_regression) { LatentSVMDetectorTest test; test.safe_run(); }