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
inference_capture.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/** @file inference_capture.cpp
19 * @brief One-owner capture, model preparation and no-overwrite result publication.
20 * @details CPU inference runs after the frame lease ends so model latency does not hold camera DMA ownership. */
21#include "inference_capture.hpp"
22
23#include <cerrno>
24#include <cstdio>
25#include <cstring>
26#include <fcntl.h>
27#include <limits>
28#include <memory>
29#include <stdexcept>
30#include <string>
31#include <sys/stat.h>
32#include <unistd.h>
33
34namespace {
35/** @brief Ensure exceptions release the model session before its mapped input/output storage. */
36struct ContextOwner {
37 inference_context context{};
38 ~ContextOwner() { inference_context_close(&context); }
39};
40
41/** @brief Borrowed bridge state used only while the camera grants a scoped pixel visit. */
42struct Preparation {
43 inference_context *context;
44 char *error;
45 size_t capacity;
46};
47
48/** @brief Copy only model-sized input while retaining the original frame's timing identity. */
49bool prepare_pixels(const imx500::FrameView &view, const std::array<const uint8_t *, 3> &planes,
50 unsigned stride, void *data)
51{
52 auto &preparation = *static_cast<Preparation *>(data);
54 image.width = view.image.width;
55 image.height = view.image.height;
56 image.format = INFERENCE_YUV420;
57 for (size_t plane = 0; plane < 3; ++plane) {
58 image.planes[plane] = planes[plane];
59 image.strides[plane] = plane ? stride / 2 : stride;
60 image.lengths[plane] = image.strides[plane] * (plane ? image.height / 2 : image.height);
61 }
62 preparation.context->frame_sequence = view.sequence;
63 preparation.context->sensor_timestamp_ns = view.sensor_timestamp_ns;
64 preparation.context->capture_identity = true;
65 return inference_context_prepare_image(preparation.context, &image, preparation.error, preparation.capacity);
66}
67
68/** @brief Permit the neutral encoder to retain a useful photograph before optional CPU analysis succeeds. */
69bool retain_pixels(const imx500::FrameView &, exif_document *) { return true; }
70
71/** @brief Preserve fake-camera JPEG bytes without reencoding, refusing overwrite and changing input files. */
72bool copy_picture(const char *source, const char *destination)
73{
74 const int input = open(source, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
75 if (input < 0) return false;
76 struct stat before{}, after{};
77 if (fstat(input, &before) || !S_ISREG(before.st_mode) || before.st_size <= 0 || before.st_size > 128 * 1024 * 1024) {
78 close(input);
79 return false;
80 }
81 const int output = open(destination, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
82 if (output < 0) { close(input); return false; }
83 unsigned char buffer[16384];
84 bool valid = true;
85 off_t copied = 0;
86 while (valid) {
87 const ssize_t count = read(input, buffer, sizeof(buffer));
88 if (count < 0 && errno == EINTR) continue;
89 if (count < 0) valid = false;
90 if (count <= 0) break;
91 if (copied > before.st_size - count) { valid = false; break; }
92 copied += count;
93 size_t written = 0;
94 while (written < static_cast<size_t>(count)) {
95 const ssize_t bytes = write(output, buffer + written, static_cast<size_t>(count) - written);
96 if (bytes < 0 && errno == EINTR) continue;
97 if (bytes <= 0) { valid = false; break; }
98 written += static_cast<size_t>(bytes);
99 }
100 }
101 valid = valid && copied == before.st_size && !fstat(input, &after) && before.st_size == after.st_size &&
102 before.st_mtim.tv_sec == after.st_mtim.tv_sec && before.st_mtim.tv_nsec == after.st_mtim.tv_nsec && !fsync(output);
103 if (close(input)) valid = false;
104 if (close(output)) valid = false;
105 if (!valid) unlink(destination);
106 return valid;
107}
108}
109
110int inference_capture_cli(int argc, char **argv, const inference_image_processor &processor,
111 const imx500::FrameProcessor &sensor_processor)
112{
113 if (argc < 4 || std::strcmp(argv[2], "--settings")) return -1;
114 const bool serve = !std::strcmp(argv[1], "--serve"), probe = !std::strcmp(argv[1], "--probe");
115 const bool capture = !std::strcmp(argv[1], "--capture");
116 if ((!serve && !probe && !capture) || (serve && argc != 6) || (probe && argc != 4) || (capture && argc != 5)) return 2;
117 inference_settings settings;
119 char error[2048]{};
120 char discovered[INFERENCE_PATH_CAPACITY];
121 const char *settings_path = argv[3];
122 if (!std::strcmp(settings_path, "-")) {
123 if (!inference_settings_find(processor.application, processor.module, discovered, sizeof(discovered))) {
124 std::fprintf(stderr, "%s: settings not found; use --settings FILE\n", processor.application);
125 return 1;
126 }
127 settings_path = discovered;
128 }
129 if (!inference_settings_read(&settings, settings_path, processor.setting, processor.context, error, sizeof(error))) {
130 std::fprintf(stderr, "%s: %s\n", processor.application, error);
131 return 1;
132 }
133 if (settings.backend == INFERENCE_IMX500) {
134 if (settings.source != INFERENCE_LIVE || !settings.model[0]) return 2;
135 char *arguments[] = {argv[0], argv[1], settings.model,
136 capture ? argv[4] : serve ? argv[4] : nullptr, serve ? argv[5] : nullptr, nullptr};
137 return imx500::run_worker(serve ? 5 : capture ? 4 : 3, arguments, sensor_processor, processor.application);
138 }
139 if (settings.backend != INFERENCE_ONNX) return 2;
140 uint64_t width = 2028, height = 1520;
141 if (serve && (!ini_uint(argv[4], &width) || !ini_uint(argv[5], &height))) return 2;
142 if (width > std::numeric_limits<unsigned>::max() || height > std::numeric_limits<unsigned>::max() ||
143 !imx500::valid_capture_dimensions(static_cast<unsigned>(width), static_cast<unsigned>(height))) return 2;
144 try {
145 ContextOwner owner;
146 auto &context = owner.context;
147 if (!inference_context_open(&context, &settings, processor.adapter, error, sizeof(error))) throw std::runtime_error(error);
148 if (processor.validate && !processor.validate(processor.context, &context, error, sizeof(error))) throw std::runtime_error(error);
149 std::unique_ptr<imx500::Camera> camera;
150 Preparation preparation{&context, error, sizeof(error)};
151 if (settings.source == INFERENCE_LIVE) {
152 camera = std::make_unique<imx500::Camera>();
153 camera->open(nullptr, static_cast<unsigned>(width), static_cast<unsigned>(height));
154 {
155 auto frame = camera->capture();
156 if (!camera->with_pixels(frame, prepare_pixels, &preparation)) throw std::runtime_error(error);
157 }
158 } else if (!settings.input[0] || !inference_jpeg_prepare(&context, settings.input, error, sizeof(error))) {
159 throw std::runtime_error(settings.input[0] ? error : "Picture source requires input in settings");
160 }
161 if (!inference_context_run(&context, error, sizeof(error))) throw std::runtime_error(error);
162 if (probe) { std::puts("camera and ONNX model ready"); return 0; }
163 const auto shoot = [&](const char *filename, uint32_t sequence) {
164 struct stat existing{};
165 if (!filename || lstat(filename, &existing) == 0 || errno != ENOENT) return false;
166 const std::string staged = std::string(filename) + ".onnx.pending.jpg";
167 if (staged.size() >= INFERENCE_PATH_CAPACITY) return false;
168 bool saved = false, prepared = false;
169 if (camera) {
170 auto frame = camera->capture();
171 prepared = camera->with_pixels(frame, prepare_pixels, &preparation);
172 const imx500::FrameProcessor photograph{nullptr, nullptr, nullptr, retain_pixels};
173 saved = camera->save(frame, staged.c_str(), photograph);
174 } else {
175 saved = copy_picture(settings.input, staged.c_str());
176 prepared = saved && inference_jpeg_prepare(&context, staged.c_str(), error, sizeof(error));
177 }
178 if (!saved) return false;
179 /* The staged image is still exclusively producer-owned, so LOOM may embed feature evidence here. */
180 context.private_image = true;
181 const bool analyzed = prepared && inference_context_run(&context, error, sizeof(error)) &&
182 processor.write(processor.context, &context, staged.c_str(), sequence, error, sizeof(error));
183 if (!analyzed)
184 std::fprintf(stderr, "%s: analysis_failed: %s\n", processor.application, error);
185 std::string staged_result, published_result;
186 bool result_published = false;
187 if (analyzed && processor.result_suffix) {
188 staged_result = staged.substr(0, staged.size() - 4) + processor.result_suffix;
189 const size_t length = std::strlen(filename);
190 if (length < 4 || std::strcmp(filename + length - 4, ".jpg")) {
191 unlink(staged_result.c_str());
192 unlink(staged.c_str());
193 return false;
194 }
195 published_result = std::string(filename, length - 4) + processor.result_suffix;
196 result_published = link(staged_result.c_str(), published_result.c_str()) == 0;
197 if (!result_published) {
198 unlink(staged_result.c_str());
199 unlink(staged.c_str());
200 return false;
201 }
202 }
203 const bool published = link(staged.c_str(), filename) == 0;
204 /* Remove only names created by this attempt; never erase a pre-existing image or analysis result. */
205 if (!published && result_published) unlink(published_result.c_str());
206 if (!staged_result.empty()) unlink(staged_result.c_str());
207 unlink(staged.c_str());
208 if (!published) return false;
209 return true;
210 };
211 if (capture) return shoot(argv[4], 1) ? 0 : 1;
212 std::puts("READY");
213 if (std::fflush(stdout)) return 1;
214 char command[INFERENCE_PATH_CAPACITY + 16];
215 uint32_t sequence = 0;
216 while (std::fgets(command, sizeof(command), stdin)) {
217 const size_t length = std::strlen(command);
218 if (!length || command[length - 1] != '\n' || std::strncmp(command, "SHOT ", 5) || sequence == UINT32_MAX) return 2;
219 command[length - 1] = '\0';
220 if (command[5] != '/' || std::strchr(command + 5, '\r')) return 2;
221 std::puts(shoot(command + 5, ++sequence) ? "OK" : "ERROR");
222 if (std::fflush(stdout)) return 1;
223 }
224 return std::ferror(stdin) ? 1 : 0;
225 } catch (const std::exception &failure) {
226 std::fprintf(stderr, "%s: %s\n", processor.application, failure.what());
227 return 1;
228 }
229}
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
Definition bounded_ini.h:52
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
static bool capture(struct transport *transport, FILE *file, const struct capture_settings *settings)
Capture to the camera's SD card and download the resulting JPEG.
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
int inference_capture_cli(int argc, char **argv, const inference_image_processor &processor, const imx500::FrameProcessor &sensor_processor)
Dispatch configured RPK/ONNX capture while preserving one physical camera owner.
Settings-driven capture bridge; neutral camera mechanics never interpret neural outputs.
@ INFERENCE_YUV420
bool inference_jpeg_prepare(struct inference_context *context, const char *filename, char *error, size_t capacity)
Stream a JPEG into an open context's single declared image input.
bool inference_context_run(struct inference_context *context, char *error, size_t capacity)
Run the currently prepared tensors using the context's timeout policy.
bool inference_context_open(struct inference_context *context, const struct inference_settings *settings, const char *adapter, char *error, size_t capacity)
Verify a bundle, map bounded tensors and bind one warm session.
bool inference_context_prepare_image(struct inference_context *context, const struct inference_image *image, char *error, size_t capacity)
Prepare a single image input without running the model, so camera leases can be released first.
void inference_context_close(struct inference_context *context)
Release the session before unmapping its borrowed tensor storage.
bool inference_settings_find(const char *application, const char *module, char *filename, size_t capacity)
Discover application_settings.ini in adjacent, development-module, then installed-share order.
bool inference_settings_read(struct inference_settings *settings, const char *filename, ini_field_handler extra, void *context, char *error, size_t capacity)
Parse one flat INI, delegate application keys and resolve paths against the selected file.
void inference_settings_init(struct inference_settings *settings, enum inference_backend backend)
Reset storage and select conservative thread/timeout defaults.
@ INFERENCE_LIVE
#define INFERENCE_PATH_CAPACITY
@ INFERENCE_ONNX
@ INFERENCE_IMX500
struct jpeg_error_mgr error
Definition lwir_image.c:60
uint8_t frame[LWIR_MAX_FRAME_BYTES]
Definition lwir_mock.c:58
int run_worker(int argc, char **argv, const FrameProcessor &processor, const char *application)
Run the shared probe/capture/READY-SHOT-OK worker with supplied model policy.
constexpr bool valid_capture_dimensions(unsigned width, unsigned height)
Check that capture dimensions meet the worker's raster policy.
static size_t count
Definition pose_log.c:79
static FILE * output
Definition pose_log.c:77
Caller-owned bounded writer with no heap allocation.
Definition exifite.h:156
Stack-owned model-specific operations; no dynamic plugin allocation.
Borrowed same-exposure evidence, without object or navigation semantics.
libcamera::Size image
uint64_t sensor_timestamp_ns
Borrowed module callbacks; the worker serializes calls and retains no per-request pixel pointers.
bool(* validate)(void *context, const struct inference_context *model, char *error, size_t capacity)
bool(* write)(void *context, const struct inference_context *model, const char *image, uint32_t sequence, char *error, size_t capacity)
Borrowed immutable planes; lengths/strides prove all addressed rows are available.
Per-application configuration; all resolved paths belong to this caller-owned value.
enum inference_backend backend
char model[INFERENCE_PATH_CAPACITY]
char input[INFERENCE_PATH_CAPACITY]
enum inference_source source
static struct camera_backend camera
Definition usher.c:521
bool valid
Definition usher.c:537
struct captured_image image
Definition usher.c:371