Visualizer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Created by wrede on 04.05.16.
  3. //
  4. #include "Visualizer.h"
  5. #include "Logger.h"
  6. #include "../core/ObjectDataAngular.h"
  7. #include "FileIO.h"
  8. namespace util
  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. util::Logger::LogInfo("Displaying data");
  106. size_t current_frame = first_frame;
  107. // Load images
  108. std::vector<std::string> image_files;
  109. util::FileIO::ListFiles(image_folder, image_files);
  110. // Create window
  111. cv::namedWindow(title, CV_WINDOW_AUTOSIZE);
  112. // Generate a random color for each individual track
  113. std::vector<cv::Scalar> colors;
  114. std::random_device rd;
  115. std::mt19937 gen(rd());
  116. colors.push_back(cv::Scalar(0, 0, 255));
  117. colors.push_back(cv::Scalar(0, 255, 0));
  118. colors.push_back(cv::Scalar(255, 0, 0));
  119. colors.push_back(cv::Scalar(0, 255, 255));
  120. for (size_t i = 4; i < tracks.size(); ++i)
  121. {
  122. // BGR
  123. cv::Scalar color(std::generate_canonical<double, 10>(gen) * 255,
  124. std::generate_canonical<double, 10>(gen) * 255,
  125. std::generate_canonical<double, 10>(gen) * 255);
  126. colors.push_back(color);
  127. }
  128. // Display frames and data
  129. int target_delay = 1000 / play_fps;
  130. int last_frame_time = GetTime();
  131. int current_delay, current_frame_time;
  132. bool play = false;
  133. while (true)
  134. {
  135. // Display image
  136. cv::Mat image = cv::imread(image_folder + "/"
  137. + image_files[current_frame],
  138. 1);
  139. // util::Logger::LogDebug("display frame " + std::to_string(current_frame));
  140. for (size_t i = 0; i < tracks.size(); ++i)
  141. {
  142. // util::Logger::LogDebug("display track #" + std::to_string(i));
  143. // tracks[i]->Visualize(image, colors[i]);
  144. tracks[i]->Visualize(image, colors[i], current_frame, 0, 0);
  145. }
  146. cv::imshow(title, image);
  147. // Get key input
  148. int key;
  149. if (play)
  150. {
  151. current_frame_time = GetTime();
  152. current_delay = last_frame_time - current_frame_time;
  153. if (current_delay < target_delay)
  154. {
  155. key = cv::waitKey(target_delay - current_delay);
  156. }
  157. else
  158. {
  159. key = 0;
  160. }
  161. last_frame_time = GetTime();
  162. }
  163. else
  164. {
  165. key = cv::waitKey(0);
  166. }
  167. // Process key input
  168. if (key == 1048678) // F
  169. {
  170. play = !play;
  171. }
  172. else if (key == 1048603) // ESC
  173. {
  174. break;
  175. }
  176. if (play || key == 1048676) // D
  177. {
  178. if (current_frame < image_files.size() - 1)
  179. {
  180. current_frame++;
  181. }
  182. else
  183. {
  184. current_frame = image_files.size() - 1;
  185. play = false;
  186. }
  187. }
  188. else if (key == 1048673) // A
  189. {
  190. if (current_frame > 0)
  191. {
  192. current_frame--;
  193. }
  194. }
  195. }
  196. }
  197. }