James Kuszmaul | 77d536c | 2023-02-11 17:30:59 -0800 | [diff] [blame] | 1 | #include "frc971/vision/foxglove_image_converter_lib.h" |
| 2 | |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 3 | #include <opencv2/imgcodecs.hpp> |
| 4 | #include <opencv2/imgproc.hpp> |
| 5 | |
James Kuszmaul | acff22f | 2023-03-04 21:00:02 -0800 | [diff] [blame] | 6 | DEFINE_int32(jpeg_quality, 60, |
James Kuszmaul | 77d536c | 2023-02-11 17:30:59 -0800 | [diff] [blame] | 7 | "Compression quality of JPEGs, 0-100; lower numbers mean lower " |
| 8 | "quality and resulting image sizes."); |
James Kuszmaul | 682daef | 2024-03-03 14:25:10 -0800 | [diff] [blame] | 9 | DEFINE_uint32(max_period_ms, 100, |
| 10 | "Fastest period at which to convert images, to limit CPU usage."); |
James Kuszmaul | 77d536c | 2023-02-11 17:30:59 -0800 | [diff] [blame] | 11 | |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 12 | namespace frc971::vision { |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 13 | std::string_view ExtensionForCompression(ImageCompression compression) { |
| 14 | switch (compression) { |
| 15 | case ImageCompression::kJpeg: |
| 16 | return "jpeg"; |
| 17 | case ImageCompression::kPng: |
| 18 | return "png"; |
| 19 | } |
| 20 | } |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 21 | |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 22 | flatbuffers::Offset<foxglove::CompressedImage> CompressImage( |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 23 | const cv::Mat image, const aos::monotonic_clock::time_point eof, |
| 24 | flatbuffers::FlatBufferBuilder *fbb, ImageCompression compression) { |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 25 | std::string_view format = ExtensionForCompression(compression); |
| 26 | // imencode doesn't let us pass in anything other than an std::vector, and |
| 27 | // performance isn't yet a big enough issue to try to avoid the copy. |
| 28 | std::vector<uint8_t> buffer; |
James Kuszmaul | 77d536c | 2023-02-11 17:30:59 -0800 | [diff] [blame] | 29 | CHECK(cv::imencode(absl::StrCat(".", format), image, buffer, |
| 30 | {cv::IMWRITE_JPEG_QUALITY, FLAGS_jpeg_quality})); |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 31 | const flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset = |
| 32 | fbb->CreateVector(buffer); |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 33 | const struct timespec timestamp_t = aos::time::to_timespec(eof); |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 34 | const foxglove::Time time{static_cast<uint32_t>(timestamp_t.tv_sec), |
| 35 | static_cast<uint32_t>(timestamp_t.tv_nsec)}; |
| 36 | const flatbuffers::Offset<flatbuffers::String> format_offset = |
| 37 | fbb->CreateString(format); |
| 38 | foxglove::CompressedImage::Builder builder(*fbb); |
| 39 | builder.add_timestamp(&time); |
| 40 | builder.add_data(data_offset); |
| 41 | builder.add_format(format_offset); |
| 42 | return builder.Finish(); |
| 43 | } |
| 44 | |
| 45 | FoxgloveImageConverter::FoxgloveImageConverter(aos::EventLoop *event_loop, |
| 46 | std::string_view input_channel, |
| 47 | std::string_view output_channel, |
| 48 | ImageCompression compression) |
| 49 | : event_loop_(event_loop), |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 50 | image_callback_( |
| 51 | event_loop_, input_channel, |
| 52 | [this, compression](const cv::Mat image, |
| 53 | const aos::monotonic_clock::time_point eof) { |
James Kuszmaul | 682daef | 2024-03-03 14:25:10 -0800 | [diff] [blame] | 54 | if (event_loop_->monotonic_now() > |
| 55 | (std::chrono::milliseconds(FLAGS_max_period_ms) + |
| 56 | sender_.monotonic_sent_time())) { |
| 57 | auto builder = sender_.MakeBuilder(); |
| 58 | builder.CheckOk(builder.Send( |
| 59 | CompressImage(image, eof, builder.fbb(), compression))); |
| 60 | } |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 61 | }), |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 62 | sender_( |
James Kuszmaul | 59a308f | 2023-01-28 19:14:07 -0800 | [diff] [blame] | 63 | event_loop_->MakeSender<foxglove::CompressedImage>(output_channel)) {} |
James Kuszmaul | 0c59396 | 2023-01-28 16:04:20 -0800 | [diff] [blame] | 64 | } // namespace frc971::vision |