blob: e03680cf61b76ea0df43b1d1ca1b2725122a201c [file] [log] [blame]
Philipp Schrader790cb542023-07-05 21:06:52 -07001#include "absl/strings/match.h"
Jim Ostrowski12ef0ff2023-10-22 23:20:20 -07002#include <opencv2/calib3d.hpp>
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08003#include <opencv2/highgui/highgui.hpp>
4#include <opencv2/imgproc.hpp>
5
6#include "aos/events/shm_event_loop.h"
7#include "aos/init.h"
Ravago Jones17e13a22023-01-28 17:12:11 -08008#include "aos/json_to_flatbuffer.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08009#include "aos/time/time.h"
milind-uc3cf9752023-02-20 23:07:30 -080010#include "frc971/constants/constants_sender_lib.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080011#include "frc971/vision/vision_generated.h"
milind-uc3cf9752023-02-20 23:07:30 -080012#include "y2023/vision/vision_util.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080013
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080014DEFINE_string(capture, "",
15 "If set, capture a single image and save it to this filename.");
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070016DEFINE_string(channel, "/camera", "Channel name for the image.");
17DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
Austin Schuhceb96542023-02-04 11:43:33 -080018DEFINE_int32(rate, 100, "Time in milliseconds to wait between images");
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070019DEFINE_double(scale, 1.0, "Scale factor for images being displayed");
Austin Schuhceb96542023-02-04 11:43:33 -080020
milind-uc3cf9752023-02-20 23:07:30 -080021namespace y2023 {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080022namespace vision {
23namespace {
24
milind-uc3cf9752023-02-20 23:07:30 -080025using frc971::vision::CameraImage;
26
27bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs,
milind-u7aa29e22023-02-23 20:22:01 -080028 aos::Fetcher<CameraImage> *image_fetcher) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080029 const CameraImage *image;
30
31 // Read next image
milind-uc3cf9752023-02-20 23:07:30 -080032 if (!image_fetcher->Fetch()) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080033 VLOG(2) << "Couldn't fetch next image";
34 return true;
35 }
milind-uc3cf9752023-02-20 23:07:30 -080036 image = image_fetcher->get();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080037 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()) {
Ravago Jones17e13a22023-01-28 17:12:11 -080046 if (absl::EndsWith(FLAGS_capture, ".bfbs")) {
milind-uc3cf9752023-02-20 23:07:30 -080047 aos::WriteFlatbufferToFile(FLAGS_capture,
48 image_fetcher->CopyFlatBuffer());
Ravago Jones17e13a22023-01-28 17:12:11 -080049 } else {
50 cv::imwrite(FLAGS_capture, bgr_image);
51 }
52
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080053 return false;
54 }
55
milind-uc3cf9752023-02-20 23:07:30 -080056 cv::Mat undistorted_image;
57 cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs);
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070058 if (FLAGS_scale != 1.0) {
59 cv::resize(undistorted_image, undistorted_image, cv::Size(), FLAGS_scale,
60 FLAGS_scale);
61 }
milind-uc3cf9752023-02-20 23:07:30 -080062 cv::imshow("Display", undistorted_image);
63
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080064 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
milind-uc3cf9752023-02-20 23:07:30 -080082 frc971::constants::WaitForConstants<Constants>(&config.message());
83
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080084 aos::ShmEventLoop event_loop(&config.message());
85
milind-uc3cf9752023-02-20 23:07:30 -080086 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(&event_loop);
87 const auto *calibration_data = FindCameraCalibration(
88 constants_fetcher.constants(), event_loop.node()->name()->string_view());
89 const cv::Mat intrinsics = CameraIntrinsics(calibration_data);
90 const cv::Mat dist_coeffs = CameraDistCoeffs(calibration_data);
91
92 aos::Fetcher<CameraImage> image_fetcher =
93 event_loop.MakeFetcher<CameraImage>(FLAGS_channel);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080094
95 // Run the display loop
96 event_loop.AddPhasedLoop(
milind-uc3cf9752023-02-20 23:07:30 -080097 [&](int) {
milind-u7aa29e22023-02-23 20:22:01 -080098 if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080099 LOG(INFO) << "Calling event_loop Exit";
100 event_loop.Exit();
101 };
102 },
Austin Schuhceb96542023-02-04 11:43:33 -0800103 ::std::chrono::milliseconds(FLAGS_rate));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800104
105 event_loop.Run();
106
107 image_fetcher = aos::Fetcher<CameraImage>();
108}
109
110} // namespace
111} // namespace vision
milind-uc3cf9752023-02-20 23:07:30 -0800112} // namespace y2023
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}