tracker_dataset.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. //
  42. // !!! this sample requires the opencv_datasets module !!!
  43. //
  44. #include "opencv2/opencv_modules.hpp"
  45. #ifdef HAVE_OPENCV_DATASETS
  46. #include "opencv2/datasets/track_vot.hpp"
  47. #include <opencv2/core/utility.hpp>
  48. #include <opencv2/tracking.hpp>
  49. #include <opencv2/videoio.hpp>
  50. #include <opencv2/highgui.hpp>
  51. #include <opencv2/imgproc.hpp>
  52. #include "samples_utility.hpp"
  53. #include <iostream>
  54. using namespace std;
  55. using namespace cv;
  56. using namespace cv::datasets;
  57. #define NUM_TEST_FRAMES 300
  58. #define TEST_VIDEO_INDEX 1 //TLD Dataset Video Index from 1-10
  59. //#define RECORD_VIDEO_FLG
  60. static Mat image;
  61. static Rect boundingBox;
  62. static bool paused;
  63. static bool selectObject = false;
  64. static bool startSelection = false;
  65. static const char* keys =
  66. { "{@tracker_algorithm | | Tracker algorithm }"
  67. "{@dataset_path |true| Dataset path }"
  68. "{@dataset_id |1| Dataset ID }"
  69. };
  70. static void onMouse(int event, int x, int y, int, void*)
  71. {
  72. if (!selectObject)
  73. {
  74. switch (event)
  75. {
  76. case EVENT_LBUTTONDOWN:
  77. //set origin of the bounding box
  78. startSelection = true;
  79. boundingBox.x = x;
  80. boundingBox.y = y;
  81. boundingBox.width = boundingBox.height = 0;
  82. break;
  83. case EVENT_LBUTTONUP:
  84. //sei with and height of the bounding box
  85. boundingBox.width = std::abs(x - boundingBox.x);
  86. boundingBox.height = std::abs(y - boundingBox.y);
  87. paused = false;
  88. selectObject = true;
  89. break;
  90. case EVENT_MOUSEMOVE:
  91. if (startSelection && !selectObject)
  92. {
  93. //draw the bounding box
  94. Mat currentFrame;
  95. image.copyTo(currentFrame);
  96. rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
  97. imshow("Tracking API", currentFrame);
  98. }
  99. break;
  100. }
  101. }
  102. }
  103. static void help()
  104. {
  105. cout << "\nThis example shows the functionality of \"Long-term optical tracking API\""
  106. "TLD dataset ID: 1~10, VOT2015 dataset ID: 1~60\n"
  107. "-- pause video [p] and draw a bounding box around the target to start the tracker\n"
  108. "Example:\n"
  109. "./example_tracking_tracker_dataset <tracker_algorithm> <dataset_path> <dataset_id>\n"
  110. << endl;
  111. cout << "\n\nHot keys: \n"
  112. "\tq - quit the program\n"
  113. "\tp - pause video\n";
  114. }
  115. int main(int argc, char *argv[])
  116. {
  117. CommandLineParser parser(argc, argv, keys);
  118. string tracker_algorithm = parser.get<string>(0);
  119. string datasetRootPath = parser.get<string>(1);
  120. int datasetID = parser.get<int>(2);
  121. if (tracker_algorithm.empty() || datasetRootPath.empty())
  122. {
  123. help();
  124. return -1;
  125. }
  126. Mat frame;
  127. paused = false;
  128. namedWindow("Tracking API", 0);
  129. setMouseCallback("Tracking API", onMouse, 0);
  130. //Create Tracker
  131. Ptr<Tracker> tracker = createTrackerByName(tracker_algorithm);
  132. if (!tracker)
  133. {
  134. cout << "***Error in the instantiation of the tracker...***\n";
  135. getchar();
  136. return 0;
  137. }
  138. //Init Dataset
  139. Ptr<TRACK_vot> dataset = TRACK_vot::create();
  140. dataset->load(datasetRootPath);
  141. dataset->initDataset(datasetID);
  142. //Read first frame
  143. dataset->getNextFrame(frame);
  144. frame.copyTo(image);
  145. rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1);
  146. imshow("Tracking API", image);
  147. bool initialized = false;
  148. paused = true;
  149. int frameCounter = 0;
  150. //Time measurment
  151. int64 e3 = getTickCount();
  152. for (;;)
  153. {
  154. if (!paused)
  155. {
  156. //Time measurment
  157. int64 e1 = getTickCount();
  158. if (initialized){
  159. if (!dataset->getNextFrame(frame))
  160. break;
  161. frame.copyTo(image);
  162. }
  163. if (!initialized && selectObject)
  164. {
  165. //initializes the tracker
  166. tracker->init(frame, boundingBox);
  167. initialized = true;
  168. }
  169. else if (initialized)
  170. {
  171. //updates the tracker
  172. if (tracker->update(frame, boundingBox))
  173. {
  174. rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1);
  175. }
  176. }
  177. imshow("Tracking API", image);
  178. frameCounter++;
  179. //Time measurment
  180. int64 e2 = getTickCount();
  181. double t1 = (e2 - e1) / getTickFrequency();
  182. cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
  183. }
  184. char c = (char)waitKey(2);
  185. if (c == 'q')
  186. break;
  187. if (c == 'p')
  188. paused = !paused;
  189. //waitKey(0);
  190. }
  191. //Time measurment
  192. int64 e4 = getTickCount();
  193. double t2 = (e4 - e3) / getTickFrequency();
  194. cout << "Average Time for Frame: " << t2 * 1000.0 / frameCounter << "ms" << endl;
  195. cout << "Average FPS: " << 1.0 / t2*frameCounter << endl;
  196. waitKey(0);
  197. return 0;
  198. }
  199. #else // ! HAVE_OPENCV_DATASETS
  200. #include <opencv2/core.hpp>
  201. int main() {
  202. CV_Error(cv::Error::StsNotImplemented , "this sample needs to be built with opencv_datasets !");
  203. return -1;
  204. }
  205. #endif // HAVE_OPENCV_DATASETS