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