test_trackers.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. //#define DEBUG_TEST
  6. #ifdef DEBUG_TEST
  7. #include <opencv2/highgui.hpp>
  8. #endif
  9. namespace opencv_test { namespace {
  10. //using namespace cv::tracking;
  11. #define TESTSET_NAMES testing::Values("david", "dudek", "faceocc2")
  12. const string TRACKING_DIR = "tracking";
  13. const string FOLDER_IMG = "data";
  14. const string FOLDER_OMIT_INIT = "initOmit";
  15. #include "test_trackers.impl.hpp"
  16. //[TESTDATA]
  17. PARAM_TEST_CASE(DistanceAndOverlap, string)
  18. {
  19. string dataset;
  20. virtual void SetUp()
  21. {
  22. dataset = GET_PARAM(0);
  23. }
  24. };
  25. TEST_P(DistanceAndOverlap, MIL)
  26. {
  27. TrackerTest<Tracker, Rect> test(TrackerMIL::create(), dataset, 30, .65f, NoTransform);
  28. test.run();
  29. }
  30. TEST_P(DistanceAndOverlap, Shifted_Data_MIL)
  31. {
  32. TrackerTest<Tracker, Rect> test(TrackerMIL::create(), dataset, 30, .6f, CenterShiftLeft);
  33. test.run();
  34. }
  35. /***************************************************************************************/
  36. //Tests with scaled initial window
  37. TEST_P(DistanceAndOverlap, Scaled_Data_MIL)
  38. {
  39. TrackerTest<Tracker, Rect> test(TrackerMIL::create(), dataset, 30, .7f, Scale_1_1);
  40. test.run();
  41. }
  42. TEST_P(DistanceAndOverlap, GOTURN)
  43. {
  44. std::string model = cvtest::findDataFile("dnn/gsoc2016-goturn/goturn.prototxt");
  45. std::string weights = cvtest::findDataFile("dnn/gsoc2016-goturn/goturn.caffemodel", false);
  46. cv::TrackerGOTURN::Params params;
  47. params.modelTxt = model;
  48. params.modelBin = weights;
  49. TrackerTest<Tracker, Rect> test(TrackerGOTURN::create(params), dataset, 35, .35f, NoTransform);
  50. test.run();
  51. }
  52. INSTANTIATE_TEST_CASE_P(Tracking, DistanceAndOverlap, TESTSET_NAMES);
  53. static bool checkIOU(const Rect& r0, const Rect& r1, double threshold)
  54. {
  55. int interArea = (r0 & r1).area();
  56. double iouVal = (interArea * 1.0 )/ (r0.area() + r1.area() - interArea);;
  57. if (iouVal > threshold)
  58. return true;
  59. else
  60. {
  61. std::cout <<"Unmatched IOU: expect IOU val ("<<iouVal <<") > the IOU threadhold ("<<threshold<<")! Box 0 is "
  62. << r0 <<", and Box 1 is "<<r1<< std::endl;
  63. return false;
  64. }
  65. }
  66. static void checkTrackingAccuracy(cv::Ptr<Tracker>& tracker, double iouThreshold = 0.7)
  67. {
  68. // Template image
  69. Mat img0 = imread(findDataFile("tracking/bag/00000001.jpg"), 1);
  70. // Tracking image sequence.
  71. std::vector<Mat> imgs;
  72. imgs.push_back(imread(findDataFile("tracking/bag/00000002.jpg"), 1));
  73. imgs.push_back(imread(findDataFile("tracking/bag/00000003.jpg"), 1));
  74. imgs.push_back(imread(findDataFile("tracking/bag/00000004.jpg"), 1));
  75. imgs.push_back(imread(findDataFile("tracking/bag/00000005.jpg"), 1));
  76. imgs.push_back(imread(findDataFile("tracking/bag/00000006.jpg"), 1));
  77. cv::Rect roi(325, 164, 100, 100);
  78. std::vector<Rect> targetRois;
  79. targetRois.push_back(cv::Rect(278, 133, 99, 104));
  80. targetRois.push_back(cv::Rect(293, 88, 93, 110));
  81. targetRois.push_back(cv::Rect(287, 76, 89, 116));
  82. targetRois.push_back(cv::Rect(297, 74, 82, 122));
  83. targetRois.push_back(cv::Rect(311, 83, 78, 125));
  84. tracker->init(img0, roi);
  85. CV_Assert(targetRois.size() == imgs.size());
  86. for (int i = 0; i < (int)imgs.size(); i++)
  87. {
  88. bool res = tracker->update(imgs[i], roi);
  89. ASSERT_TRUE(res);
  90. ASSERT_TRUE(checkIOU(roi, targetRois[i], iouThreshold)) << cv::format("Fail at img %d.",i);
  91. }
  92. }
  93. TEST(GOTURN, accuracy)
  94. {
  95. std::string model = cvtest::findDataFile("dnn/gsoc2016-goturn/goturn.prototxt");
  96. std::string weights = cvtest::findDataFile("dnn/gsoc2016-goturn/goturn.caffemodel", false);
  97. cv::TrackerGOTURN::Params params;
  98. params.modelTxt = model;
  99. params.modelBin = weights;
  100. cv::Ptr<Tracker> tracker = TrackerGOTURN::create(params);
  101. // TODO! GOTURN have low accuracy. Try to remove this api at 5.x.
  102. checkTrackingAccuracy(tracker, 0.08);
  103. }
  104. TEST(DaSiamRPN, accuracy)
  105. {
  106. std::string model = cvtest::findDataFile("dnn/onnx/models/dasiamrpn_model.onnx", false);
  107. std::string kernel_r1 = cvtest::findDataFile("dnn/onnx/models/dasiamrpn_kernel_r1.onnx", false);
  108. std::string kernel_cls1 = cvtest::findDataFile("dnn/onnx/models/dasiamrpn_kernel_cls1.onnx", false);
  109. cv::TrackerDaSiamRPN::Params params;
  110. params.model = model;
  111. params.kernel_r1 = kernel_r1;
  112. params.kernel_cls1 = kernel_cls1;
  113. cv::Ptr<Tracker> tracker = TrackerDaSiamRPN::create(params);
  114. checkTrackingAccuracy(tracker, 0.7);
  115. }
  116. TEST(NanoTrack, accuracy_NanoTrack_V1)
  117. {
  118. std::string backbonePath = cvtest::findDataFile("dnn/onnx/models/nanotrack_backbone_sim.onnx", false);
  119. std::string neckheadPath = cvtest::findDataFile("dnn/onnx/models/nanotrack_head_sim.onnx", false);
  120. cv::TrackerNano::Params params;
  121. params.backbone = backbonePath;
  122. params.neckhead = neckheadPath;
  123. cv::Ptr<Tracker> tracker = TrackerNano::create(params);
  124. checkTrackingAccuracy(tracker);
  125. }
  126. TEST(NanoTrack, accuracy_NanoTrack_V2)
  127. {
  128. std::string backbonePath = cvtest::findDataFile("dnn/onnx/models/nanotrack_backbone_sim_v2.onnx", false);
  129. std::string neckheadPath = cvtest::findDataFile("dnn/onnx/models/nanotrack_head_sim_v2.onnx", false);
  130. cv::TrackerNano::Params params;
  131. params.backbone = backbonePath;
  132. params.neckhead = neckheadPath;
  133. cv::Ptr<Tracker> tracker = TrackerNano::create(params);
  134. checkTrackingAccuracy(tracker, 0.69);
  135. }
  136. }} // namespace opencv_test::