Visualizer.cpp 5.5 KB

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