blob: 78731e5d9e3cfea45a2ee3b2a8b70e14d47e2840 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080021namespace y2023::vision {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080022namespace {
23
milind-uc3cf9752023-02-20 23:07:30 -080024using frc971::vision::CameraImage;
25
26bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs,
milind-u7aa29e22023-02-23 20:22:01 -080027 aos::Fetcher<CameraImage> *image_fetcher) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080028 const CameraImage *image;
29
30 // Read next image
milind-uc3cf9752023-02-20 23:07:30 -080031 if (!image_fetcher->Fetch()) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080032 VLOG(2) << "Couldn't fetch next image";
33 return true;
34 }
milind-uc3cf9752023-02-20 23:07:30 -080035 image = image_fetcher->get();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080036 CHECK(image != nullptr) << "Couldn't read image";
37
38 // Create color image:
39 cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2,
40 (void *)image->data()->data());
41 cv::Mat bgr_image(cv::Size(image->cols(), image->rows()), CV_8UC3);
42 cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV);
43
44 if (!FLAGS_capture.empty()) {
Ravago Jones17e13a22023-01-28 17:12:11 -080045 if (absl::EndsWith(FLAGS_capture, ".bfbs")) {
milind-uc3cf9752023-02-20 23:07:30 -080046 aos::WriteFlatbufferToFile(FLAGS_capture,
47 image_fetcher->CopyFlatBuffer());
Ravago Jones17e13a22023-01-28 17:12:11 -080048 } else {
49 cv::imwrite(FLAGS_capture, bgr_image);
50 }
51
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080052 return false;
53 }
54
milind-uc3cf9752023-02-20 23:07:30 -080055 cv::Mat undistorted_image;
56 cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs);
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070057 if (FLAGS_scale != 1.0) {
58 cv::resize(undistorted_image, undistorted_image, cv::Size(), FLAGS_scale,
59 FLAGS_scale);
60 }
milind-uc3cf9752023-02-20 23:07:30 -080061 cv::imshow("Display", undistorted_image);
62
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080063 int keystroke = cv::waitKey(1);
64 if ((keystroke & 0xFF) == static_cast<int>('c')) {
65 // Convert again, to get clean image
66 cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV);
67 std::stringstream name;
68 name << "capture-" << aos::realtime_clock::now() << ".png";
69 cv::imwrite(name.str(), bgr_image);
70 LOG(INFO) << "Saved image file: " << name.str();
71 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
72 return false;
73 }
74 return true;
75}
76
77void ViewerMain() {
78 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
79 aos::configuration::ReadConfig(FLAGS_config);
80
milind-uc3cf9752023-02-20 23:07:30 -080081 frc971::constants::WaitForConstants<Constants>(&config.message());
82
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080083 aos::ShmEventLoop event_loop(&config.message());
84
milind-uc3cf9752023-02-20 23:07:30 -080085 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(&event_loop);
86 const auto *calibration_data = FindCameraCalibration(
87 constants_fetcher.constants(), event_loop.node()->name()->string_view());
88 const cv::Mat intrinsics = CameraIntrinsics(calibration_data);
89 const cv::Mat dist_coeffs = CameraDistCoeffs(calibration_data);
90
91 aos::Fetcher<CameraImage> image_fetcher =
92 event_loop.MakeFetcher<CameraImage>(FLAGS_channel);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080093
94 // Run the display loop
95 event_loop.AddPhasedLoop(
milind-uc3cf9752023-02-20 23:07:30 -080096 [&](int) {
milind-u7aa29e22023-02-23 20:22:01 -080097 if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080098 LOG(INFO) << "Calling event_loop Exit";
99 event_loop.Exit();
100 };
101 },
Austin Schuhceb96542023-02-04 11:43:33 -0800102 ::std::chrono::milliseconds(FLAGS_rate));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800103
104 event_loop.Run();
105
106 image_fetcher = aos::Fetcher<CameraImage>();
107}
108
109} // namespace
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800110} // namespace y2023::vision
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800111
112int main(int argc, char **argv) {
113 aos::InitGoogle(&argc, &argv);
milind-uc3cf9752023-02-20 23:07:30 -0800114 y2023::vision::ViewerMain();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800115}