Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 1 | #include "absl/strings/match.h" |
Jim Ostrowski | 12ef0ff | 2023-10-22 23:20:20 -0700 | [diff] [blame] | 2 | #include <opencv2/calib3d.hpp> |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 3 | #include <opencv2/highgui/highgui.hpp> |
| 4 | #include <opencv2/imgproc.hpp> |
| 5 | |
| 6 | #include "aos/events/shm_event_loop.h" |
| 7 | #include "aos/init.h" |
Ravago Jones | 17e13a2 | 2023-01-28 17:12:11 -0800 | [diff] [blame] | 8 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 9 | #include "aos/time/time.h" |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 10 | #include "frc971/constants/constants_sender_lib.h" |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 11 | #include "frc971/vision/vision_generated.h" |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 12 | #include "y2023/vision/vision_util.h" |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 13 | |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 14 | DEFINE_string(capture, "", |
| 15 | "If set, capture a single image and save it to this filename."); |
Jim Ostrowski | e2d6f66 | 2023-09-09 18:00:05 -0700 | [diff] [blame] | 16 | DEFINE_string(channel, "/camera", "Channel name for the image."); |
| 17 | DEFINE_string(config, "aos_config.json", "Path to the config file to use."); |
Austin Schuh | ceb9654 | 2023-02-04 11:43:33 -0800 | [diff] [blame] | 18 | DEFINE_int32(rate, 100, "Time in milliseconds to wait between images"); |
Jim Ostrowski | e2d6f66 | 2023-09-09 18:00:05 -0700 | [diff] [blame] | 19 | DEFINE_double(scale, 1.0, "Scale factor for images being displayed"); |
Austin Schuh | ceb9654 | 2023-02-04 11:43:33 -0800 | [diff] [blame] | 20 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 21 | namespace y2023::vision { |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 22 | namespace { |
| 23 | |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 24 | using frc971::vision::CameraImage; |
| 25 | |
| 26 | bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs, |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 27 | aos::Fetcher<CameraImage> *image_fetcher) { |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 28 | const CameraImage *image; |
| 29 | |
| 30 | // Read next image |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 31 | if (!image_fetcher->Fetch()) { |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 32 | VLOG(2) << "Couldn't fetch next image"; |
| 33 | return true; |
| 34 | } |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 35 | image = image_fetcher->get(); |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 36 | CHECK(image != nullptr) << "Couldn't read image"; |
| 37 | |
| 38 | // Create color image: |
| 39 | cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2, |
| 40 | (void *)image->data()->data()); |
| 41 | cv::Mat bgr_image(cv::Size(image->cols(), image->rows()), CV_8UC3); |
| 42 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
| 43 | |
| 44 | if (!FLAGS_capture.empty()) { |
Ravago Jones | 17e13a2 | 2023-01-28 17:12:11 -0800 | [diff] [blame] | 45 | if (absl::EndsWith(FLAGS_capture, ".bfbs")) { |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 46 | aos::WriteFlatbufferToFile(FLAGS_capture, |
| 47 | image_fetcher->CopyFlatBuffer()); |
Ravago Jones | 17e13a2 | 2023-01-28 17:12:11 -0800 | [diff] [blame] | 48 | } else { |
| 49 | cv::imwrite(FLAGS_capture, bgr_image); |
| 50 | } |
| 51 | |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 55 | cv::Mat undistorted_image; |
| 56 | cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs); |
Jim Ostrowski | e2d6f66 | 2023-09-09 18:00:05 -0700 | [diff] [blame] | 57 | if (FLAGS_scale != 1.0) { |
| 58 | cv::resize(undistorted_image, undistorted_image, cv::Size(), FLAGS_scale, |
| 59 | FLAGS_scale); |
| 60 | } |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 61 | cv::imshow("Display", undistorted_image); |
| 62 | |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 63 | int keystroke = cv::waitKey(1); |
| 64 | if ((keystroke & 0xFF) == static_cast<int>('c')) { |
| 65 | // Convert again, to get clean image |
| 66 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
| 67 | std::stringstream name; |
| 68 | name << "capture-" << aos::realtime_clock::now() << ".png"; |
| 69 | cv::imwrite(name.str(), bgr_image); |
| 70 | LOG(INFO) << "Saved image file: " << name.str(); |
| 71 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void ViewerMain() { |
| 78 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 79 | aos::configuration::ReadConfig(FLAGS_config); |
| 80 | |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 81 | frc971::constants::WaitForConstants<Constants>(&config.message()); |
| 82 | |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 83 | aos::ShmEventLoop event_loop(&config.message()); |
| 84 | |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 85 | frc971::constants::ConstantsFetcher<Constants> constants_fetcher(&event_loop); |
| 86 | const auto *calibration_data = FindCameraCalibration( |
| 87 | constants_fetcher.constants(), event_loop.node()->name()->string_view()); |
| 88 | const cv::Mat intrinsics = CameraIntrinsics(calibration_data); |
| 89 | const cv::Mat dist_coeffs = CameraDistCoeffs(calibration_data); |
| 90 | |
| 91 | aos::Fetcher<CameraImage> image_fetcher = |
| 92 | event_loop.MakeFetcher<CameraImage>(FLAGS_channel); |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 93 | |
| 94 | // Run the display loop |
| 95 | event_loop.AddPhasedLoop( |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 96 | [&](int) { |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 97 | if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) { |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 98 | LOG(INFO) << "Calling event_loop Exit"; |
| 99 | event_loop.Exit(); |
| 100 | }; |
| 101 | }, |
Austin Schuh | ceb9654 | 2023-02-04 11:43:33 -0800 | [diff] [blame] | 102 | ::std::chrono::milliseconds(FLAGS_rate)); |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 103 | |
| 104 | event_loop.Run(); |
| 105 | |
| 106 | image_fetcher = aos::Fetcher<CameraImage>(); |
| 107 | } |
| 108 | |
| 109 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 110 | } // namespace y2023::vision |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 111 | |
| 112 | int main(int argc, char **argv) { |
| 113 | aos::InitGoogle(&argc, &argv); |
milind-u | c3cf975 | 2023-02-20 23:07:30 -0800 | [diff] [blame] | 114 | y2023::vision::ViewerMain(); |
Austin Schuh | db2ed9d | 2022-12-26 14:02:26 -0800 | [diff] [blame] | 115 | } |