James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_ |
| 2 | #define FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_ |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 3 | #include "external/com_github_foxglove_schemas/CompressedImage_generated.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 4 | |
| 5 | #include "aos/events/event_loop.h" |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 6 | #include "frc971/vision/charuco_lib.h" |
| 7 | #include "frc971/vision/vision_generated.h" |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 8 | |
| 9 | namespace 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. |
| 15 | enum class ImageCompression { kJpeg, kPng }; |
| 16 | |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 17 | std::string_view ExtensionForCompression(ImageCompression compression); |
| 18 | |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 19 | flatbuffers::Offset<foxglove::CompressedImage> CompressImage( |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 20 | const cv::Mat image, const aos::monotonic_clock::time_point eof, |
| 21 | flatbuffers::FlatBufferBuilder *fbb, ImageCompression compression); |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 22 | |
| 23 | // This class provides a simple converter that will take an AOS CameraImage |
| 24 | // channel and output |
| 25 | class 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 Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 37 | ImageCallback image_callback_; |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 38 | aos::Sender<foxglove::CompressedImage> sender_; |
| 39 | }; |
| 40 | } // namespace frc971::vision |
| 41 | #endif // FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_ |