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 | |
| 6 | #include "aos/events/logging/logger.h" |
| 7 | #include "aos/events/simulated_event_loop.h" |
| 8 | #include "aos/init.h" |
| 9 | #include "y2020/vision/vision_generated.h" |
| 10 | |
| 11 | DEFINE_string(config, "y2020/config.json", "Path to the config file to use."); |
| 12 | DEFINE_string(logfile, "", "Path to the log file to use."); |
| 13 | DEFINE_string(node, "pi1", "Node name to replay."); |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 14 | DEFINE_string(image_save_prefix, "/tmp/img", |
| 15 | "Prefix to use for saving images from the logfile."); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 16 | |
| 17 | namespace frc971 { |
| 18 | namespace vision { |
| 19 | namespace { |
| 20 | |
| 21 | void ViewerMain() { |
| 22 | CHECK(!FLAGS_logfile.empty()) << "You forgot to specify a logfile."; |
| 23 | |
| 24 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 25 | aos::configuration::ReadConfig(FLAGS_config); |
| 26 | |
| 27 | aos::logger::LogReader reader(FLAGS_logfile, &config.message()); |
| 28 | reader.Register(); |
| 29 | const aos::Node *node = nullptr; |
| 30 | if (aos::configuration::MultiNode(reader.configuration())) { |
| 31 | node = aos::configuration::GetNode(reader.configuration(), FLAGS_node); |
| 32 | } |
| 33 | std::unique_ptr<aos::EventLoop> event_loop = |
| 34 | reader.event_loop_factory()->MakeEventLoop("player", node); |
| 35 | |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 36 | int image_count = 0; |
| 37 | event_loop->MakeWatcher("/camera", [&image_count](const CameraImage &image) { |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 38 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
| 39 | CHECK(image_mat.isContinuous()); |
| 40 | const int number_pixels = image.rows() * image.cols(); |
| 41 | for (int i = 0; i < number_pixels; ++i) { |
| 42 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 43 | image.data()->data()[i * 2]; |
| 44 | } |
| 45 | |
| 46 | cv::imshow("Display", image_mat); |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 47 | if (!FLAGS_image_save_prefix.empty()) { |
| 48 | cv::imwrite("/tmp/img" + std::to_string(image_count++) + ".png", |
| 49 | image_mat); |
| 50 | } |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 51 | cv::waitKey(1); |
| 52 | }); |
| 53 | |
| 54 | reader.event_loop_factory()->Run(); |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | } // namespace vision |
| 59 | } // namespace frc971 |
| 60 | |
| 61 | // Quick and lightweight grayscale viewer for images |
| 62 | int main(int argc, char **argv) { |
| 63 | aos::InitGoogle(&argc, &argv); |
| 64 | frc971::vision::ViewerMain(); |
| 65 | } |