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 | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 3 | #include "aos/events/event_loop.h" |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 4 | #include "external/com_github_foxglove_schemas/CompressedImage_generated.h" |
| 5 | #include "frc971/vision/charuco_lib.h" |
| 6 | #include "frc971/vision/vision_generated.h" |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 7 | |
| 8 | namespace frc971::vision { |
| 9 | // Empirically, from 2022 logs: |
| 10 | // PNG is an ~2x space savings relative to raw images. |
| 11 | // JPEG is an ~10x space savings relative to PNG. |
| 12 | // Both perform significantly better than attempting to perform in-browser |
| 13 | // conversion with a user-script in Foxglove Studio. |
| 14 | enum class ImageCompression { kJpeg, kPng }; |
| 15 | |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 16 | std::string_view ExtensionForCompression(ImageCompression compression); |
| 17 | |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 18 | flatbuffers::Offset<foxglove::CompressedImage> CompressImage( |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 19 | const cv::Mat image, const aos::monotonic_clock::time_point eof, |
| 20 | flatbuffers::FlatBufferBuilder *fbb, ImageCompression compression); |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 21 | |
| 22 | // This class provides a simple converter that will take an AOS CameraImage |
| 23 | // channel and output |
| 24 | class FoxgloveImageConverter { |
| 25 | public: |
| 26 | // Watches for frc971.vision.CameraImage messages on the input_channel and |
| 27 | // sends foxglove.CompressedImage messages on the output_channel, using the |
| 28 | // specified image compression algorithm. |
| 29 | FoxgloveImageConverter(aos::EventLoop *event_loop, |
| 30 | std::string_view input_channel, |
| 31 | std::string_view output_channel, |
| 32 | ImageCompression compression); |
| 33 | |
| 34 | private: |
| 35 | aos::EventLoop *event_loop_; |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 36 | ImageCallback image_callback_; |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 37 | aos::Sender<foxglove::CompressedImage> sender_; |
| 38 | }; |
| 39 | } // namespace frc971::vision |
| 40 | #endif // FRC971_VISION_FOXGLOVE_IMAGE_CONVERTER_H_ |