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