Visualizer.cpp 6.8 KB

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