Milind Upadhyay | b7e3c24 | 2022-03-12 20:05:25 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/log_reader.h" |
| 2 | #include "aos/events/simulated_event_loop.h" |
| 3 | #include "aos/init.h" |
| 4 | #include "frc971/vision/vision_generated.h" |
| 5 | #include "opencv2/calib3d.hpp" |
| 6 | #include "opencv2/features2d.hpp" |
| 7 | #include "opencv2/highgui/highgui.hpp" |
| 8 | #include "opencv2/imgproc.hpp" |
| 9 | #include "y2022/vision/blob_detector.h" |
| 10 | |
| 11 | DEFINE_string(node, "pi1", "Node name to replay."); |
| 12 | DEFINE_string(image_save_prefix, "/tmp/img", |
| 13 | "Prefix to use for saving images from the logfile."); |
| 14 | DEFINE_bool(display, false, "If true, display the images with a timeout."); |
| 15 | DEFINE_bool(detected_only, false, |
| 16 | "If true, only write images which had blobs (unfiltered) detected"); |
| 17 | DEFINE_bool(filtered_only, false, |
| 18 | "If true, only write images which had blobs (filtered) detected"); |
| 19 | |
| 20 | namespace y2022 { |
| 21 | namespace vision { |
| 22 | namespace { |
| 23 | |
| 24 | void ViewerMain(int argc, char *argv[]) { |
| 25 | std::vector<std::string> unsorted_logfiles = |
| 26 | aos::logger::FindLogs(argc, argv); |
| 27 | |
| 28 | // Open logfiles |
| 29 | aos::logger::LogReader reader(aos::logger::SortParts(unsorted_logfiles)); |
| 30 | reader.Register(); |
| 31 | const aos::Node *node = nullptr; |
| 32 | if (aos::configuration::MultiNode(reader.configuration())) { |
| 33 | node = aos::configuration::GetNode(reader.configuration(), FLAGS_node); |
| 34 | } |
| 35 | std::unique_ptr<aos::EventLoop> event_loop = |
| 36 | reader.event_loop_factory()->MakeEventLoop("player", node); |
| 37 | |
| 38 | int image_count = 0; |
| 39 | event_loop->MakeWatcher( |
| 40 | "/camera/decimated", |
| 41 | [&image_count](const frc971::vision::CameraImage &image) { |
| 42 | // Create color image: |
| 43 | cv::Mat image_color_mat(cv::Size(image.cols(), image.rows()), CV_8UC2, |
| 44 | (void *)image.data()->data()); |
| 45 | cv::Mat image_mat(cv::Size(image.cols(), image.rows()), CV_8UC3); |
| 46 | cv::cvtColor(image_color_mat, image_mat, cv::COLOR_YUV2BGR_YUYV); |
| 47 | |
| 48 | bool use_image = true; |
| 49 | if (FLAGS_detected_only || FLAGS_filtered_only) { |
| 50 | BlobDetector::BlobResult blob_result; |
| 51 | BlobDetector::ExtractBlobs(image_mat, &blob_result); |
| 52 | |
| 53 | use_image = |
| 54 | ((FLAGS_filtered_only ? blob_result.filtered_blobs.size() |
| 55 | : blob_result.unfiltered_blobs.size()) > 0); |
| 56 | } |
| 57 | if (use_image) { |
| 58 | if (!FLAGS_image_save_prefix.empty()) { |
| 59 | cv::imwrite(FLAGS_image_save_prefix + |
| 60 | std::to_string(image_count++) + ".png", |
| 61 | image_mat); |
| 62 | } |
| 63 | if (FLAGS_display) { |
| 64 | cv::imshow("Display", image_mat); |
| 65 | cv::waitKey(FLAGS_detected_only || FLAGS_filtered_only ? 10 : 1); |
| 66 | } |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | reader.event_loop_factory()->Run(); |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | } // namespace vision |
| 75 | } // namespace y2022 |
| 76 | |
| 77 | // Quick and lightweight viewer for image logs |
| 78 | int main(int argc, char **argv) { |
| 79 | aos::InitGoogle(&argc, &argv); |
| 80 | y2022::vision::ViewerMain(argc, argv); |
| 81 | } |