Visualizer.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. int Visualizer::GetTime()
  11. {
  12. return std::chrono::duration_cast<std::chrono::milliseconds>(
  13. std::chrono::system_clock::now().time_since_epoch()).count();
  14. }
  15. void Visualizer::Display(std::vector<core::TrackletPtr>& tracks,
  16. std::string image_folder, bool output,
  17. std::string output_path, std::string title, size_t first_frame,
  18. int play_fps, int grid_width, int grid_height)
  19. {
  20. util::Logger::LogInfo("Displaying data");
  21. size_t current_frame = first_frame;
  22. // Load images
  23. std::vector<std::string> image_files;
  24. util::FileIO::ListFiles(image_folder, image_files);
  25. // Generate a random color for each individual track
  26. std::vector<cv::Scalar> colors;
  27. std::random_device rd;
  28. std::mt19937 gen(rd());
  29. colors.push_back(cv::Scalar(0, 255, 0));
  30. colors.push_back(cv::Scalar(0, 0, 255));
  31. colors.push_back(cv::Scalar(0, 255, 255));
  32. colors.push_back(cv::Scalar(255, 0, 0));
  33. for (size_t i = 4; i < tracks.size(); ++i)
  34. {
  35. // BGR
  36. cv::Scalar color(std::generate_canonical<double, 10>(gen) * 255,
  37. std::generate_canonical<double, 10>(gen) * 255,
  38. std::generate_canonical<double, 10>(gen) * 255);
  39. colors.push_back(color);
  40. }
  41. //TODO move to extra class
  42. //TODO create necessary directories
  43. if (output)
  44. {
  45. util::Logger::LogInfo("Start writing output images");
  46. for (size_t i = 0; i < image_files.size(); ++i)
  47. {
  48. cv::Mat image = cv::imread(image_folder + "/" + image_files[i], 1);
  49. for (size_t j = 0; j < tracks.size(); ++j)
  50. {
  51. tracks[j]->Visualize(image, colors[j], i, 0, 0);
  52. }
  53. cv::imwrite(output_path + "/images/" + image_files[i], image);
  54. }
  55. util::Logger::LogInfo("Finished writing output images");
  56. }
  57. // Create window
  58. cv::namedWindow(title, cv::WindowFlags::WINDOW_AUTOSIZE);
  59. // Display frames and data
  60. int target_delay = 1000 / play_fps;
  61. int last_frame_time = GetTime();
  62. int current_delay, current_frame_time;
  63. bool play = false;
  64. while (true)
  65. {
  66. // Display image
  67. cv::Mat image = cv::imread(image_folder + "/" + image_files[current_frame], 1);
  68. // Draw grid
  69. if (grid_width > 0 && grid_height > 0)
  70. {
  71. cv::Scalar gridColor(255, 255, 255);
  72. double cellWidth = image.cols / grid_width;
  73. double cellHeight = image.rows / grid_height;
  74. for (int x = 0; x < grid_width; ++x)
  75. {
  76. for (int y = 0; y < grid_height; ++y)
  77. {
  78. cv::rectangle(image,
  79. cv::Point2d(x * cellWidth, y * cellHeight),
  80. cv::Point2d((x + 1) * cellWidth, (y + 1) * cellHeight),
  81. gridColor);
  82. }
  83. }
  84. }
  85. //Visualize the tracklets
  86. for (size_t i = 0; i < tracks.size(); ++i)
  87. {
  88. tracks[i]->Visualize(image, colors[i], current_frame, 0, 0);
  89. }
  90. cv::imshow(title, image);
  91. // Get key input
  92. int key;
  93. if (play)
  94. {
  95. current_frame_time = GetTime();
  96. current_delay = last_frame_time - current_frame_time;
  97. if (current_delay < target_delay)
  98. {
  99. key = cv::waitKey(target_delay - current_delay);
  100. }
  101. else
  102. {
  103. key = 0;
  104. }
  105. last_frame_time = GetTime();
  106. }
  107. else
  108. {
  109. key = cv::waitKey(0);
  110. }
  111. // Process key input
  112. if (key == 1048678) // F
  113. {
  114. play = !play;
  115. }
  116. else if (key == 1048603) // ESC
  117. {
  118. break;
  119. }
  120. if (play || key == 1048676) // D
  121. {
  122. if (current_frame < image_files.size() - 1)
  123. {
  124. current_frame++;
  125. }
  126. else
  127. {
  128. current_frame = image_files.size() - 1;
  129. play = false;
  130. }
  131. }
  132. else if (key == 1048673) // A
  133. {
  134. if (current_frame > 0)
  135. {
  136. current_frame--;
  137. }
  138. }
  139. }
  140. }
  141. }