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