goturnTracker.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. //Demo of GOTURN tracker
  42. //In order to use GOTURN tracker, GOTURN architecture goturn.prototxt and goturn.caffemodel are required to exist in root folder.
  43. //There are 2 ways to get caffemodel:
  44. //1 - Train you own GOTURN model using <https://github.com/Auron-X/GOTURN_Training_Toolkit>
  45. //2 - Download pretrained caffemodel from <https://github.com/opencv/opencv_extra>
  46. #include "opencv2/opencv_modules.hpp"
  47. #if defined(HAVE_OPENCV_DNN) && defined(HAVE_OPENCV_DATASETS)
  48. #include "opencv2/datasets/track_alov.hpp"
  49. #include <opencv2/core/utility.hpp>
  50. #include <opencv2/imgproc.hpp>
  51. #include <opencv2/tracking.hpp>
  52. #include <opencv2/videoio.hpp>
  53. #include <opencv2/highgui.hpp>
  54. #include <iostream>
  55. using namespace std;
  56. using namespace cv;
  57. using namespace cv::datasets;
  58. #define NUM_TEST_FRAMES 1000
  59. static Mat image;
  60. static bool paused;
  61. static bool selectObjects = false;
  62. static bool startSelection = false;
  63. static Rect boundingBox;
  64. static const char* keys =
  65. { "{@dataset_path || Dataset path }"
  66. "{@dataset_id |1| Dataset ID }"
  67. };
  68. static void onMouse(int event, int x, int y, int, void*)
  69. {
  70. if (!selectObjects)
  71. {
  72. switch (event)
  73. {
  74. case EVENT_LBUTTONDOWN:
  75. //set origin of the bounding box
  76. startSelection = true;
  77. boundingBox.x = x;
  78. boundingBox.y = y;
  79. boundingBox.width = boundingBox.height = 0;
  80. break;
  81. case EVENT_LBUTTONUP:
  82. //sei with and height of the bounding box
  83. boundingBox.width = std::abs(x - boundingBox.x);
  84. boundingBox.height = std::abs(y - boundingBox.y);
  85. paused = false;
  86. selectObjects = true;
  87. startSelection = false;
  88. break;
  89. case EVENT_MOUSEMOVE:
  90. if (startSelection && !selectObjects)
  91. {
  92. //draw the bounding box
  93. Mat currentFrame;
  94. image.copyTo(currentFrame);
  95. rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
  96. imshow("GOTURN Tracking", currentFrame);
  97. }
  98. break;
  99. }
  100. }
  101. }
  102. static void help()
  103. {
  104. cout << "\nThis example is a simple demo of GOTURN tracking on ALOV300++ dataset"
  105. "ALOV dataset contains videos with ID range: 1~314\n"
  106. "-- pause video [p] and draw a bounding boxes around the targets to start the tracker\n"
  107. "Example:\n"
  108. "./goturnTracker <dataset_path> <dataset_id>\n"
  109. << endl;
  110. cout << "\n\nHot keys: \n"
  111. "\tq - quit the program\n"
  112. "\tp - pause video\n";
  113. }
  114. int main(int argc, char *argv[])
  115. {
  116. CommandLineParser parser(argc, argv, keys);
  117. string datasetRootPath = parser.get<string>(0);
  118. int datasetID = parser.get<int>(1);
  119. if (datasetRootPath.empty())
  120. {
  121. help();
  122. return -1;
  123. }
  124. Mat frame;
  125. paused = false;
  126. namedWindow("GOTURN Tracking", 0);
  127. setMouseCallback("GOTURN Tracking", onMouse, 0);
  128. //Create GOTURN tracker
  129. auto tracker = TrackerGOTURN::create();
  130. //Load and init full ALOV300++ dataset with a given datasetID, as alternative you can use loadAnnotatedOnly(..)
  131. //to load only frames with labelled ground truth ~ every 5-th frame
  132. Ptr<cv::datasets::TRACK_alov> dataset = TRACK_alov::create();
  133. dataset->load(datasetRootPath);
  134. dataset->initDataset(datasetID);
  135. //Read first frame
  136. dataset->getNextFrame(frame);
  137. if (frame.empty())
  138. {
  139. cout << "invalid dataset: " << datasetRootPath << endl;
  140. return -2;
  141. }
  142. frame.copyTo(image);
  143. rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1);
  144. imshow("GOTURN Tracking", image);
  145. bool initialized = false;
  146. paused = true;
  147. int frameCounter = 0;
  148. //Time measurment
  149. int64 e3 = getTickCount();
  150. for (;;)
  151. {
  152. if (!paused)
  153. {
  154. //Time measurment
  155. int64 e1 = getTickCount();
  156. if (initialized){
  157. if (!dataset->getNextFrame(frame))
  158. break;
  159. frame.copyTo(image);
  160. }
  161. if (!initialized && selectObjects)
  162. {
  163. //Initialize the tracker and add targets
  164. tracker->init(frame, boundingBox);
  165. rectangle(frame, boundingBox, Scalar(0, 0, 255), 2, 1);
  166. initialized = true;
  167. }
  168. else if (initialized)
  169. {
  170. //Update all targets
  171. if (tracker->update(frame, boundingBox))
  172. {
  173. rectangle(frame, boundingBox, Scalar(0, 0, 255), 2, 1);
  174. }
  175. }
  176. imshow("GOTURN Tracking", frame);
  177. frameCounter++;
  178. //Time measurment
  179. int64 e2 = getTickCount();
  180. double t1 = (e2 - e1) / getTickFrequency();
  181. cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
  182. }
  183. char c = (char)waitKey(2);
  184. if (c == 'q')
  185. break;
  186. if (c == 'p')
  187. paused = !paused;
  188. }
  189. //Time measurment
  190. int64 e4 = getTickCount();
  191. double t2 = (e4 - e3) / getTickFrequency();
  192. cout << "Average Time for Frame: " << t2 * 1000.0 / frameCounter << "ms" << endl;
  193. cout << "Average FPS: " << 1.0 / t2*frameCounter << endl;
  194. waitKey(0);
  195. return 0;
  196. }
  197. #else // ! HAVE_OPENCV_DNN && HAVE_OPENCV_DATASETS
  198. #include <opencv2/core.hpp>
  199. int main() {
  200. CV_Error(cv::Error::StsNotImplemented , "this sample needs to be built with opencv_datasets and opencv_dnn !");
  201. return -1;
  202. }
  203. #endif