blob: cdba9e3369313a5ebc2d77909616b1c84da20449 [file] [log] [blame]
James Kuszmaul0c593962023-01-28 16:04:20 -08001#ifndef FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_
2#define FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_
James Kuszmaul59a308f2023-01-28 19:14:07 -08003#include "external/com_github_foxglove_schemas/CompressedImage_generated.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07004
5#include "aos/events/event_loop.h"
James Kuszmaul59a308f2023-01-28 19:14:07 -08006#include "frc971/vision/charuco_lib.h"
7#include "frc971/vision/vision_generated.h"
James Kuszmaul0c593962023-01-28 16:04:20 -08008
9namespace frc971::vision {
10// Empirically, from 2022 logs:
11// PNG is an ~2x space savings relative to raw images.
12// JPEG is an ~10x space savings relative to PNG.
13// Both perform significantly better than attempting to perform in-browser
14// conversion with a user-script in Foxglove Studio.
15enum class ImageCompression { kJpeg, kPng };
16
James Kuszmaul59a308f2023-01-28 19:14:07 -080017std::string_view ExtensionForCompression(ImageCompression compression);
18
James Kuszmaul0c593962023-01-28 16:04:20 -080019flatbuffers::Offset<foxglove::CompressedImage> CompressImage(
James Kuszmaul59a308f2023-01-28 19:14:07 -080020 const cv::Mat image, const aos::monotonic_clock::time_point eof,
21 flatbuffers::FlatBufferBuilder *fbb, ImageCompression compression);
James Kuszmaul0c593962023-01-28 16:04:20 -080022
23// This class provides a simple converter that will take an AOS CameraImage
24// channel and output
25class FoxgloveImageConverter {
26 public:
27 // Watches for frc971.vision.CameraImage messages on the input_channel and
28 // sends foxglove.CompressedImage messages on the output_channel, using the
29 // specified image compression algorithm.
30 FoxgloveImageConverter(aos::EventLoop *event_loop,
31 std::string_view input_channel,
32 std::string_view output_channel,
33 ImageCompression compression);
34
35 private:
36 aos::EventLoop *event_loop_;
James Kuszmaul59a308f2023-01-28 19:14:07 -080037 ImageCallback image_callback_;
James Kuszmaul0c593962023-01-28 16:04:20 -080038 aos::Sender<foxglove::CompressedImage> sender_;
39};
40} // namespace frc971::vision
41#endif // FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_