Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
soda_object_postprocess.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file soda_object_postprocess.cpp
20 * @brief EXIF-driven object-hit post-processing for SODA.
21 * @details This component does not run inference. It reads USHER's object
22 * record, creates sibling review copies and a padded crop, and appends one CSV
23 * row. The original flight JPEG is never changed.
24 */
26
27#include <fcntl.h>
28// clang-format off
29#include <stddef.h>
30#include <stdio.h>
31#include <jpeglib.h>
32// clang-format on
33#include "exifite.h"
34#include <setjmp.h>
35#include <unistd.h>
36
37#include <algorithm>
38#include <cerrno>
39#include <cmath>
40#include <cstdio>
41#include <cstdlib>
42#include <cstring>
43#include <iostream>
44#include <new>
45#include <string>
46#include <vector>
47
48#include "path_utils.h"
49#include "jpeg_marker_guard.h"
50
51/** Parsing, drawing, and file-writing helpers used by the public SODA entry
52 * point. */
54
55/** Matches image_exif.c's write_tagged_comment(): an 8-byte ASCII-charset
56 * prefix precedes the text payload in EXIF_TAG_USER_COMMENT, and the tagged
57 * record itself is introduced by
58 * "\n<TAG>\n". The entry is zero-filled before being written and sized one byte
59 * past the visible text (image_exif.c's replace_tag()/write_tagged_comment()),
60 * so a trailing NUL is present in practice, but entry->size (not strlen()) is
61 * used regardless, to stay correct even if that trailing byte is ever not zero.
62 */
63constexpr size_t kAsciiCommentPrefixSize = 8;
64constexpr char kObjectDetectionMarker[] = "\nAICAM_OBJECTS_V1\n";
65constexpr size_t kObjectLabelCapacity = 64;
66/** Same default as the now-removed Python cropper, kept identical so a ported
67 * call site produces the same crop given the same box. */
68constexpr double kCropMargin = 0.18;
69/** Matches the outline width the now-removed Python draw_hits() used (PIL's
70 * ImageDraw.rectangle(..., width=4)). There is no text label here -- see
71 * draw_box_outline()'s comment for why. */
72constexpr int kBoxOutlineThickness = 4;
73/** Fixed copy buffer avoids allocating a second full-resolution JPEG-sized
74 * block. */
75constexpr size_t kCopyChunkBytes = 65536;
76/** Bounded detection row; rows stay comfortably below append atomicity limits.
77 */
78constexpr size_t kCsvRowCapacity = 512;
79/** Keep crop output quality consistent with the previous SODA crop path. */
80constexpr int kCropJpegQuality = 95;
81
83 int count = 0; /**< Total object detections reported for this shot. */
84 std::string
85 label; /**< Bounded model-provided label of the selected object. */
86 double confidence = 0.0; /**< Confidence of the selected best box. */
87 /**< Selected box in original JPEG coordinates. */
88 int box_x = 0, box_y = 0, box_w = 0, box_h = 0;
89};
90
91/** Half-open [left,right) x [top,bottom) crop rectangle. */
92struct CropBox {
94};
95
96/** RGB pixels and dimensions decoded once for annotation and crop output. */
98 std::vector<unsigned char> pixels;
99 unsigned width = 0;
100 unsigned height = 0;
101};
102
103/** libjpeg error state with local recovery for malformed input streams. */
105 struct jpeg_error_mgr manager;
106 jmp_buf recovery;
107 void (*emit)(j_common_ptr, int);
108};
109
110/** Redirect libjpeg fatal errors to the caller's setjmp cleanup path.
111 * @param jpeg_info Active libjpeg common state. */
112void jpeg_error_exit(j_common_ptr jpeg_info) {
113 JpegErrorContext* error = reinterpret_cast<JpegErrorContext*>(jpeg_info->err);
114 longjmp(error->recovery, 1);
115}
116
117/** @brief Check JPEG marker bounds before forwarding a codec diagnostic.
118 * @param state Codec state carrying the installed JpegErrorContext.
119 * @param level libjpeg diagnostic level.
120 * @details Retain libjpeg's normal warning accounting for accepted input. */
121void jpeg_message(j_common_ptr state, int level) {
123 reinterpret_cast<JpegErrorContext*>(state->err)->emit(state, level);
124}
125
126/** @brief Parse an already-decoded EXIF_TAG_USER_COMMENT payload for an
127 * AICAM_OBJECTS_V1 hit. Kept separate from the EXIF/file I/O in
128 * read_object_detection() so the parsing logic is unit-testable against
129 * synthetic buffers without needing a real JPEG on disk.
130 * @param comment_text Complete EXIF UserComment payload after charset prefix.
131 * @param[out] out Parsed detection fields on success.
132 * @return true only for a genuine hit; false when the marker is absent (every
133 * non-spottercam image) or the summary after it is "status=ok; count=0" /
134 * "status=analysis_failed; ...", both of which fail to match the count>=1
135 * pattern below. */
136bool parse_object_summary(const std::string& comment_text,
137 ObjectDetection* out) {
138 size_t marker_pos = comment_text.find(kObjectDetectionMarker);
139 if (marker_pos == std::string::npos) {
140 return false;
141 }
142 std::string summary =
143 comment_text.substr(marker_pos + std::strlen(kObjectDetectionMarker));
144
145 int count = 0, box_x = 0, box_y = 0, box_w = 0, box_h = 0;
146 double confidence = 0.0;
147 char label[kObjectLabelCapacity];
148 if (std::sscanf(
149 summary.c_str(),
150 "status=ok; count=%d; label=%63[^;]; confidence=%lf; box=%d,%d,%d,%d",
151 &count, label, &confidence, &box_x, &box_y, &box_w, &box_h) != 7 ||
152 count < 1 || !std::isfinite(confidence) || confidence < 0.0 ||
153 confidence > 1.0 || box_x < 0 || box_y < 0 || box_w <= 0 || box_h <= 0) {
154 return false;
155 }
156
157 out->count = count;
158 out->label = label;
159 out->confidence = confidence;
160 out->box_x = box_x;
161 out->box_y = box_y;
162 out->box_w = box_w;
163 out->box_h = box_h;
164 return true;
165}
166
167/** @brief Read the AICAM_OBJECTS_V1 record usher's
168 * image_exif_write_object_detections() writes into EXIF_TAG_USER_COMMENT, if
169 * present and reporting count >= 1.
170 * @return true only for a genuine hit; false for a read/parse failure, a shot
171 * without the marker (every non-spottercam image), or a "status=ok;
172 * count=0"/"analysis_failed" shot. */
173bool read_object_detection(const char* image_path, ObjectDetection* out) {
174 unsigned char payload[EXIF_MAX_PAYLOAD];
175 size_t size = 0;
176 exif_view view{};
177 exif_value entry{};
178 if (exif_read_jpeg(image_path, payload, sizeof(payload), &size) != 0 ||
179 exif_parse(&view, payload, size) != 0 ||
180 !exif_get(&view, EXIF_CAMERA, EXIF_USER_COMMENT, &entry) ||
181 entry.format != EXIF_UNDEFINED || entry.size <= kAsciiCommentPrefixSize ||
182 std::memcmp(entry.data, "ASCII\0\0\0", kAsciiCommentPrefixSize) != 0) return false;
183 std::string text(
184 reinterpret_cast<const char*>(entry.data) + kAsciiCommentPrefixSize,
186
187 return parse_object_summary(text, out);
188}
189
190/** Resolve a sibling output directory and ensure it is writable.
191 * @param image_path Source photo path.
192 * @param name Sibling directory name.
193 * @param[out] out Resolved directory path.
194 * @return True when the directory exists and is usable. */
195bool sibling_directory(const char* image_path, const char* name,
196 std::string* out) {
197 std::string path(image_path);
198 size_t slash = path.find_last_of('/');
199 std::string base_dir =
200 (slash == std::string::npos) ? "." : path.substr(0, slash);
201 *out = base_dir + "/" + name;
202 return usher_ensure_directory(out->c_str()) == 0;
203}
204
205/** Return the final path component for display and CSV output.
206 * @param image_path Source photo path.
207 * @return Basename without directory components. */
208std::string basename_of(const char* image_path) {
209 std::string path(image_path);
210 size_t slash = path.find_last_of('/');
211 return (slash == std::string::npos) ? path : path.substr(slash + 1);
212}
213
214/** Copy every byte of @p source_path into a newly created/truncated @p
215 * dest_path.
216 * @param source_path Source JPEG.
217 * @param dest_path Destination copy.
218 * @return True only for a complete copy; removes partial output on failure.
219 * @details A standalone copy loop, deliberately not shared with
220 * spotter_cam_pipe.c's own copy_file() -- that function is file-local there,
221 * and linking spotter_cam_pipe.o into soda would couple this build to usher's
222 * object graph for ~20 lines of code. */
223bool copy_file_bytes(const char* source_path, const char* dest_path) {
224 int source_fd = open(source_path, O_RDONLY);
225 if (source_fd < 0) {
226 return false;
227 }
228 int dest_fd = open(dest_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
229 if (dest_fd < 0) {
230 close(source_fd);
231 return false;
232 }
233
234 char buffer[kCopyChunkBytes];
235 ssize_t bytes_read;
236 bool ok = true;
237 while ((bytes_read = read(source_fd, buffer, sizeof(buffer))) > 0) {
238 ssize_t written_total = 0;
239 while (written_total < bytes_read) {
240 ssize_t written = write(dest_fd, buffer + written_total,
241 static_cast<size_t>(bytes_read - written_total));
242 if (written < 0) {
243 if (errno == EINTR) {
244 continue;
245 }
246 ok = false;
247 break;
248 }
249 written_total += written;
250 }
251 if (!ok) {
252 break;
253 }
254 }
255 if (bytes_read < 0) {
256 ok = false;
257 }
258
259 close(source_fd);
260 if (close(dest_fd) != 0) {
261 ok = false;
262 }
263 if (!ok) {
264 unlink(dest_path);
265 }
266 return ok;
267}
268
269/** Add @p margin times the box size on each side, then clamp to the image
270 * bounds, guarding a degenerate zero-area result so it's always safely
271 * croppable.
272 * @param box_x Detection left pixel.
273 * @param box_y Detection top pixel.
274 * @param box_w Detection width.
275 * @param box_h Detection height.
276 * @param img_w Source-image width.
277 * @param img_h Source-image height.
278 * @param margin Fraction of box size added on every side.
279 * @return Clamped half-open crop with nonzero area. */
280CropBox compute_crop_box(int box_x, int box_y, int box_w, int box_h,
281 unsigned img_w, unsigned img_h, double margin) {
282 double pad_x = box_w * margin;
283 double pad_y = box_h * margin;
284 int left = std::max(0, static_cast<int>(std::lround(box_x - pad_x)));
285 int top = std::max(0, static_cast<int>(std::lround(box_y - pad_y)));
286 int right = std::min(static_cast<int>(img_w),
287 static_cast<int>(std::lround(box_x + box_w + pad_x)));
288 int bottom = std::min(static_cast<int>(img_h),
289 static_cast<int>(std::lround(box_y + box_h + pad_y)));
290 right = std::max(right, left + 1);
291 bottom = std::max(bottom, top + 1);
292 return CropBox{left, top, right, bottom};
293}
294
295/** Decode a JPEG to a contiguous row-major RGB buffer.
296 * @param path Input image path.
297 * @param[out] image Decoded pixels and image dimensions.
298 * @return True only for a complete valid JPEG decode.
299 * @details Same libjpeg pattern as image_mock_transform.c's decode_jpeg(),
300 * duplicated rather than shared since that file's decode/encode helpers are
301 * file-local (static) and out of scope to refactor for this change. */
302bool decode_jpeg_rgb(const char* path, DecodedImage* image) {
303 if (path == nullptr || image == nullptr) return false;
304 FILE* input = fopen(path, "rb");
305 if (input == nullptr) {
306 return false;
307 }
308
309 struct jpeg_decompress_struct* decoder =
310 static_cast<struct jpeg_decompress_struct*>(calloc(1, sizeof(*decoder)));
311 if (decoder == nullptr) {
312 fclose(input);
313 return false;
314 }
315
317 decoder->err = jpeg_std_error(&error.manager);
318 error.emit = error.manager.emit_message;
319 error.manager.emit_message = jpeg_message;
320 error.manager.error_exit = jpeg_error_exit;
321 if (setjmp(error.recovery)) {
322 jpeg_destroy_decompress(decoder);
323 free(decoder);
324 fclose(input);
325 return false;
326 }
327
328 jpeg_create_decompress(decoder);
329 jpeg_stdio_src(decoder, input);
330 if (jpeg_read_header(decoder, TRUE) != JPEG_HEADER_OK ||
331 !decoder->image_width || !decoder->image_height ||
332 decoder->image_width > 8192 || decoder->image_height > 8192 ||
333 static_cast<uint64_t>(decoder->image_width) * decoder->image_height > 32U * 1024U * 1024U) {
334 jpeg_destroy_decompress(decoder);
335 free(decoder);
336 fclose(input);
337 return false;
338 }
339 decoder->out_color_space = JCS_RGB;
340 jpeg_start_decompress(decoder);
341
342 image->width = decoder->output_width;
343 image->height = decoder->output_height;
344 if (decoder->output_components != 3 || image->width < 1 ||
345 image->height < 1) {
346 jpeg_destroy_decompress(decoder);
347 free(decoder);
348 fclose(input);
349 return false;
350 }
351
352 size_t row_size = static_cast<size_t>(image->width) * 3;
353 try {
354 image->pixels.resize(row_size * image->height);
355 } catch (const std::bad_alloc&) {
356 jpeg_destroy_decompress(decoder);
357 free(decoder);
358 fclose(input);
359 return false;
360 }
361
362 while (decoder->output_scanline < decoder->output_height) {
363 JSAMPROW row[1] = {
364 &image->pixels[static_cast<size_t>(decoder->output_scanline) *
365 row_size]};
366 if (jpeg_read_scanlines(decoder, row, 1) != 1) {
367 jpeg_destroy_decompress(decoder);
368 free(decoder);
369 fclose(input);
370 return false;
371 }
372 }
373
374 jpeg_finish_decompress(decoder);
375 jpeg_destroy_decompress(decoder);
376 free(decoder);
377 return fclose(input) == 0;
378}
379
380/** Draw a green rectangle outline in place on a decoded RGB buffer.
381 * @param image Mutable decoded image.
382 * @param box_x Box left coordinate.
383 * @param box_y Box top coordinate.
384 * @param box_w Box width.
385 * @param box_h Box height.
386 * @param thickness Outline width in pixels.
387 * @details The native capture and metadata stages never draw on the frame saved
388 * to the configured photo directory; that file stays a clean flight-record
389 * image. SODA is the only place that burns a box in, onto its own copies
390 * (objects_captured/ and the crop), using the box this process already read
391 * back from EXIF. This draws only the outline, not the "label 0.NN" confidence
392 * text -- replicating PIL's text rendering in raw C++ would need a
393 * font-rendering dependency for a label that's already in the console log and
394 * the CSV row. */
395void draw_box_outline(DecodedImage* image, int box_x, int box_y, int box_w,
396 int box_h, int thickness) {
397 int left = box_x;
398 int top = box_y;
399 int right = box_x + box_w;
400 int bottom = box_y + box_h;
401 int img_w = static_cast<int>(image->width);
402 int img_h = static_cast<int>(image->height);
403
404 auto set_pixel = [&](int x, int y) {
405 if (x < 0 || y < 0 || x >= img_w || y >= img_h) {
406 return;
407 }
408 size_t offset =
409 (static_cast<size_t>(y) * image->width + static_cast<size_t>(x)) * 3;
410 image->pixels[offset] = 0;
411 image->pixels[offset + 1] = 255;
412 image->pixels[offset + 2] = 0;
413 };
414
415 for (int stroke = 0; stroke < thickness; ++stroke) {
416 for (int x = left; x < right; ++x) {
417 set_pixel(x, top + stroke);
418 set_pixel(x, bottom - 1 - stroke);
419 }
420 for (int y = top; y < bottom; ++y) {
421 set_pixel(left + stroke, y);
422 set_pixel(right - 1 - stroke, y);
423 }
424 }
425}
426
427/** Encode the selected region of an already-decoded frame to a new JPEG file.
428 * @param path Destination JPEG path.
429 * @param image Full-frame RGB source.
430 * @param box Half-open source rectangle; rows are referenced directly without a
431 * crop copy.
432 * @return True when all rows encode and the output closes successfully.
433 * @details No separate crop buffer is allocated: since the decoded buffer is
434 * row-major RGB at the *full* image's stride, each output scanline's row
435 * pointer can point directly into it at the crop's left/top offset -- the
436 * encoder only reads `crop_w*3` bytes from wherever that pointer starts.
437 * Quality 95, matching image_mock_transform.c's encode_jpeg(). */
438bool encode_cropped_jpeg(const char* path, const DecodedImage& image,
439 const CropBox& box) {
440 int crop_w = box.right - box.left;
441 int crop_h = box.bottom - box.top;
442 if (crop_w <= 0 || crop_h <= 0) {
443 return false;
444 }
445
446 FILE* output = fopen(path, "wb");
447 if (output == nullptr) {
448 return false;
449 }
450
451 struct jpeg_compress_struct* encoder =
452 static_cast<struct jpeg_compress_struct*>(calloc(1, sizeof(*encoder)));
453 if (encoder == nullptr) {
454 fclose(output);
455 return false;
456 }
457
459 encoder->err = jpeg_std_error(&error.manager);
460 error.manager.error_exit = jpeg_error_exit;
461 if (setjmp(error.recovery)) {
462 jpeg_destroy_compress(encoder);
463 free(encoder);
464 fclose(output);
465 return false;
466 }
467
468 jpeg_create_compress(encoder);
469 jpeg_stdio_dest(encoder, output);
470 encoder->image_width = static_cast<JDIMENSION>(crop_w);
471 encoder->image_height = static_cast<JDIMENSION>(crop_h);
472 encoder->input_components = 3;
473 encoder->in_color_space = JCS_RGB;
474 jpeg_set_defaults(encoder);
475 jpeg_set_quality(encoder, kCropJpegQuality, TRUE);
476 jpeg_start_compress(encoder, TRUE);
477
478 size_t full_row_size = static_cast<size_t>(image.width) * 3;
479 while (encoder->next_scanline < encoder->image_height) {
480 size_t source_row = static_cast<size_t>(box.top) + encoder->next_scanline;
481 const unsigned char* row_start =
482 &image.pixels[source_row * full_row_size +
483 static_cast<size_t>(box.left) * 3];
484 JSAMPROW row[1] = {const_cast<JSAMPROW>(row_start)};
485 if (jpeg_write_scanlines(encoder, row, 1) != 1) {
486 jpeg_destroy_compress(encoder);
487 free(encoder);
488 fclose(output);
489 return false;
490 }
491 }
492
493 jpeg_finish_compress(encoder);
494 jpeg_destroy_compress(encoder);
495 free(encoder);
496 return fclose(output) == 0;
497}
498
499/** Write one complete buffer in a single syscall, retrying only an EINTR that
500 * transferred nothing. Callers rely on the single-syscall property for O_APPEND
501 * atomicity -- see append_detection_csv().
502 * @param fd Open output descriptor.
503 * @param data Complete row or header-plus-row payload.
504 * @return True only if the syscall wrote the entire buffer. */
505bool write_all_atomic(int fd, const std::string& data) {
506 ssize_t written;
507 do {
508 written = write(fd, data.data(), data.size());
509 } while (written < 0 && errno == EINTR);
510 return written == static_cast<ssize_t>(data.size());
511}
512
513/** Escape one bounded model label as a CSV field. */
514std::string csv_field(const std::string& value) {
515 if (value.find_first_of(",\"\r\n") == std::string::npos) return value;
516 std::string escaped = "\"";
517 for (char character : value) {
518 if (character == '\"') escaped += '\"';
519 escaped += character;
520 }
521 escaped += '\"';
522 return escaped;
523}
524
525/** Append one CSV row for @p detection to @p csv_path, safe against concurrent
526 * SODA processes racing on the same file (usher can run multiple worker
527 * threads' finish_image() close together, each spawning its own SODA child).
528 * @details Header-or-not is decided with O_CREAT|O_EXCL: whichever process wins
529 * that race writes header+row together in a single write(); every loser falls
530 * back to a plain O_APPEND open and writes only its own row. Each row (and the
531 * header+row for the winner) goes out as exactly one write() call, which POSIX
532 * guarantees is atomic against other O_APPEND writers on the same file as long
533 * as it stays under PIPE_BUF -- comfortably true for these rows.
534 * @param csv_path Shared CSV path.
535 * @param image_basename Source JPEG basename.
536 * @param detection EXIF-derived hit summary.
537 * @return True when exactly one complete row/header+row is written. */
538bool append_detection_csv(const std::string& csv_path,
539 const std::string& image_basename,
540 const ObjectDetection& detection) {
541 std::string label = csv_field(detection.label);
542 char row_buffer[kCsvRowCapacity];
543 int row_length = std::snprintf(
544 row_buffer, sizeof(row_buffer), "%s,%s,%d,%.4f,%d,%d,%d,%d\n",
545 image_basename.c_str(), label.c_str(), detection.count,
546 detection.confidence, detection.box_x, detection.box_y, detection.box_w,
547 detection.box_h);
548 if (row_length <= 0 ||
549 static_cast<size_t>(row_length) >= sizeof(row_buffer)) {
550 return false;
551 }
552 std::string row(row_buffer, static_cast<size_t>(row_length));
553
554 int fd = open(csv_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0644);
555 if (fd >= 0) {
556 static const char kHeader[] =
557 "image,label,count,confidence,box_x,box_y,box_w,box_h\n";
558 bool ok = write_all_atomic(fd, std::string(kHeader) + row);
559 return close(fd) == 0 && ok;
560 }
561 if (errno != EEXIST) {
562 return false;
563 }
564
565 fd = open(csv_path.c_str(), O_WRONLY | O_APPEND);
566 if (fd < 0) {
567 return false;
568 }
569 bool ok = write_all_atomic(fd, row);
570 return close(fd) == 0 && ok;
571}
572
573} // namespace soda_object_postprocess_detail
574
575using namespace soda_object_postprocess_detail;
576
577/** Create SODA review derivatives only when the source EXIF describes an object
578 * hit.
579 * @return True for no-op/no-hit or complete processing; false when a detected
580 * hit could not be fully copied, cropped, or recorded. */
581bool soda_process_object_detection(const char* image_path) {
582 if (image_path == nullptr || image_path[0] == '\0') {
583 return true;
584 }
585
586 ObjectDetection detection;
587 if (!read_object_detection(image_path, &detection)) {
588 return true;
589 }
590
591 std::string basename = basename_of(image_path);
592 bool ok = true;
593
594 // Decode once and draw the box once; both the objects_captured/ copy and the
595 // crop are regions of this same annotated buffer, so the box appears
596 // consistently in both without decoding or drawing twice. The original file
597 // at image_path is never touched -- it stays the plain capture saved by the
598 // native camera backend.
599 DecodedImage decoded;
600 bool decoded_ok = decode_jpeg_rgb(image_path, &decoded);
601 if (decoded_ok) {
602 draw_box_outline(&decoded, detection.box_x, detection.box_y,
603 detection.box_w, detection.box_h, kBoxOutlineThickness);
604 } else {
605 std::cerr << "SODA: failed to decode " << image_path << " for annotation"
606 << std::endl;
607 }
608
609 std::string hits_dir;
610 if (sibling_directory(image_path, "objects_captured", &hits_dir)) {
611 std::string dest_path = hits_dir + "/" + basename;
612 bool saved;
613 if (decoded_ok) {
614 CropBox full_frame{0, 0, static_cast<int>(decoded.width),
615 static_cast<int>(decoded.height)};
616 saved = encode_cropped_jpeg(dest_path.c_str(), decoded, full_frame);
617 } else {
618 // Annotation failed -- still land an (unannotated) copy so the hit isn't
619 // lost entirely.
620 saved = copy_file_bytes(image_path, dest_path.c_str());
621 }
622 if (!saved) {
623 std::cerr << "SODA: failed to save annotated copy of " << image_path
624 << " into " << hits_dir << std::endl;
625 ok = false;
626 }
627 std::string csv_path = hits_dir + "/detections.csv";
628 if (!append_detection_csv(csv_path, basename, detection)) {
629 std::cerr << "SODA: failed to record detection in " << csv_path
630 << std::endl;
631 ok = false;
632 }
633 } else {
634 std::cerr << "SODA: failed to prepare objects_captured directory for "
635 << image_path << std::endl;
636 ok = false;
637 }
638
639 std::string crop_dir;
640 if (sibling_directory(image_path, "tight_crop_objects", &crop_dir)) {
641 if (decoded_ok) {
643 detection.box_x, detection.box_y, detection.box_w, detection.box_h,
644 decoded.width, decoded.height, kCropMargin);
645 std::string stem = basename;
646 size_t dot = stem.find_last_of('.');
647 if (dot != std::string::npos) {
648 stem.erase(dot);
649 }
650 std::string crop_path = crop_dir + "/" + stem + "_object01.jpg";
651 if (!encode_cropped_jpeg(crop_path.c_str(), decoded, box)) {
652 std::cerr << "SODA: failed to save crop for " << image_path
653 << std::endl;
654 ok = false;
655 }
656 } else {
657 std::cerr << "SODA: failed to crop " << image_path
658 << " (decode already failed)" << std::endl;
659 ok = false;
660 }
661 } else {
662 std::cerr << "SODA: failed to prepare tight_crop_objects directory for "
663 << image_path << std::endl;
664 ok = false;
665 }
666
667 std::cerr << "SODA: " << detection.label
668 << " detected (count=" << detection.count
669 << ", conf=" << detection.confidence << "); post-processed "
670 << image_path << std::endl;
671
672 return ok;
673}
size_t size
Definition chdk_pipe.c:143
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
Bounded C11 EXIF metadata and JPEG streaming interfaces.
int exif_parse(struct exif_view *view, const unsigned char *payload, size_t size)
Validate an EXIF APP1 payload and create a borrowed view.
Definition exifite.c:257
bool exif_get(const struct exif_view *view, enum exif_directory directory, uint16_t tag, struct exif_value *value)
Look up a directory-local tag without copying its bytes.
Definition exifite.c:310
int exif_read_jpeg(const char *filename, unsigned char *payload, size_t capacity, size_t *size)
Read the single EXIF APP1 segment before the first JPEG scan.
Definition exifite.c:1022
#define EXIF_MAX_PAYLOAD
APP1 payload ceiling: the JPEG length includes its own two bytes.
Definition exifite.h:51
@ EXIF_UNDEFINED
Definition exifite.h:70
@ EXIF_CAMERA
Definition exifite.h:59
@ EXIF_USER_COMMENT
Definition exifite.h:93
Reject invalid Huffman selectors before the bundled codec indexes its tables.
static void usher_jpeg_validate_marker(j_common_ptr state)
Validate marker parameters from an error manager's emit_message callback.
struct jpeg_compress_struct encoder
Definition lwir_image.c:59
uint8_t row[LWIR_MAX_WIDTH *3U]
Definition lwir_image.c:63
struct jpeg_error_mgr error
Definition lwir_image.c:60
struct jpeg_decompress_struct decoder
Definition lwir_mock.c:55
void jpeg_message(j_common_ptr state, int level)
Check JPEG marker bounds before forwarding a codec diagnostic.
void draw_box_outline(DecodedImage *image, int box_x, int box_y, int box_w, int box_h, int thickness)
void jpeg_error_exit(j_common_ptr jpeg_info)
bool parse_object_summary(const std::string &comment_text, ObjectDetection *out)
Parse an already-decoded EXIF_TAG_USER_COMMENT payload for an AICAM_OBJECTS_V1 hit....
bool sibling_directory(const char *image_path, const char *name, std::string *out)
std::string csv_field(const std::string &value)
bool decode_jpeg_rgb(const char *path, DecodedImage *image)
bool write_all_atomic(int fd, const std::string &data)
bool copy_file_bytes(const char *source_path, const char *dest_path)
CropBox compute_crop_box(int box_x, int box_y, int box_w, int box_h, unsigned img_w, unsigned img_h, double margin)
bool read_object_detection(const char *image_path, ObjectDetection *out)
Read the AICAM_OBJECTS_V1 record usher's image_exif_write_object_detections() writes into EXIF_TAG_US...
std::string basename_of(const char *image_path)
bool append_detection_csv(const std::string &csv_path, const std::string &image_basename, const ObjectDetection &detection)
bool encode_cropped_jpeg(const char *path, const DecodedImage &image, const CropBox &box)
Small path and writable-directory helpers shared by USHER backends.
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
Definition path_utils.h:97
static size_t count
Definition pose_log.c:79
static FILE * output
Definition pose_log.c:77
int fd
Definition serial.c:39
bool soda_process_object_detection(const char *image_path)
Post-process a captured image for an object-detection hit, if it has one.
EXIF-driven object-hit post-processing for SODA.
Borrowed bytes plus enough type information for safe interpretation.
Definition exifite.h:119
const unsigned char * data
Definition exifite.h:120
size_t size
Definition exifite.h:121
uint16_t format
Definition exifite.h:123
Borrowed view of a validated APP1 payload.
Definition exifite.h:131
struct captured_image image
Definition usher.c:371