Visualizer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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,
  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], 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. // Create window
  59. cv::namedWindow(title, CV_WINDOW_AUTOSIZE);
  60. // Display frames and data
  61. int target_delay = 1000 / play_fps;
  62. int last_frame_time = GetTime();
  63. int current_delay, current_frame_time;
  64. bool play = false;
  65. while (true)
  66. {
  67. // Display image
  68. cv::Mat image = cv::imread(image_folder + "/" + image_files[current_frame], 1);
  69. // Draw grid if resolution is specified
  70. if (show_grid && grid_width > 0 && grid_height > 0)
  71. {
  72. cv::Scalar gridColor(255, 255, 255);
  73. double cellWidth = image.cols / grid_width;
  74. double cellHeight = image.rows / grid_height;
  75. for (int x = 0; x < grid_width; ++x)
  76. {
  77. for (int y = 0; y < grid_height; ++y)
  78. {
  79. cv::rectangle(image,
  80. cv::Point2d(x * cellWidth, y * cellHeight),
  81. cv::Point2d((x + 1) * cellWidth, (y + 1) * cellHeight),
  82. gridColor);
  83. }
  84. }
  85. }
  86. // Visualize the tracklets
  87. for (size_t i = 0; i < tracks.size(); ++i)
  88. {
  89. tracks[i]->Visualize(image, colors[i], current_frame + frame_offset, 0, 0);
  90. }
  91. cv::imshow(title, image);
  92. // Get key input
  93. int key;
  94. if (play)
  95. {
  96. current_frame_time = GetTime();
  97. current_delay = last_frame_time - current_frame_time;
  98. if (current_delay < target_delay)
  99. {
  100. key = cv::waitKey(target_delay - current_delay);
  101. }
  102. else
  103. {
  104. key = 0;
  105. }
  106. last_frame_time = GetTime();
  107. }
  108. else
  109. {
  110. key = cv::waitKey(0);
  111. }
  112. // Process key input
  113. if (key == 1048678) // F
  114. {
  115. play = !play;
  116. }
  117. else if (key == 1048603) // ESC
  118. {
  119. break;
  120. }
  121. if (play || key == 1048676) // D
  122. {
  123. if (current_frame < image_files.size() - 1)
  124. {
  125. current_frame++;
  126. }
  127. else
  128. {
  129. current_frame = image_files.size() - 1;
  130. play = false;
  131. }
  132. }
  133. else if (key == 1048673) // A
  134. {
  135. if (current_frame > 0)
  136. {
  137. current_frame--;
  138. }
  139. }
  140. }
  141. }
  142. }