Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 1 | #include "absl/strings/match.h" |
| 2 | #include <opencv2/calib3d.hpp> |
| 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" |
| 8 | #include "aos/json_to_flatbuffer.h" |
| 9 | #include "aos/time/time.h" |
| 10 | #include "frc971/constants/constants_sender_lib.h" |
| 11 | #include "frc971/vision/vision_generated.h" |
| 12 | #include "frc971/vision/vision_util_lib.h" |
| 13 | #include "y2024/vision/vision_util.h" |
| 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 15 | ABSL_FLAG(std::string, capture, "", |
| 16 | "If set, capture a single image and save it to this filename."); |
| 17 | ABSL_FLAG(std::string, channel, "/camera", "Channel name for the image."); |
| 18 | ABSL_FLAG(std::string, config, "aos_config.json", |
| 19 | "Path to the config file to use."); |
| 20 | ABSL_FLAG(int32_t, rate, 100, "Time in milliseconds to wait between images"); |
| 21 | ABSL_FLAG(double, scale, 1.0, "Scale factor for images being displayed"); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 22 | |
| 23 | namespace y2024::vision { |
| 24 | namespace { |
| 25 | |
| 26 | using frc971::vision::CameraImage; |
| 27 | |
| 28 | bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs, |
| 29 | aos::Fetcher<CameraImage> *image_fetcher) { |
| 30 | const CameraImage *image; |
| 31 | |
| 32 | // Read next image |
| 33 | if (!image_fetcher->Fetch()) { |
| 34 | VLOG(2) << "Couldn't fetch next image"; |
| 35 | return true; |
| 36 | } |
| 37 | image = image_fetcher->get(); |
| 38 | CHECK(image != nullptr) << "Couldn't read image"; |
| 39 | |
| 40 | // Create color image: |
| 41 | cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2, |
| 42 | (void *)image->data()->data()); |
| 43 | cv::Mat bgr_image(cv::Size(image->cols(), image->rows()), CV_8UC3); |
| 44 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
| 45 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 46 | if (!absl::GetFlag(FLAGS_capture).empty()) { |
| 47 | if (absl::EndsWith(absl::GetFlag(FLAGS_capture), ".bfbs")) { |
| 48 | aos::WriteFlatbufferToFile(absl::GetFlag(FLAGS_capture), |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 49 | image_fetcher->CopyFlatBuffer()); |
| 50 | } else { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 51 | cv::imwrite(absl::GetFlag(FLAGS_capture), bgr_image); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | cv::Mat undistorted_image; |
| 58 | cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 59 | if (absl::GetFlag(FLAGS_scale) != 1.0) { |
| 60 | cv::resize(undistorted_image, undistorted_image, cv::Size(), |
| 61 | absl::GetFlag(FLAGS_scale), absl::GetFlag(FLAGS_scale)); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 62 | } |
| 63 | cv::imshow("Display", undistorted_image); |
| 64 | |
| 65 | int keystroke = cv::waitKey(1); |
| 66 | if ((keystroke & 0xFF) == static_cast<int>('c')) { |
| 67 | // Convert again, to get clean image |
| 68 | cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV); |
| 69 | std::stringstream name; |
| 70 | name << "capture-" << aos::realtime_clock::now() << ".png"; |
| 71 | cv::imwrite(name.str(), bgr_image); |
| 72 | LOG(INFO) << "Saved image file: " << name.str(); |
| 73 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void ViewerMain() { |
| 80 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 81 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 82 | |
| 83 | frc971::constants::WaitForConstants<y2024::Constants>(&config.message()); |
| 84 | |
| 85 | aos::ShmEventLoop event_loop(&config.message()); |
| 86 | |
| 87 | frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher( |
| 88 | &event_loop); |
Jim Ostrowski | cc3d594 | 2024-06-29 18:05:54 -0700 | [diff] [blame^] | 89 | CHECK(absl::GetFlag(FLAGS_channel).length() == 8) |
| 90 | << " channel should be of the form '/cameraN' for viewing images from " |
| 91 | "camera N"; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 92 | int camera_id = std::stoi(absl::GetFlag(FLAGS_channel).substr(7, 1)); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 93 | const auto *calibration_data = FindCameraCalibration( |
Jim Ostrowski | ddebdcb | 2024-02-29 22:25:36 -0800 | [diff] [blame] | 94 | constants_fetcher.constants(), event_loop.node()->name()->string_view(), |
| 95 | camera_id); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 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)); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [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))); |
Jim Ostrowski | 9bf206a | 2024-01-26 23:31:58 -0800 | [diff] [blame] | 112 | |
| 113 | event_loop.Run(); |
| 114 | |
| 115 | image_fetcher = aos::Fetcher<CameraImage>(); |
| 116 | } |
| 117 | |
| 118 | } // namespace |
| 119 | } // namespace y2024::vision |
| 120 | |
| 121 | int main(int argc, char **argv) { |
| 122 | aos::InitGoogle(&argc, &argv); |
| 123 | y2024::vision::ViewerMain(); |
| 124 | } |