blob: b518a8e9667301be0819ebbc4663fa2bb1138001 [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
15DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
16DEFINE_string(channel, "/camera", "Channel name for the image.");
17
18DEFINE_string(capture, "",
19 "If set, capture a single image and save it to this filename.");
20
Austin Schuhceb96542023-02-04 11:43:33 -080021DEFINE_int32(rate, 100, "Time in milliseconds to wait between images");
22
milind-uc3cf9752023-02-20 23:07:30 -080023namespace y2023 {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080024namespace vision {
25namespace {
26
milind-uc3cf9752023-02-20 23:07:30 -080027using frc971::vision::CameraImage;
28
29bool DisplayLoop(const cv::Mat intrinsics, const cv::Mat dist_coeffs,
milind-u7aa29e22023-02-23 20:22:01 -080030 aos::Fetcher<CameraImage> *image_fetcher) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080031 const CameraImage *image;
32
33 // Read next image
milind-uc3cf9752023-02-20 23:07:30 -080034 if (!image_fetcher->Fetch()) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080035 VLOG(2) << "Couldn't fetch next image";
36 return true;
37 }
milind-uc3cf9752023-02-20 23:07:30 -080038 image = image_fetcher->get();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080039 CHECK(image != nullptr) << "Couldn't read image";
40
41 // Create color image:
42 cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2,
43 (void *)image->data()->data());
44 cv::Mat bgr_image(cv::Size(image->cols(), image->rows()), CV_8UC3);
45 cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV);
46
47 if (!FLAGS_capture.empty()) {
Ravago Jones17e13a22023-01-28 17:12:11 -080048 if (absl::EndsWith(FLAGS_capture, ".bfbs")) {
milind-uc3cf9752023-02-20 23:07:30 -080049 aos::WriteFlatbufferToFile(FLAGS_capture,
50 image_fetcher->CopyFlatBuffer());
Ravago Jones17e13a22023-01-28 17:12:11 -080051 } else {
52 cv::imwrite(FLAGS_capture, bgr_image);
53 }
54
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080055 return false;
56 }
57
milind-uc3cf9752023-02-20 23:07:30 -080058 cv::Mat undistorted_image;
59 cv::undistort(bgr_image, undistorted_image, intrinsics, dist_coeffs);
milind-uc3cf9752023-02-20 23:07:30 -080060 cv::imshow("Display", undistorted_image);
61
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080062 int keystroke = cv::waitKey(1);
63 if ((keystroke & 0xFF) == static_cast<int>('c')) {
64 // Convert again, to get clean image
65 cv::cvtColor(image_color_mat, bgr_image, cv::COLOR_YUV2BGR_YUYV);
66 std::stringstream name;
67 name << "capture-" << aos::realtime_clock::now() << ".png";
68 cv::imwrite(name.str(), bgr_image);
69 LOG(INFO) << "Saved image file: " << name.str();
70 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
71 return false;
72 }
73 return true;
74}
75
76void ViewerMain() {
77 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
78 aos::configuration::ReadConfig(FLAGS_config);
79
milind-uc3cf9752023-02-20 23:07:30 -080080 frc971::constants::WaitForConstants<Constants>(&config.message());
81
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080082 aos::ShmEventLoop event_loop(&config.message());
83
milind-uc3cf9752023-02-20 23:07:30 -080084 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(&event_loop);
85 const auto *calibration_data = FindCameraCalibration(
86 constants_fetcher.constants(), event_loop.node()->name()->string_view());
87 const cv::Mat intrinsics = CameraIntrinsics(calibration_data);
88 const cv::Mat dist_coeffs = CameraDistCoeffs(calibration_data);
89
90 aos::Fetcher<CameraImage> image_fetcher =
91 event_loop.MakeFetcher<CameraImage>(FLAGS_channel);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080092
93 // Run the display loop
94 event_loop.AddPhasedLoop(
milind-uc3cf9752023-02-20 23:07:30 -080095 [&](int) {
milind-u7aa29e22023-02-23 20:22:01 -080096 if (!DisplayLoop(intrinsics, dist_coeffs, &image_fetcher)) {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080097 LOG(INFO) << "Calling event_loop Exit";
98 event_loop.Exit();
99 };
100 },
Austin Schuhceb96542023-02-04 11:43:33 -0800101 ::std::chrono::milliseconds(FLAGS_rate));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800102
103 event_loop.Run();
104
105 image_fetcher = aos::Fetcher<CameraImage>();
106}
107
108} // namespace
109} // namespace vision
milind-uc3cf9752023-02-20 23:07:30 -0800110} // namespace y2023
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}