blob: d5ada142aa722e54d38a992936bde9dbb2681c6d [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
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::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::Constants>(&config.message());
83
84 aos::ShmEventLoop event_loop(&config.message());
85
86 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
87 &event_loop);
Jim Ostrowskiddebdcb2024-02-29 22:25:36 -080088 CHECK(FLAGS_channel.length() == 8);
89 int camera_id = std::stoi(FLAGS_channel.substr(7, 1));
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080090 const auto *calibration_data = FindCameraCalibration(
Jim Ostrowskiddebdcb2024-02-29 22:25:36 -080091 constants_fetcher.constants(), event_loop.node()->name()->string_view(),
92 camera_id);
Jim Ostrowski9bf206a2024-01-26 23:31:58 -080093 const cv::Mat intrinsics = frc971::vision::CameraIntrinsics(calibration_data);
94 const cv::Mat dist_coeffs =
95 frc971::vision::CameraDistCoeffs(calibration_data);
96
97 aos::Fetcher<CameraImage> image_fetcher =
98 event_loop.MakeFetcher<CameraImage>(FLAGS_channel);
99
100 // Run the display loop
101 event_loop.AddPhasedLoop(
102 [&](int) {
103 if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) {
104 LOG(INFO) << "Calling event_loop Exit";
105 event_loop.Exit();
106 };
107 },
108 ::std::chrono::milliseconds(FLAGS_rate));
109
110 event_loop.Run();
111
112 image_fetcher = aos::Fetcher<CameraImage>();
113}
114
115} // namespace
116} // namespace y2024::vision
117
118int main(int argc, char **argv) {
119 aos::InitGoogle(&argc, &argv);
120 y2024::vision::ViewerMain();
121}