Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1 | #include "absl/flags/flag.h" |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 2 | #include <opencv2/calib3d.hpp> |
| 3 | #include <opencv2/features2d.hpp> |
| 4 | #include <opencv2/highgui/highgui.hpp> |
| 5 | #include <opencv2/imgproc.hpp> |
| 6 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 7 | #include "aos/events/logging/log_reader.h" |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 8 | #include "aos/events/simulated_event_loop.h" |
| 9 | #include "aos/init.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 10 | #include "frc971/vision/vision_generated.h" |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 12 | ABSL_FLAG(std::string, node, "pi1", "Node name to replay."); |
| 13 | ABSL_FLAG(std::string, image_save_prefix, "/tmp/img", |
| 14 | "Prefix to use for saving images from the logfile."); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 15 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 16 | namespace frc971::vision { |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | |
James Kuszmaul | 4567127 | 2021-08-01 20:42:53 -0700 | [diff] [blame] | 19 | void ViewerMain(int argc, char *argv[]) { |
James Kuszmaul | 4567127 | 2021-08-01 20:42:53 -0700 | [diff] [blame] | 20 | // open logfiles |
Austin Schuh | c160973 | 2023-06-26 11:51:28 -0700 | [diff] [blame] | 21 | aos::logger::LogReader reader( |
| 22 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 23 | reader.Register(); |
| 24 | const aos::Node *node = nullptr; |
| 25 | if (aos::configuration::MultiNode(reader.configuration())) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 26 | node = aos::configuration::GetNode(reader.configuration(), |
| 27 | absl::GetFlag(FLAGS_node)); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 28 | } |
| 29 | std::unique_ptr<aos::EventLoop> event_loop = |
| 30 | reader.event_loop_factory()->MakeEventLoop("player", node); |
| 31 | |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 32 | int image_count = 0; |
| 33 | event_loop->MakeWatcher("/camera", [&image_count](const CameraImage &image) { |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 34 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
| 35 | CHECK(image_mat.isContinuous()); |
| 36 | const int number_pixels = image.rows() * image.cols(); |
| 37 | for (int i = 0; i < number_pixels; ++i) { |
| 38 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 39 | image.data()->data()[i * 2]; |
| 40 | } |
| 41 | |
| 42 | cv::imshow("Display", image_mat); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 43 | if (!absl::GetFlag(FLAGS_image_save_prefix).empty()) { |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 44 | cv::imwrite("/tmp/img" + std::to_string(image_count++) + ".png", |
| 45 | image_mat); |
| 46 | } |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 47 | cv::waitKey(1); |
| 48 | }); |
| 49 | |
| 50 | reader.event_loop_factory()->Run(); |
| 51 | } |
| 52 | |
| 53 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 54 | } // namespace frc971::vision |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 55 | |
| 56 | // Quick and lightweight grayscale viewer for images |
| 57 | int main(int argc, char **argv) { |
| 58 | aos::InitGoogle(&argc, &argv); |
James Kuszmaul | 4567127 | 2021-08-01 20:42:53 -0700 | [diff] [blame] | 59 | frc971::vision::ViewerMain(argc, argv); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 60 | } |