Visualizer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, bool output, std::string output_path,
  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, bool output, std::string output_path,
  103. std::string title, size_t first_frame, int play_fps, int grid_width,
  104. int grid_height)
  105. {
  106. util::Logger::LogInfo("Displaying data");
  107. size_t current_frame = first_frame;
  108. // Load images
  109. std::vector<std::string> image_files;
  110. util::FileIO::ListFiles(image_folder, image_files);
  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. colors.push_back(cv::Scalar(0, 255, 0));
  116. colors.push_back(cv::Scalar(0, 0, 255));
  117. colors.push_back(cv::Scalar(0, 255, 255));
  118. colors.push_back(cv::Scalar(255, 0, 0));
  119. for (size_t i = 4; i < tracks.size(); ++i)
  120. {
  121. // BGR
  122. cv::Scalar color(std::generate_canonical<double, 10>(gen) * 255,
  123. std::generate_canonical<double, 10>(gen) * 255,
  124. std::generate_canonical<double, 10>(gen) * 255);
  125. colors.push_back(color);
  126. }
  127. //TODO move to extra class
  128. //TODO create necessary directories
  129. if (output)
  130. {
  131. util::Logger::LogInfo("Start writing output images");
  132. for (size_t i = 0; i < image_files.size(); ++i)
  133. {
  134. cv::Mat image = cv::imread(image_folder + "/" + image_files[i], 1);
  135. for (size_t j = 0; j < tracks.size(); ++j)
  136. {
  137. tracks[j]->Visualize(image, colors[j], i, 0, 0);
  138. }
  139. cv::imwrite(output_path + "/images/" + image_files[i], image);
  140. }
  141. util::Logger::LogInfo("Finished writing output images");
  142. }
  143. // Create window
  144. cv::namedWindow(title, CV_WINDOW_AUTOSIZE);
  145. // Display frames and data
  146. int target_delay = 1000 / play_fps;
  147. int last_frame_time = GetTime();
  148. int current_delay, current_frame_time;
  149. bool play = false;
  150. while (true)
  151. {
  152. // Display image
  153. cv::Mat image = cv::imread(image_folder + "/" + image_files[current_frame], 1);
  154. // Draw grid
  155. if (grid_width > 0 && grid_height > 0)
  156. {
  157. cv::Scalar gridColor(255, 255, 255);
  158. double cellWidth = image.cols / grid_width;
  159. double cellHeight = image.rows / grid_height;
  160. for (int x = 0; x < grid_width; ++x)
  161. {
  162. for (int y = 0; y < grid_height; ++y)
  163. {
  164. cv::rectangle(image,
  165. cv::Point2d(x * cellWidth, y * cellHeight),
  166. cv::Point2d((x + 1) * cellWidth, (y + 1) * cellHeight),
  167. gridColor);
  168. }
  169. }
  170. }
  171. //Visualize the tracklets
  172. for (size_t i = 0; i < tracks.size(); ++i)
  173. {
  174. tracks[i]->Visualize(image, colors[i], current_frame, 0, 0);
  175. }
  176. cv::imshow(title, image);
  177. // Get key input
  178. int key;
  179. if (play)
  180. {
  181. current_frame_time = GetTime();
  182. current_delay = last_frame_time - current_frame_time;
  183. if (current_delay < target_delay)
  184. {
  185. key = cv::waitKey(target_delay - current_delay);
  186. }
  187. else
  188. {
  189. key = 0;
  190. }
  191. last_frame_time = GetTime();
  192. }
  193. else
  194. {
  195. key = cv::waitKey(0);
  196. }
  197. // Process key input
  198. if (key == 1048678) // F
  199. {
  200. play = !play;
  201. }
  202. else if (key == 1048603) // ESC
  203. {
  204. break;
  205. }
  206. if (play || key == 1048676) // D
  207. {
  208. if (current_frame < image_files.size() - 1)
  209. {
  210. current_frame++;
  211. }
  212. else
  213. {
  214. current_frame = image_files.size() - 1;
  215. play = false;
  216. }
  217. }
  218. else if (key == 1048673) // A
  219. {
  220. if (current_frame > 0)
  221. {
  222. current_frame--;
  223. }
  224. }
  225. }
  226. }
  227. }