blob: 0af7e0ec0327dd4fad34a211ebd21cdb09a60032 [file] [log] [blame]
Philipp Schrader790cb542023-07-05 21:06:52 -07001#include "absl/strings/match.h"
2#include "opencv2/calib3d.hpp"
3#include "opencv2/imgproc.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 Schuhdb2ed9d2022-12-26 14:02:26 -080015DEFINE_string(capture, "",
16 "If set, capture a single image and save it to this filename.");
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070017DEFINE_string(channel, "/camera", "Channel name for the image.");
18DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
Austin Schuhceb96542023-02-04 11:43:33 -080019DEFINE_int32(rate, 100, "Time in milliseconds to wait between images");
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070020DEFINE_double(scale, 1.0, "Scale factor for images being displayed");
Austin Schuhceb96542023-02-04 11:43:33 -080021
milind-uc3cf9752023-02-20 23:07:30 -080022namespace y2023 {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080023namespace vision {
24namespace {
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
46 if (!FLAGS_capture.empty()) {
Ravago Jones17e13a22023-01-28 17:12:11 -080047 if (absl::EndsWith(FLAGS_capture, ".bfbs")) {
milind-uc3cf9752023-02-20 23:07:30 -080048 aos::WriteFlatbufferToFile(FLAGS_capture,
49 image_fetcher->CopyFlatBuffer());
Ravago Jones17e13a22023-01-28 17:12:11 -080050 } else {
51 cv::imwrite(FLAGS_capture, bgr_image);
52 }
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);
Jim Ostrowskie2d6f662023-09-09 18:00:05 -070059 if (FLAGS_scale != 1.0) {
60 cv::resize(undistorted_image, undistorted_image, cv::Size(), FLAGS_scale,
61 FLAGS_scale);
62 }
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 =
81 aos::configuration::ReadConfig(FLAGS_config);
82
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 =
94 event_loop.MakeFetcher<CameraImage>(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 Schuhceb96542023-02-04 11:43:33 -0800104 ::std::chrono::milliseconds(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
112} // namespace vision
milind-uc3cf9752023-02-20 23:07:30 -0800113} // namespace y2023
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800114
115int main(int argc, char **argv) {
116 aos::InitGoogle(&argc, &argv);
milind-uc3cf9752023-02-20 23:07:30 -0800117 y2023::vision::ViewerMain();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800118}