blob: 83ef483a193804f7763c11f1ccc09063ff3ae7b8 [file] [log] [blame]
Austin Schuh99f7c6a2024-06-25 22:07:44 -07001#include "absl/flags/flag.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07002#include "absl/strings/match.h"
Jim Ostrowski12ef0ff2023-10-22 23:20:20 -07003#include <opencv2/calib3d.hpp>
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08004#include <opencv2/highgui/highgui.hpp>
5#include <opencv2/imgproc.hpp>
6
7#include "aos/events/shm_event_loop.h"
8#include "aos/init.h"
Ravago Jones17e13a22023-01-28 17:12:11 -08009#include "aos/json_to_flatbuffer.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080010#include "aos/time/time.h"
milind-uc3cf9752023-02-20 23:07:30 -080011#include "frc971/constants/constants_sender_lib.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080012#include "frc971/vision/vision_generated.h"
milind-uc3cf9752023-02-20 23:07:30 -080013#include "y2023/vision/vision_util.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080014
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");
Austin Schuhceb96542023-02-04 11:43:33 -080022
Stephan Pleinesf63bde82024-01-13 15:59:33 -080023namespace y2023::vision {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080024namespace {
25
milind-uc3cf9752023-02-20 23:07:30 -080026using frc971::vision::CameraImage;
27
28bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs,
milind-u7aa29e22023-02-23 20:22:01 -080029 aos::Fetcher<CameraImage> *image_fetcher) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080030 const CameraImage *image;
31
32 // Read next image
milind-uc3cf9752023-02-20 23:07:30 -080033 if (!image_fetcher->Fetch()) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080034 VLOG(2) << "Couldn't fetch next image";
35 return true;
36 }
milind-uc3cf9752023-02-20 23:07:30 -080037 image = image_fetcher->get();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080038 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),
milind-uc3cf9752023-02-20 23:07:30 -080049 image_fetcher->CopyFlatBuffer());
Ravago Jones17e13a22023-01-28 17:12:11 -080050 } else {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070051 cv::imwrite(absl::GetFlag(FLAGS_capture), bgr_image);
Ravago Jones17e13a22023-01-28 17:12:11 -080052 }
53
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080054 return false;
55 }
56
milind-uc3cf9752023-02-20 23:07:30 -080057 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 Ostrowskie2d6f662023-09-09 18:00:05 -070062 }
milind-uc3cf9752023-02-20 23:07:30 -080063 cv::imshow("Display", undistorted_image);
64
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080065 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));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080082
milind-uc3cf9752023-02-20 23:07:30 -080083 frc971::constants::WaitForConstants<Constants>(&config.message());
84
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080085 aos::ShmEventLoop event_loop(&config.message());
86
milind-uc3cf9752023-02-20 23:07:30 -080087 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(&event_loop);
88 const auto *calibration_data = FindCameraCalibration(
89 constants_fetcher.constants(), event_loop.node()->name()->string_view());
90 const cv::Mat intrinsics = CameraIntrinsics(calibration_data);
91 const cv::Mat dist_coeffs = CameraDistCoeffs(calibration_data);
92
93 aos::Fetcher<CameraImage> image_fetcher =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070094 event_loop.MakeFetcher<CameraImage>(absl::GetFlag(FLAGS_channel));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080095
96 // Run the display loop
97 event_loop.AddPhasedLoop(
milind-uc3cf9752023-02-20 23:07:30 -080098 [&](int) {
milind-u7aa29e22023-02-23 20:22:01 -080099 if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800100 LOG(INFO) << "Calling event_loop Exit";
101 event_loop.Exit();
102 };
103 },
Austin Schuh99f7c6a2024-06-25 22:07:44 -0700104 ::std::chrono::milliseconds(absl::GetFlag(FLAGS_rate)));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800105
106 event_loop.Run();
107
108 image_fetcher = aos::Fetcher<CameraImage>();
109}
110
111} // namespace
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800112} // namespace y2023::vision
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800113
114int main(int argc, char **argv) {
115 aos::InitGoogle(&argc, &argv);
milind-uc3cf9752023-02-20 23:07:30 -0800116 y2023::vision::ViewerMain();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800117}