Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 1 | #include "opencv2/calib3d.hpp" |
| 2 | #include "opencv2/features2d.hpp" |
| 3 | #include "opencv2/highgui/highgui.hpp" |
| 4 | #include "opencv2/imgproc.hpp" |
| 5 | |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 6 | #include "aos/events/logging/log_reader.h" |
| 7 | #include "aos/events/simulated_event_loop.h" |
| 8 | #include "aos/init.h" |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
| 10 | #include "frc971/input/joystick_state_generated.h" |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 11 | #include "frc971/vision/vision_generated.h" |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 12 | #include "y2022/control_loops/superstructure/superstructure_status_generated.h" |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 13 | #include "y2022/vision/blob_detector.h" |
| 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 15 | ABSL_FLAG(std::string, pi, "pi3", "Node name to replay."); |
| 16 | ABSL_FLAG(std::string, image_save_prefix, "/tmp/img", |
| 17 | "Prefix to use for saving images from the logfile."); |
| 18 | ABSL_FLAG(bool, display, false, "If true, display the images with a timeout."); |
| 19 | ABSL_FLAG(bool, detected_only, false, |
| 20 | "If true, only write images which had blobs (unfiltered) detected"); |
| 21 | ABSL_FLAG(bool, filtered_only, false, |
| 22 | "If true, only write images which had blobs (filtered) detected"); |
| 23 | ABSL_FLAG(bool, match_timestamps, false, |
| 24 | "If true, name the files based on the time since the robot was " |
| 25 | "enabled (match start). Only consider images during this time"); |
| 26 | ABSL_FLAG(std::string, logger_pi_log, "/tmp/logger_pi/", |
| 27 | "Path to logger pi log"); |
| 28 | ABSL_FLAG(std::string, roborio_log, "/tmp/roborio/", "Path to roborio log"); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 29 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 30 | namespace y2022::vision { |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 31 | namespace { |
| 32 | |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 33 | using aos::monotonic_clock; |
| 34 | namespace superstructure = control_loops::superstructure; |
| 35 | |
| 36 | // Information to extract from the roborio log |
| 37 | struct ReplayData { |
| 38 | monotonic_clock::time_point match_start; |
| 39 | monotonic_clock::time_point match_end; |
| 40 | std::map<monotonic_clock::time_point, bool> robot_moving; |
| 41 | std::map<monotonic_clock::time_point, superstructure::SuperstructureState> |
| 42 | superstructure_states; |
| 43 | }; |
| 44 | |
| 45 | // Extract the useful data from the roborio log to be used for naming images |
| 46 | void ReplayRoborio(ReplayData *data) { |
| 47 | data->match_start = monotonic_clock::min_time; |
| 48 | data->match_end = monotonic_clock::min_time; |
| 49 | |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 50 | // Open logfiles |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 51 | aos::logger::LogReader reader(aos::logger::SortParts( |
| 52 | aos::logger::FindLogs(absl::GetFlag(FLAGS_roborio_log)))); |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 53 | reader.Register(); |
| 54 | const aos::Node *roborio = |
| 55 | aos::configuration::GetNode(reader.configuration(), "roborio"); |
| 56 | |
| 57 | std::unique_ptr<aos::EventLoop> event_loop = |
| 58 | reader.event_loop_factory()->MakeEventLoop("roborio", roborio); |
| 59 | |
| 60 | auto joystick_state_fetcher = |
| 61 | event_loop->MakeFetcher<aos::JoystickState>("/roborio/aos"); |
| 62 | auto drivetrain_status_fetcher = |
| 63 | event_loop->MakeFetcher<frc971::control_loops::drivetrain::Status>( |
| 64 | "/drivetrain"); |
| 65 | auto superstructure_status_fetcher = |
| 66 | event_loop->MakeFetcher<superstructure::Status>("/superstructure"); |
| 67 | |
| 68 | // Periodically check if the robot state updated |
| 69 | event_loop->AddPhasedLoop( |
| 70 | [&](int) { |
| 71 | // Find the match start and end times |
| 72 | if (joystick_state_fetcher.Fetch()) { |
| 73 | if (data->match_start == monotonic_clock::min_time && |
| 74 | joystick_state_fetcher->enabled()) { |
| 75 | data->match_start = |
| 76 | joystick_state_fetcher.context().monotonic_event_time; |
| 77 | } else { |
| 78 | if (data->match_end == monotonic_clock::min_time && |
| 79 | data->match_start != monotonic_clock::min_time && |
| 80 | !joystick_state_fetcher->autonomous() && |
| 81 | !joystick_state_fetcher->enabled()) { |
| 82 | data->match_end = |
| 83 | joystick_state_fetcher.context().monotonic_event_time; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Add whether the robot was moving at a non-negligible speed to |
| 89 | // the image name for debugging. |
| 90 | drivetrain_status_fetcher.Fetch(); |
| 91 | if (drivetrain_status_fetcher.get()) { |
| 92 | // If the robot speed was atleast this (m/s), it is |
| 93 | // considered moving. |
| 94 | constexpr double kMinMovingRobotSpeed = 0.5; |
| 95 | data->robot_moving[drivetrain_status_fetcher.context() |
| 96 | .monotonic_event_time] = |
| 97 | (std::abs(drivetrain_status_fetcher->robot_speed()) >= |
| 98 | kMinMovingRobotSpeed); |
| 99 | } |
| 100 | |
| 101 | superstructure_status_fetcher.Fetch(); |
| 102 | if (superstructure_status_fetcher.get()) { |
| 103 | data->superstructure_states[superstructure_status_fetcher.context() |
| 104 | .monotonic_event_time] = |
| 105 | superstructure_status_fetcher->state(); |
| 106 | } |
| 107 | }, |
| 108 | std::chrono::milliseconds(50)); |
| 109 | reader.event_loop_factory()->Run(); |
| 110 | } |
| 111 | |
| 112 | template <typename T> |
| 113 | T ClosestElement(const std::map<monotonic_clock::time_point, T> &map, |
| 114 | monotonic_clock::time_point now) { |
| 115 | T closest; |
| 116 | |
| 117 | // The closest element is either the one right above it, or the element before |
| 118 | // that one |
| 119 | auto closest_it = map.lower_bound(now); |
| 120 | // Handle the case where now is greater than all times in the map |
| 121 | if (closest_it == map.cend()) { |
| 122 | closest_it--; |
| 123 | closest = closest_it->second; |
| 124 | } else { |
| 125 | // Start off with the closest as the first after now |
| 126 | closest = closest_it->second; |
| 127 | const monotonic_clock::duration after_duration = closest_it->first - now; |
| 128 | closest_it--; |
| 129 | |
| 130 | // If there is a time before, check if that's closer to now |
| 131 | if (closest_it != map.cbegin()) { |
| 132 | const monotonic_clock::duration before_duration = now - closest_it->first; |
| 133 | if (before_duration < after_duration) { |
| 134 | closest = closest_it->second; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return closest; |
| 140 | } |
| 141 | |
| 142 | // Extract images from the pi logs |
| 143 | void ReplayPi(const ReplayData &data) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 144 | if (absl::GetFlag(FLAGS_match_timestamps)) { |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 145 | CHECK_NE(data.match_start, monotonic_clock::min_time) |
| 146 | << "Can't use match timestamps if match never started"; |
| 147 | CHECK_NE(data.match_end, monotonic_clock::min_time) |
| 148 | << "Can't use match timestamps if match never ended"; |
| 149 | } |
| 150 | |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 151 | // Open logfiles |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 152 | aos::logger::LogReader reader(aos::logger::SortParts( |
| 153 | aos::logger::FindLogs(absl::GetFlag(FLAGS_logger_pi_log)))); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 154 | reader.Register(); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 155 | const aos::Node *pi = aos::configuration::GetNode(reader.configuration(), |
| 156 | absl::GetFlag(FLAGS_pi)); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 157 | |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 158 | std::unique_ptr<aos::EventLoop> event_loop = |
| 159 | reader.event_loop_factory()->MakeEventLoop("player", pi); |
| 160 | |
| 161 | LOG(INFO) << "Match start: " << data.match_start |
| 162 | << ", match end: " << data.match_end; |
| 163 | |
| 164 | size_t nonmatch_image_count = 0; |
| 165 | |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 166 | event_loop->MakeWatcher( |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 167 | "/camera/decimated", [&](const frc971::vision::CameraImage &image) { |
| 168 | const auto match_start = data.match_start; |
| 169 | // Find the closest robot moving and superstructure state to now |
| 170 | const bool robot_moving = |
| 171 | ClosestElement(data.robot_moving, event_loop->monotonic_now()); |
| 172 | const auto superstructure_state = ClosestElement( |
| 173 | data.superstructure_states, event_loop->monotonic_now()); |
| 174 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 175 | if (absl::GetFlag(FLAGS_match_timestamps)) { |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 176 | if (event_loop->monotonic_now() < data.match_start) { |
| 177 | // Ignore prematch images if we only care about ones during the |
| 178 | // match |
| 179 | return; |
| 180 | } else if (event_loop->monotonic_now() >= data.match_end) { |
| 181 | // We're done if the match is over and we only wanted match images |
| 182 | reader.event_loop_factory()->Exit(); |
| 183 | return; |
| 184 | } |
| 185 | } |
| 186 | |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 187 | // Create color image: |
| 188 | cv::Mat image_color_mat(cv::Size(image.cols(), image.rows()), CV_8UC2, |
| 189 | (void *)image.data()->data()); |
| 190 | cv::Mat image_mat(cv::Size(image.cols(), image.rows()), CV_8UC3); |
| 191 | cv::cvtColor(image_color_mat, image_mat, cv::COLOR_YUV2BGR_YUYV); |
| 192 | |
| 193 | bool use_image = true; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 194 | if (absl::GetFlag(FLAGS_detected_only) || |
| 195 | absl::GetFlag(FLAGS_filtered_only)) { |
Milind Upadhyay | a31f027 | 2022-04-03 13:55:22 -0700 | [diff] [blame] | 196 | // TODO(milind): if adding target estimation here in the future, |
| 197 | // undistortion is needed |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 198 | BlobDetector::BlobResult blob_result; |
| 199 | BlobDetector::ExtractBlobs(image_mat, &blob_result); |
| 200 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 201 | use_image = ((absl::GetFlag(FLAGS_filtered_only) |
| 202 | ? blob_result.filtered_blobs.size() |
| 203 | : blob_result.unfiltered_blobs.size()) > 0); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 204 | } |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 205 | |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 206 | if (use_image) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 207 | if (!absl::GetFlag(FLAGS_image_save_prefix).empty()) { |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 208 | std::stringstream image_name; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 209 | image_name << absl::GetFlag(FLAGS_image_save_prefix); |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 210 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 211 | if (absl::GetFlag(FLAGS_match_timestamps)) { |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 212 | // Add the time since match start into the image for debugging. |
| 213 | // We can match images with the game recording. |
| 214 | image_name << "match_" |
| 215 | << std::chrono::duration_cast<std::chrono::seconds>( |
| 216 | event_loop->monotonic_now() - match_start) |
| 217 | .count() |
| 218 | << 's'; |
| 219 | } else { |
| 220 | image_name << nonmatch_image_count++; |
| 221 | } |
| 222 | |
| 223 | // Add superstructure state to the filename |
| 224 | if (superstructure_state != |
| 225 | superstructure::SuperstructureState::IDLE) { |
| 226 | std::string superstructure_state_name = |
| 227 | superstructure::EnumNameSuperstructureState( |
| 228 | superstructure_state); |
| 229 | std::transform(superstructure_state_name.begin(), |
| 230 | superstructure_state_name.end(), |
| 231 | superstructure_state_name.begin(), |
| 232 | [](char c) { return std::tolower(c); }); |
| 233 | image_name << '_' << superstructure_state_name; |
| 234 | } |
| 235 | |
| 236 | if (robot_moving) { |
| 237 | image_name << "_moving"; |
| 238 | } |
| 239 | |
| 240 | image_name << ".png"; |
| 241 | |
| 242 | cv::imwrite(image_name.str(), image_mat); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 243 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 244 | if (absl::GetFlag(FLAGS_display)) { |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 245 | cv::imshow("Display", image_mat); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 246 | cv::waitKey(absl::GetFlag(FLAGS_detected_only) || |
| 247 | absl::GetFlag(FLAGS_filtered_only) |
| 248 | ? 10 |
| 249 | : 1); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | }); |
| 253 | |
| 254 | reader.event_loop_factory()->Run(); |
| 255 | } |
| 256 | |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 257 | void ViewerMain() { |
| 258 | ReplayData data; |
| 259 | ReplayRoborio(&data); |
| 260 | ReplayPi(data); |
| 261 | } |
| 262 | |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 263 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 264 | } // namespace y2022::vision |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 265 | |
| 266 | // Quick and lightweight viewer for image logs |
| 267 | int main(int argc, char **argv) { |
| 268 | aos::InitGoogle(&argc, &argv); |
Milind Upadhyay | e321586 | 2022-03-24 19:59:19 -0700 | [diff] [blame] | 269 | y2022::vision::ViewerMain(); |
Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 270 | } |