blob: d857cb67c68ba705f1691e7f3fcb832160bb1332 [file] [log] [blame]
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001#include "absl/flags/flag.h"
Yash Maheshwarie0b25c52024-05-22 20:23:36 -07002#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 Schuh99f7c6a2024-06-25 22:07:44 -070016ABSL_FLAG(std::string, capture, "",
17 "If set, capture a single image and save it to this filename.");
18ABSL_FLAG(std::string, channel, "/camera", "Channel name for the image.");
19ABSL_FLAG(std::string, config, "aos_config.json",
20 "Path to the config file to use.");
21ABSL_FLAG(int32_t, rate, 100, "Time in milliseconds to wait between images");
22ABSL_FLAG(double, scale, 1.0, "Scale factor for images being displayed");
Yash Maheshwarie0b25c52024-05-22 20:23:36 -070023
24namespace y2024_swerve::vision {
25namespace {
26
27using frc971::vision::CameraImage;
28
29bool 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 Schuh99f7c6a2024-06-25 22:07:44 -070047 if (!absl::GetFlag(FLAGS_capture).empty()) {
48 if (absl::EndsWith(absl::GetFlag(FLAGS_capture), ".bfbs")) {
49 aos::WriteFlatbufferToFile(absl::GetFlag(FLAGS_capture),
Yash Maheshwarie0b25c52024-05-22 20:23:36 -070050 image_fetcher->CopyFlatBuffer());
51 } else {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070052 cv::imwrite(absl::GetFlag(FLAGS_capture), bgr_image);
Yash Maheshwarie0b25c52024-05-22 20:23:36 -070053 }
54
55 return false;
56 }
57
58 cv::Mat undistorted_image;
59 cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070060 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 Maheshwarie0b25c52024-05-22 20:23:36 -070063 }
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
80void ViewerMain() {
81 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070082 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
Yash Maheshwarie0b25c52024-05-22 20:23:36 -070083
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 Schuh99f7c6a2024-06-25 22:07:44 -070091 CHECK(absl::GetFlag(FLAGS_channel).length() == 8);
92 int camera_id = std::stoi(absl::GetFlag(FLAGS_channel).substr(7, 1));
Yash Maheshwarie0b25c52024-05-22 20:23:36 -070093 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 Schuh99f7c6a2024-06-25 22:07:44 -0700101 event_loop.MakeFetcher<CameraImage>(absl::GetFlag(FLAGS_channel));
Yash Maheshwarie0b25c52024-05-22 20:23:36 -0700102
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 Schuh99f7c6a2024-06-25 22:07:44 -0700111 ::std::chrono::milliseconds(absl::GetFlag(FLAGS_rate)));
Yash Maheshwarie0b25c52024-05-22 20:23:36 -0700112
113 event_loop.Run();
114
115 image_fetcher = aos::Fetcher<CameraImage>();
116}
117
118} // namespace
119} // namespace y2024_swerve::vision
120
121int main(int argc, char **argv) {
122 aos::InitGoogle(&argc, &argv);
123 y2024_swerve::vision::ViewerMain();
124}