blob: c1bf88b10fb417820a25392df99beaeb60c918bf [file] [log] [blame]
Milind Upadhyayb7e3c242022-03-12 20:05:25 -08001#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
11DEFINE_string(node, "pi1", "Node name to replay.");
12DEFINE_string(image_save_prefix, "/tmp/img",
13 "Prefix to use for saving images from the logfile.");
14DEFINE_bool(display, false, "If true, display the images with a timeout.");
15DEFINE_bool(detected_only, false,
16 "If true, only write images which had blobs (unfiltered) detected");
17DEFINE_bool(filtered_only, false,
18 "If true, only write images which had blobs (filtered) detected");
19
20namespace y2022 {
21namespace vision {
22namespace {
23
24void 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
78int main(int argc, char **argv) {
79 aos::InitGoogle(&argc, &argv);
80 y2022::vision::ViewerMain(argc, argv);
81}