Visualizer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Created by wrede on 04.05.16.
  3. //
  4. #include "Visualizer.h"
  5. #include "../util/Logger.h"
  6. #include "../core/ObjectDataAngular.h"
  7. #include "../util/FileIO.h"
  8. namespace visual
  9. {
  10. void Visualizer::Display(core::DetectionSequence& sequence,
  11. std::string image_folder,
  12. std::string title, size_t first_frame,
  13. int play_fps)
  14. {
  15. size_t current_frame = first_frame;
  16. // Load images
  17. std::vector<std::string> image_files;
  18. util::FileIO::ListFiles(image_folder, image_files);
  19. if (image_files.size() != sequence.GetFrameCount())
  20. {
  21. perror("Image count and frame count don't match\n");
  22. }
  23. // Create window
  24. cv::namedWindow(title, CV_WINDOW_AUTOSIZE);
  25. // Display frames and data
  26. int target_delay = 1000 / play_fps;
  27. int last_frame_time = GetTime();
  28. int current_delay, current_frame_time;
  29. bool play = false;
  30. while (true)
  31. {
  32. // Display image
  33. cv::Mat image = cv::imread(image_folder + "/"
  34. + image_files[current_frame],
  35. 1);
  36. for (size_t i = 0; i < sequence.GetObjectCount(current_frame); ++i)
  37. {
  38. core::ObjectDataPtr obj = sequence.GetObject(current_frame, i);
  39. // BGR
  40. cv::Scalar color(0,
  41. (1.0 - obj->GetDetectionScore()) * 255,
  42. obj->GetDetectionScore() * 255);
  43. obj->Visualize(image, color);
  44. }
  45. cv::imshow(title, image);
  46. // Receive key input
  47. int key;
  48. if (play)
  49. {
  50. current_frame_time = GetTime();
  51. current_delay = last_frame_time - current_frame_time;
  52. if (current_delay < target_delay)
  53. {
  54. key = cv::waitKey(target_delay - current_delay);
  55. }
  56. else
  57. {
  58. key = 0;
  59. }
  60. last_frame_time = GetTime();
  61. }
  62. else
  63. {
  64. key = cv::waitKey(0);
  65. }
  66. // Process key input
  67. if (key == 1048678) // F
  68. {
  69. play = !play;
  70. }
  71. else if (key == 1048603) // ESC
  72. {
  73. break;
  74. }
  75. if (play || key == 1048676) // D
  76. {
  77. if (current_frame < image_files.size() - 1)
  78. {
  79. current_frame++;
  80. }
  81. else
  82. {
  83. current_frame = image_files.size() - 1;
  84. play = false;
  85. }
  86. }
  87. else if (key == 1048673) // A
  88. {
  89. if (current_frame > 0)
  90. {
  91. current_frame--;
  92. }
  93. }
  94. }
  95. }
  96. int Visualizer::GetTime()
  97. {
  98. return std::chrono::duration_cast<std::chrono::milliseconds>(
  99. std::chrono::system_clock::now().time_since_epoch()).count();
  100. }
  101. void Visualizer::Display(std::vector<core::TrackletPtr>& tracks,
  102. std::string image_folder, std::string title,
  103. size_t first_frame, int play_fps)
  104. {
  105. size_t current_frame = first_frame;
  106. // Load images
  107. std::vector<std::string> image_files;
  108. util::FileIO::ListFiles(image_folder, image_files);
  109. // Create window
  110. cv::namedWindow(title, CV_WINDOW_AUTOSIZE);
  111. // Generate a random color for each individual track
  112. std::vector<cv::Scalar> colors;
  113. std::random_device rd;
  114. std::mt19937 gen(rd());
  115. for (size_t i = 0; i < tracks.size(); ++i)
  116. {
  117. // BGR
  118. cv::Scalar color(std::generate_canonical<double, 10>(gen) * 255,
  119. std::generate_canonical<double, 10>(gen) * 255,
  120. std::generate_canonical<double, 10>(gen) * 255);
  121. colors.push_back(color);
  122. }
  123. // Display frames and data
  124. int target_delay = 1000 / play_fps;
  125. int last_frame_time = GetTime();
  126. int current_delay, current_frame_time;
  127. bool play = false;
  128. while (true)
  129. {
  130. // Display image
  131. cv::Mat image = cv::imread(image_folder + "/"
  132. + image_files[current_frame],
  133. 1);
  134. util::Logger::LogDebug("visualize frame " + std::to_string(current_frame));
  135. for (size_t i = 0; i < tracks.size(); ++i)
  136. {
  137. tracks[i]->Visualize(image, colors[i], current_frame, 1, 1);
  138. }
  139. cv::imshow(title, image);
  140. // Get key input
  141. int key;
  142. if (play)
  143. {
  144. current_frame_time = GetTime();
  145. current_delay = last_frame_time - current_frame_time;
  146. if (current_delay < target_delay)
  147. {
  148. key = cv::waitKey(target_delay - current_delay);
  149. }
  150. else
  151. {
  152. key = 0;
  153. }
  154. last_frame_time = GetTime();
  155. }
  156. else
  157. {
  158. key = cv::waitKey(0);
  159. }
  160. // Process key input
  161. if (key == 1048678) // F
  162. {
  163. play = !play;
  164. }
  165. else if (key == 1048603) // ESC
  166. {
  167. break;
  168. }
  169. if (play || key == 1048676) // D
  170. {
  171. if (current_frame < image_files.size() - 1)
  172. {
  173. current_frame++;
  174. }
  175. else
  176. {
  177. current_frame = image_files.size() - 1;
  178. play = false;
  179. }
  180. }
  181. else if (key == 1048673) // A
  182. {
  183. if (current_frame > 0)
  184. {
  185. current_frame--;
  186. }
  187. }
  188. }
  189. }
  190. }