multiTracker_dataset.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "opencv2/opencv_modules.hpp"
  42. #include "opencv2/core.hpp"
  43. #ifdef HAVE_OPENCV_DATASETS
  44. #include "opencv2/datasets/track_vot.hpp"
  45. #include <opencv2/core/utility.hpp>
  46. #include <opencv2/tracking.hpp>
  47. #include <opencv2/videoio.hpp>
  48. #include <opencv2/highgui.hpp>
  49. #include "samples_utility.hpp"
  50. #include <iostream>
  51. using namespace std;
  52. using namespace cv;
  53. using namespace cv::datasets;
  54. #define NUM_TEST_FRAMES 1000
  55. static Mat image;
  56. static bool paused;
  57. static bool selectObjects = false;
  58. static bool startSelection = false;
  59. vector<Rect2d> boundingBoxes;
  60. int targetsCnt = 0;
  61. int targetsNum = 0;
  62. Rect2d boundingBox;
  63. static const char* keys =
  64. { "{@tracker_algorithm | | Tracker algorithm }"
  65. "{@target_num |1| Number of targets }"
  66. "{@dataset_path |true| Dataset path }"
  67. "{@dataset_id |1| Dataset ID }"
  68. };
  69. static void onMouse(int event, int x, int y, int, void*)
  70. {
  71. if (!selectObjects)
  72. {
  73. switch (event)
  74. {
  75. case EVENT_LBUTTONDOWN:
  76. //set origin of the bounding box
  77. startSelection = true;
  78. boundingBox.x = x;
  79. boundingBox.y = y;
  80. boundingBox.width = boundingBox.height = 0;
  81. break;
  82. case EVENT_LBUTTONUP:
  83. //sei with and height of the bounding box
  84. boundingBox.width = std::abs(x - boundingBox.x);
  85. boundingBox.height = std::abs(y - boundingBox.y);
  86. boundingBoxes.push_back(boundingBox);
  87. targetsCnt++;
  88. if (targetsCnt == targetsNum)
  89. {
  90. paused = false;
  91. selectObjects = true;
  92. }
  93. startSelection = false;
  94. break;
  95. case EVENT_MOUSEMOVE:
  96. if (startSelection && !selectObjects)
  97. {
  98. //draw the bounding box
  99. Mat currentFrame;
  100. image.copyTo(currentFrame);
  101. for (int i = 0; i < (int)boundingBoxes.size(); i++)
  102. rectangle(currentFrame, boundingBoxes[i], Scalar(255, 0, 0), 2, 1);
  103. rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
  104. imshow("Tracking API", currentFrame);
  105. }
  106. break;
  107. }
  108. }
  109. }
  110. static void help()
  111. {
  112. cout << "\nThis example shows the functionality of \"Long-term optical tracking API\""
  113. "TLD dataset ID: 1~10, VOT2015 dataset ID: 1~60\n"
  114. "-- pause video [p] and draw a bounding boxes around the targets to start the tracker\n"
  115. "Example:\n"
  116. "./example_tracking_multiTracker_dataset<tracker_algorithm> <number_of_targets> <dataset_path> <dataset_id>\n"
  117. << endl;
  118. cout << "\n\nHot keys: \n"
  119. "\tq - quit the program\n"
  120. "\tp - pause video\n";
  121. }
  122. int main(int argc, char *argv[])
  123. {
  124. CommandLineParser parser(argc, argv, keys);
  125. string tracker_algorithm = parser.get<string>(0);
  126. targetsNum = parser.get<int>(1);
  127. string datasetRootPath = parser.get<string>(2);
  128. int datasetID = parser.get<int>(3);
  129. if (tracker_algorithm.empty() || datasetRootPath.empty() || targetsNum < 1)
  130. {
  131. help();
  132. return -1;
  133. }
  134. Mat frame;
  135. paused = false;
  136. namedWindow("Tracking API", 0);
  137. setMouseCallback("Tracking API", onMouse, 0);
  138. legacy::MultiTrackerTLD mt;
  139. //Init Dataset
  140. Ptr<TRACK_vot> dataset = TRACK_vot::create();
  141. dataset->load(datasetRootPath);
  142. dataset->initDataset(datasetID);
  143. //Read first frame
  144. dataset->getNextFrame(frame);
  145. frame.copyTo(image);
  146. for (int i = 0; i < (int)boundingBoxes.size(); i++)
  147. rectangle(image, boundingBoxes[i], Scalar(255, 0, 0), 2, 1);
  148. imshow("Tracking API", image);
  149. bool initialized = false;
  150. paused = true;
  151. int frameCounter = 0;
  152. //Time measurment
  153. int64 e3 = getTickCount();
  154. for (;;)
  155. {
  156. if (!paused)
  157. {
  158. //Time measurment
  159. int64 e1 = getTickCount();
  160. if (initialized){
  161. if (!dataset->getNextFrame(frame))
  162. break;
  163. frame.copyTo(image);
  164. }
  165. if (!initialized && selectObjects)
  166. {
  167. //Initialize the tracker and add targets
  168. for (int i = 0; i < (int)boundingBoxes.size(); i++)
  169. {
  170. if (!mt.addTarget(frame, boundingBoxes[i], createTrackerByName_legacy(tracker_algorithm)))
  171. {
  172. cout << "Trackers Init Error!!!";
  173. return 0;
  174. }
  175. rectangle(frame, boundingBoxes[i], mt.colors[0], 2, 1);
  176. }
  177. initialized = true;
  178. }
  179. else if (initialized)
  180. {
  181. //Update all targets
  182. if (mt.update(frame))
  183. {
  184. for (int i = 0; i < mt.targetNum; i++)
  185. {
  186. rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1);
  187. }
  188. }
  189. }
  190. imshow("Tracking API", frame);
  191. frameCounter++;
  192. //Time measurment
  193. int64 e2 = getTickCount();
  194. double t1 = (e2 - e1) / getTickFrequency();
  195. cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
  196. }
  197. char c = (char)waitKey(2);
  198. if (c == 'q')
  199. break;
  200. if (c == 'p')
  201. paused = !paused;
  202. //waitKey(0);
  203. }
  204. //Time measurment
  205. int64 e4 = getTickCount();
  206. double t2 = (e4 - e3) / getTickFrequency();
  207. cout << "Average Time for Frame: " << t2 * 1000.0 / frameCounter << "ms" << endl;
  208. cout << "Average FPS: " << 1.0 / t2*frameCounter << endl;
  209. waitKey(0);
  210. return 0;
  211. }
  212. #else // ! HAVE_OPENCV_DATASETS
  213. #include <opencv2/core.hpp>
  214. int main() {
  215. CV_Error(cv::Error::StsNotImplemented , "this sample needs to be built with opencv_datasets !");
  216. return -1;
  217. }
  218. #endif // HAVE_OPENCV_DATASETS