Visualizer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, int grid_width, int grid_height)
  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, 255, 0));
  117. colors.push_back(cv::Scalar(0, 0, 255));
  118. colors.push_back(cv::Scalar(0, 255, 255));
  119. colors.push_back(cv::Scalar(255, 0, 0));
  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. //TODO draw grid (experimental)
  140. if (grid_width > 0 && grid_height > 0)
  141. {
  142. cv::Scalar gridColor(255, 255, 255);
  143. double cellWidth = image.cols / grid_width;
  144. double cellHeight = image.rows / grid_height;
  145. for (int x = 0; x < grid_width; ++x)
  146. {
  147. for (int y = 0; y < grid_height; ++y)
  148. {
  149. cv::rectangle(image,
  150. cv::Point2d(x * cellWidth, y * cellHeight),
  151. cv::Point2d((x + 1) * cellWidth, (y + 1) * cellHeight),
  152. gridColor);
  153. }
  154. }
  155. }
  156. for (size_t i = 0; i < tracks.size(); ++i)
  157. {
  158. tracks[i]->Visualize(image, colors[i], current_frame, 0, 0);
  159. }
  160. cv::imshow(title, image);
  161. // Get key input
  162. int key;
  163. if (play)
  164. {
  165. current_frame_time = GetTime();
  166. current_delay = last_frame_time - current_frame_time;
  167. if (current_delay < target_delay)
  168. {
  169. key = cv::waitKey(target_delay - current_delay);
  170. }
  171. else
  172. {
  173. key = 0;
  174. }
  175. last_frame_time = GetTime();
  176. }
  177. else
  178. {
  179. key = cv::waitKey(0);
  180. }
  181. // Process key input
  182. if (key == 1048678) // F
  183. {
  184. play = !play;
  185. }
  186. else if (key == 1048603) // ESC
  187. {
  188. break;
  189. }
  190. if (play || key == 1048676) // D
  191. {
  192. if (current_frame < image_files.size() - 1)
  193. {
  194. current_frame++;
  195. }
  196. else
  197. {
  198. current_frame = image_files.size() - 1;
  199. play = false;
  200. }
  201. }
  202. else if (key == 1048673) // A
  203. {
  204. if (current_frame > 0)
  205. {
  206. current_frame--;
  207. }
  208. }
  209. }
  210. }
  211. }