|
24 | 24 | #include "utilities/http.hpp" |
25 | 25 | #include "utilities/logging.hpp" |
26 | 26 | #include "utilities/serialize.hpp" |
| 27 | +#include "cv/utilities.hpp" |
27 | 28 |
|
28 | 29 | extern "C" { |
29 | 30 | #include "udp_squared/protocol.h" |
@@ -251,39 +252,11 @@ DEF_GCS_HANDLE(Get, camera, capture) { |
251 | 252 | } |
252 | 253 |
|
253 | 254 | std::optional<ImageTelemetry> telemetry = image->TELEMETRY; |
254 | | - |
255 | | - // START COMPRESSION |
256 | | - // Compress the image before converting to base64 |
257 | | - std::vector<uchar> compressed_data; |
258 | | - std::vector<int> compression_params; |
259 | | - compression_params.push_back(cv::IMWRITE_JPEG_QUALITY); |
260 | | - compression_params.push_back(85); // Quality: 0-100, 85 is a good balance for transmission |
261 | | - |
262 | | - cv::Mat compressed_image; |
263 | | - if (cv::imencode(".jpg", image->DATA, compressed_data, compression_params)) { |
264 | | - // Create compressed Mat from encoded data |
265 | | - compressed_image = cv::imdecode(compressed_data, cv::IMREAD_COLOR); |
266 | | - |
267 | | - if (!compressed_image.empty()) { |
268 | | - LOG_F(INFO, |
269 | | - "Compressed manual capture image from %zu bytes to %zu bytes " |
270 | | - "(%.1f%% compression)", |
271 | | - image->DATA.total() * image->DATA.elemSize(), compressed_data.size(), |
272 | | - (1.0 - static_cast<double>(compressed_data.size()) / |
273 | | - (image->DATA.total() * image->DATA.elemSize())) * |
274 | | - 100.0); |
275 | | - } else { |
276 | | - LOG_F(WARNING, "Failed to decode compressed manual capture image, using original"); |
277 | | - compressed_image = image->DATA; |
278 | | - } |
279 | | - } else { |
280 | | - LOG_F(WARNING, "Failed to compress manual capture image, using original"); |
281 | | - compressed_image = image->DATA; |
282 | | - } |
283 | | - |
| 255 | + std::optional<cv::Mat> optCompressedImg = compressImg(image->DATA); |
| 256 | + cv::Mat compressed_image = optCompressedImg.has_value() ? |
| 257 | + optCompressedImg->clone() : image->DATA; |
284 | 258 | ManualImage manual_image; |
285 | 259 | manual_image.set_img_b64(cvMatToBase64(compressed_image)); |
286 | | - // END COMPRESSION |
287 | 260 |
|
288 | 261 | manual_image.set_timestamp(image->TIMESTAMP); |
289 | 262 | if (telemetry.has_value()) { |
@@ -379,40 +352,12 @@ DEF_GCS_HANDLE(Get, targets, all) { |
379 | 352 | IdentifiedTarget target; |
380 | 353 | // Set the run ID |
381 | 354 | target.set_run_id(run.run_id); |
382 | | - // START COMPRESSION |
383 | | - |
384 | | - // Compress the annotated image before converting to base64 |
385 | | - std::vector<uchar> compressed_data; |
386 | | - std::vector<int> compression_params; |
387 | | - compression_params.push_back(cv::IMWRITE_JPEG_QUALITY); |
388 | | - compression_params.push_back(85); |
389 | | - // Quality: 0-100, 85 is a good balance for transmission |
390 | | - |
391 | | - cv::Mat compressed_image; |
392 | | - if (cv::imencode(".jpg", run.annotatedImage, compressed_data, compression_params)) { |
393 | | - // Create compressed Mat from encoded data |
394 | | - compressed_image = cv::imdecode(compressed_data, cv::IMREAD_COLOR); |
395 | | - |
396 | | - if (!compressed_image.empty()) { |
397 | | - LOG_F(INFO, "Compressed image from %zu bytes to %zu bytes (%.1f%% compression)", |
398 | | - run.annotatedImage.total() * run.annotatedImage.elemSize(), |
399 | | - compressed_data.size(), |
400 | | - (1.0 - static_cast<double>(compressed_data.size()) / |
401 | | - (run.annotatedImage.total() * run.annotatedImage.elemSize())) * |
402 | | - 100.0); |
403 | | - } else { |
404 | | - LOG_F(WARNING, "Failed to decode compressed image, using original"); |
405 | | - compressed_image = run.annotatedImage; |
406 | | - } |
407 | | - } else { |
408 | | - LOG_F(WARNING, "Failed to compress image, using original"); |
409 | | - compressed_image = run.annotatedImage; |
410 | | - } |
411 | 355 |
|
412 | | - // Convert the compressed image to base64 and set it (once per run) |
| 356 | + std::optional<cv::Mat> optCompressedImg = compressImg(run.annotatedImage); |
| 357 | + cv::Mat compressed_image = optCompressedImg.has_value() ? |
| 358 | + optCompressedImg->clone() : run.annotatedImage; |
413 | 359 | std::string b64 = cvMatToBase64(compressed_image); |
414 | 360 | target.set_picture(b64); |
415 | | - // END COMPRESSION |
416 | 361 |
|
417 | 362 | // Ensure coords and bboxes vectors are the same size (should be guaranteed by Aggregator |
418 | 363 | // logic) |
|
0 commit comments