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