blob: c72c08e1ff9bc04657b13eb87b085843c15d0406 [file] [log] [blame]
Jim Ostrowski9bf206a2024-01-26 23:31:58 -08001#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 Schuh99f7c6a2024-06-25 22:07:44 -070015ABSL_FLAG(std::string, capture, "",
16 "If set, capture a single image and save it to this filename.");
17ABSL_FLAG(std::string, channel, "/camera", "Channel name for the image.");
18ABSL_FLAG(std::string, config, "aos_config.json",
19 "Path to the config file to use.");
20ABSL_FLAG(int32_t, rate, 100, "Time in milliseconds to wait between images");
21ABSL_FLAG(double, scale, 1.0, "Scale factor for images being displayed");
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080022
23namespace y2024::vision {
24namespace {
25
26using frc971::vision::CameraImage;
27
28bool 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 Schuh99f7c6a2024-06-25 22:07:44 -070046 if (!absl::GetFlag(FLAGS_capture).empty()) {
47 if (absl::EndsWith(absl::GetFlag(FLAGS_capture), ".bfbs")) {
48 aos::WriteFlatbufferToFile(absl::GetFlag(FLAGS_capture),
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080049 image_fetcher->CopyFlatBuffer());
50 } else {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070051 cv::imwrite(absl::GetFlag(FLAGS_capture), bgr_image);
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080052 }
53
54 return false;
55 }
56
57 cv::Mat undistorted_image;
58 cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070059 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 Ostrowski9bf206a2024-01-26 23:31:58 -080062 }
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
79void ViewerMain() {
80 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070081 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080082
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);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070089 CHECK(absl::GetFlag(FLAGS_channel).length() == 8);
90 int camera_id = std::stoi(absl::GetFlag(FLAGS_channel).substr(7, 1));
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080091 const auto *calibration_data = FindCameraCalibration(
Jim Ostrowskiddebdcb2024-02-29 22:25:36 -080092 constants_fetcher.constants(), event_loop.node()->name()->string_view(),
93 camera_id);
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080094 const cv::Mat intrinsics = frc971::vision::CameraIntrinsics(calibration_data);
95 const cv::Mat dist_coeffs =
96 frc971::vision::CameraDistCoeffs(calibration_data);
97
98 aos::Fetcher<CameraImage> image_fetcher =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070099 event_loop.MakeFetcher<CameraImage>(absl::GetFlag(FLAGS_channel));
Jim Ostrowski9bf206a2024-01-26 23:31:58 -0800100
101 // Run the display loop
102 event_loop.AddPhasedLoop(
103 [&](int) {
104 if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) {
105 LOG(INFO) << "Calling event_loop Exit";
106 event_loop.Exit();
107 };
108 },
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700109 ::std::chrono::milliseconds(absl::GetFlag(FLAGS_rate)));
Jim Ostrowski9bf206a2024-01-26 23:31:58 -0800110
111 event_loop.Run();
112
113 image_fetcher = aos::Fetcher<CameraImage>();
114}
115
116} // namespace
117} // namespace y2024::vision
118
119int main(int argc, char **argv) {
120 aos::InitGoogle(&argc, &argv);
121 y2024::vision::ViewerMain();
122}