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_worker.c
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_worker.c
19 * @brief Persistent saved-picture inference with explicit request correlation and module-owned results.
20 * @details No sensor is acquired here; simulated and real saved images enter the same model path. */
21#include "inference_worker.h"
22
23#include <errno.h>
24#include <stdio.h>
25#include <string.h>
26
27/** @brief Publish a result only after complete preparation and execution for this picture. */
28static bool analyze_image(struct inference_context *context,
29 const struct inference_image_processor *processor,
30 const char *image, uint32_t sequence, char *error, size_t capacity)
31{
32 return inference_jpeg_prepare(context, image, error, capacity) &&
34 processor->write(processor->context, context, image, sequence, error, capacity);
35}
36
37/** @brief Probe sibling INT8 then FP32 ONNX exports when automatic settings point to an RPK or unsupported INT8 graph. */
38static bool open_fallback_onnx(struct inference_context *context,
39 struct inference_settings *settings,
40 const struct inference_image_processor *processor,
41 char *error, size_t capacity)
42{
43 const char *suffix = strrchr(settings->model, '.');
44 const char *slash = strrchr(settings->model, '/');
45 if (!suffix || !slash || (settings->backend != INFERENCE_IMX500 &&
46 strcmp(slash + 1, "vehicle_yolo11n_int8.onnx") != 0)) return false;
47 static const char *const candidates[][2] = {
48 {"vehicle_yolo11n_int8.onnx", "vehicle_yolo11n_int8.ini"},
49 {"vehicle_yolo11n_fp32.onnx", "vehicle_yolo11n_fp32.ini"}
50 };
51 const int directory_length = (int)(slash - settings->model);
52 for (size_t index = 0; index < sizeof(candidates) / sizeof(candidates[0]); ++index) {
53 struct inference_settings candidate = *settings;
54 candidate.backend = INFERENCE_ONNX;
55 const int model_len = snprintf(candidate.model, sizeof(candidate.model), "%.*s/%s",
56 directory_length, settings->model, candidates[index][0]);
57 const int desc_len = snprintf(candidate.descriptor, sizeof(candidate.descriptor), "%.*s/%s",
58 directory_length, settings->model, candidates[index][1]);
59 if (model_len < 0 || (size_t)model_len >= sizeof(candidate.model) ||
60 desc_len < 0 || (size_t)desc_len >= sizeof(candidate.descriptor)) continue;
61 char probe_error[2048];
62 if (inference_context_open(context, &candidate, processor->adapter, probe_error, sizeof(probe_error))) {
63 if (!processor->validate || processor->validate(processor->context, context, probe_error, sizeof(probe_error))) {
64 *settings = candidate;
65 if (capacity) error[0] = '\0';
66 return true;
67 }
69 }
70 if (probe_error[0]) snprintf(error, capacity, "%s", probe_error);
71 }
72 return false;
73}
74
75int inference_worker_cli(int argc, char **argv, const struct inference_image_processor *processor)
76{
77 bool server = false, check = false, selected = false, private_images = false;
78 const char *image = NULL, *settings_path = NULL;
79 uint32_t sequence = 1;
80 if (!processor || !processor->write || !processor->application || !processor->module || !processor->adapter) return 2;
81 for (int index = 1; index < argc; ++index) {
82 if (!strcmp(argv[index], "--analyze") || !strcmp(argv[index], "--analyze-server") || !strcmp(argv[index], "--check-model")) selected = true;
83 }
84 if (!selected) return -1;
85 for (int index = 1; index < argc; ++index) {
86 if (!strcmp(argv[index], "--analyze-server")) { if (server) return 2; server = true; }
87 else if (!strcmp(argv[index], "--check-model")) { if (check) return 2; check = true; }
88 else if (!strcmp(argv[index], "--private-images")) { if (private_images) return 2; private_images = true; }
89 else {
90 const char *option = argv[index];
91 if (++index == argc) return 2;
92 if (!strcmp(option, "--analyze")) { if (image) return 2; image = argv[index]; }
93 else if (!strcmp(option, "--settings")) { if (settings_path) return 2; settings_path = argv[index]; }
94 else if (!strcmp(option, "--sequence")) {
95 uint64_t number;
96 if (!ini_uint(argv[index], &number) || !number || number > UINT32_MAX) return 2;
97 sequence = (uint32_t)number;
98 } else return 2;
99 }
100 }
101 if ((unsigned)server + (unsigned)check + (unsigned)(image != NULL) != 1) return 2;
102 char discovered[INFERENCE_PATH_CAPACITY], error[2048];
103 if (!settings_path) {
104 if (!inference_settings_find(processor->application, processor->module, discovered, sizeof(discovered))) {
105 fprintf(stderr, "%s: settings not found; use --settings FILE\n", processor->application);
106 return 1;
107 }
108 settings_path = discovered;
109 }
110 struct inference_settings settings;
112 if (!inference_settings_read(&settings, settings_path, processor->setting, processor->context, error, sizeof(error))) {
113 fprintf(stderr, "%s: %s\n", processor->application, error);
114 return 1;
115 }
116 struct inference_context context = {0};
117 if (!inference_context_open(&context, &settings, processor->adapter, error, sizeof(error)) &&
118 !open_fallback_onnx(&context, &settings, processor, error, sizeof(error))) {
119 fprintf(stderr, "%s: %s\n", processor->application, error);
120 return 1;
121 }
122 if (processor->validate && !processor->validate(processor->context, &context, error, sizeof(error))) {
123 fprintf(stderr, "%s: %s\n", processor->application, error);
124 inference_context_close(&context);
125 return 1;
126 }
127 int result = 0;
128 context.private_image = private_images;
129 /* Only a producer may opt into mutation; normal analysis must leave published source images immutable. */
130 if (check) {
131 if (!inference_context_run(&context, error, sizeof(error))) result = 1;
132 else printf("%s model ready adapter=%s sha256=%s\n", processor->application, context.model.adapter, context.model.graph_sha256);
133 } else if (image) {
134 if (!analyze_image(&context, processor, image, sequence, error, sizeof(error))) result = 1;
135 else puts("OK");
136 } else {
137 puts("READY");
138 if (fflush(stdout)) result = 1;
139 uint32_t previous = 0;
140 char command[INFERENCE_PATH_CAPACITY + 64];
141 while (!result && fgets(command, sizeof(command), stdin)) {
142 const size_t length = strlen(command);
143 if (!length || command[length - 1] != '\n' || strncmp(command, "ANALYZE ", 8)) { result = 2; break; }
144 command[length - 1] = '\0';
145 char *number = command + 8, *path = strchr(number, ' ');
146 if (!path) { result = 2; break; }
147 *path++ = '\0';
148 uint64_t parsed;
149 /* Reject replay/reordering before running the graph, not after an old result has already been written. */
150 if (!ini_uint(number, &parsed) || parsed <= previous || parsed > UINT32_MAX || !path[0] || strchr(path, '\r')) { result = 2; break; }
151 previous = (uint32_t)parsed;
152 const bool analyzed = analyze_image(&context, processor, path, previous, error, sizeof(error));
153 if (!analyzed) fprintf(stderr, "%s: %s\n", processor->application, error);
154 puts(analyzed ? "OK" : "ERROR");
155 if (fflush(stdout)) result = 1;
156 }
157 if (ferror(stdin)) result = 1;
158 }
159 if (result == 1) fprintf(stderr, "%s: %s\n", processor->application, error);
160 inference_context_close(&context);
161 return result;
162}
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
Definition bounded_ini.h:52
static unsigned capacity[COVERAGE_CAMERAS]
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.
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.
#define INFERENCE_PATH_CAPACITY
@ INFERENCE_ONNX
@ INFERENCE_IMX500
static bool analyze_image(struct inference_context *context, const struct inference_image_processor *processor, const char *image, uint32_t sequence, char *error, size_t capacity)
Publish a result only after complete preparation and execution for this picture.
int inference_worker_cli(int argc, char **argv, const struct inference_image_processor *processor)
Dispatch one-shot analysis, model checks or the persistent ANALYZE protocol.
static bool open_fallback_onnx(struct inference_context *context, struct inference_settings *settings, const struct inference_image_processor *processor, char *error, size_t capacity)
Probe sibling INT8 then FP32 ONNX exports when automatic settings point to an RPK or unsupported INT8...
Warm saved-picture processing without acquiring a camera or interpreting CAM results.
struct jpeg_error_mgr error
Definition lwir_image.c:60
Independent, noncopyable model owner; mapped tensor buffers and session are reused between jobs.
struct inference_model model
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)
Per-application configuration; all resolved paths belong to this caller-owned value.
enum inference_backend backend
char model[INFERENCE_PATH_CAPACITY]
char descriptor[INFERENCE_PATH_CAPACITY]
struct usher_control_result result
Definition usher.c:377
struct captured_image image
Definition usher.c:371