Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1 | #include "absl/flags/flag.h" |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 2 | #include "absl/strings/match.h" |
| 3 | #include "opencv2/calib3d.hpp" |
| 4 | #include "opencv2/highgui/highgui.hpp" |
| 5 | #include "opencv2/imgproc.hpp" |
| 6 | |
| 7 | #include "aos/events/shm_event_loop.h" |
| 8 | #include "aos/init.h" |
| 9 | #include "aos/json_to_flatbuffer.h" |
| 10 | #include "aos/time/time.h" |
| 11 | #include "frc971/constants/constants_sender_lib.h" |
| 12 | #include "frc971/vision/vision_generated.h" |
| 13 | #include "frc971/vision/vision_util_lib.h" |
| 14 | #include "y2024_swerve/vision/vision_util.h" |
| 15 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 16 | ABSL_FLAG(std::string, capture, "", |
| 17 | "If set, capture a single image and save it to this filename."); |
| 18 | ABSL_FLAG(std::string, channel, "/camera", "Channel name for the image."); |
| 19 | ABSL_FLAG(std::string, config, "aos_config.json", |
| 20 | "Path to the config file to use."); |
| 21 | ABSL_FLAG(int32_t, rate, 100, "Time in milliseconds to wait between images"); |
| 22 | ABSL_FLAG(double, scale, 1.0, "Scale factor for images being displayed"); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 23 | |
| 24 | namespace y2024_swerve::vision { |
| 25 | namespace { |
| 26 | |
| 27 | using frc971::vision::CameraImage; |
| 28 | |
| 29 | bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs, |
| 30 | aos::Fetcher<CameraImage> *image_fetcher) { |
| 31 | const CameraImage *image; |
| 32 | |
| 33 | // Read next image |
| 34 | if (!image_fetcher->Fetch()) { |
| 35 | VLOG(2) << "Couldn't fetch next image"; |
| 36 | return true; |
| 37 | } |
| 38 | image = image_fetcher->get(); |
| 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 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 47 | if (!absl::GetFlag(FLAGS_capture).empty()) { |
| 48 | if (absl::EndsWith(absl::GetFlag(FLAGS_capture), ".bfbs")) { |
| 49 | aos::WriteFlatbufferToFile(absl::GetFlag(FLAGS_capture), |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 50 | image_fetcher->CopyFlatBuffer()); |
| 51 | } else { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 52 | cv::imwrite(absl::GetFlag(FLAGS_capture), bgr_image); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | cv::Mat undistorted_image; |
| 59 | cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 60 | if (absl::GetFlag(FLAGS_scale) != 1.0) { |
| 61 | cv::resize(undistorted_image, undistorted_image, cv::Size(), |
| 62 | absl::GetFlag(FLAGS_scale), absl::GetFlag(FLAGS_scale)); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 63 | } |
| 64 | cv::imshow("Display", undistorted_image); |
| 65 | |
| 66 | int keystroke = cv::waitKey(1); |
| 67 | if ((keystroke & 0xFF) == static_cast<int>('c')) { |
| 68 | // Convert again, to get clean image |
| 69 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
| 70 | std::stringstream name; |
| 71 | name << "capture-" << aos::realtime_clock::now() << ".png"; |
| 72 | cv::imwrite(name.str(), bgr_image); |
| 73 | LOG(INFO) << "Saved image file: " << name.str(); |
| 74 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void ViewerMain() { |
| 81 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 82 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 83 | |
| 84 | frc971::constants::WaitForConstants<y2024_swerve::Constants>( |
| 85 | &config.message()); |
| 86 | |
| 87 | aos::ShmEventLoop event_loop(&config.message()); |
| 88 | |
| 89 | frc971::constants::ConstantsFetcher<y2024_swerve::Constants> |
| 90 | constants_fetcher(&event_loop); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 91 | CHECK(absl::GetFlag(FLAGS_channel).length() == 8); |
| 92 | int camera_id = std::stoi(absl::GetFlag(FLAGS_channel).substr(7, 1)); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 93 | const auto *calibration_data = FindCameraCalibration( |
| 94 | constants_fetcher.constants(), event_loop.node()->name()->string_view(), |
| 95 | camera_id); |
| 96 | const cv::Mat intrinsics = frc971::vision::CameraIntrinsics(calibration_data); |
| 97 | const cv::Mat dist_coeffs = |
| 98 | frc971::vision::CameraDistCoeffs(calibration_data); |
| 99 | |
| 100 | aos::Fetcher<CameraImage> image_fetcher = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 101 | event_loop.MakeFetcher<CameraImage>(absl::GetFlag(FLAGS_channel)); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 102 | |
| 103 | // Run the display loop |
| 104 | event_loop.AddPhasedLoop( |
| 105 | [&](int) { |
| 106 | if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) { |
| 107 | LOG(INFO) << "Calling event_loop Exit"; |
| 108 | event_loop.Exit(); |
| 109 | }; |
| 110 | }, |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 111 | ::std::chrono::milliseconds(absl::GetFlag(FLAGS_rate))); |
Yash Maheshwari | e0b25c5 | 2024-05-22 20:23:36 -0700 | [diff] [blame] | 112 | |
| 113 | event_loop.Run(); |
| 114 | |
| 115 | image_fetcher = aos::Fetcher<CameraImage>(); |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | } // namespace y2024_swerve::vision |
| 120 | |
| 121 | int main(int argc, char **argv) { |
| 122 | aos::InitGoogle(&argc, &argv); |
| 123 | y2024_swerve::vision::ViewerMain(); |
| 124 | } |