Visualizer.cpp 5.6 KB

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