-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSSCMA_Micro_Core.h
381 lines (321 loc) · 12.4 KB
/
SSCMA_Micro_Core.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/***
* @file SSCMA_Micro_Core.h
*
* @details This header file declares the SSCMA Micro Core class, which
* provides a microcontroller optimized AI inference API for Arduino.
*
* Copyright (C) 2024Seeed Technology Co., Ltd. All right reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#ifndef _SSCMA_MICRO_CORE_H_
#define _SSCMA_MICRO_CORE_H_
#if defined(__AVR__)
#error "Insufficient memory: This code cannot run on any AVR platform due to limited memory."
#endif
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#if (CONFIG_IDF_TARGET_ESP32 | CONFIG_IDF_TARGET_ESP32S3 | CONFIG_IDF_TARGET_ESP32S2)
#define MA_PORTING_ESPRESSIF_ESP32 1
#endif
#if MA_PORTING_ESPRESSIF_ESP32
#include <esp_camera.h>
#endif
#ifndef MA_RETURN_IF_UNEXPECTED
#define MA_RETURN_IF_UNEXPECTED(expr) \
do { \
auto ret = (expr); \
if (!ret.success) { \
printf("%s\n", ret.message.c_str()); \
return; \
} \
} while (0)
#else
#warning "MA_RETURN_IF_UNEXPECTED is already defined."
#endif
/**
* @brief The SSCMAMicroCore class provides a microcontroller optimized for SSCMA.
*/
class SSCMAMicroCore {
public:
/**
* @brief Configuration for invoking the SSCMAMicroCore.
*/
struct InvokeConfig {
int top_k; ///< The number of top results to return.
float score_threshold; ///< The minimum score threshold for results.
float nms_threshold; ///< The non-maximum suppression threshold.
};
/**
* @brief Configuration for the SSCMAMicroCore.
*/
struct Config {
int model_id; ///< The ID of the model to use.
int algorithm_id; ///< The ID of the algorithm to use.
const InvokeConfig* invoke_config; ///< The invocation configuration.
static Config DefaultConfig; ///< The default configuration.
};
/**
* @brief Represents a bounding box in an image.
*/
struct Box {
float x; ///< The x-coordinate of the box.
float y; ///< The y-coordinate of the box.
float w; ///< The width of the box.
float h; ///< The height of the box.
float score; ///< The confidence score of the box.
int target; ///< The target ID associated with the box.
};
/**
* @brief Represents a class detection result.
*/
struct Class {
int target; ///< The target ID of the class.
float score; ///< The confidence score of the class.
};
/**
* @brief Represents a point in 3D / 2D (with score) space.
*/
struct Point {
float x; ///< The x-coordinate of the point.
float y; ///< The y-coordinate of the point.
float z; ///< The z-coordinate of the point.
float score; ///< The confidence score of the point.
int target; ///< The target ID associated with the point.
};
/**
* @brief Represents keypoints detected in an image.
*/
struct Keypoints {
Box box; ///< The bounding box of the keypoints.
std::vector<Point> points; ///< The list of keypoints.
};
/**
* @brief Typedef for a callback function that handles boxes.
*/
using BoxesCallback = std::function<void(const std::vector<Box>&, void*)>;
/**
* @brief Typedef for a callback function that handles classes.
*/
using ClassesCallback = std::function<void(const std::vector<Class>&, void*)>;
/**
* @brief Typedef for a callback function that handles points.
*/
using PointsCallback = std::function<void(const std::vector<Point>&, void*)>;
/**
* @brief Typedef for a callback function that handles keypoints.
*/
using KeypointsCallback = std::function<void(const std::vector<Keypoints>&, void*)>;
/**
* @brief Performance metrics for the SSCMAMicroCore.
*/
struct Perf {
uint32_t preprocess; ///< Time taken for preprocessing.
uint32_t inference; ///< Time taken for inference.
uint32_t postprocess; ///< Time taken for postprocessing.
};
/**
* @brief Typedef for a callback function that handles performance metrics.
*/
using PerfCallback = std::function<void(const Perf&, void*)>;
/**
* @brief Enum for pixel formats.
*/
enum PixelFormat {
kUNKNOWN = 0, ///< Unknown pixel format.
kRGB888 = 1, ///< 24-bit RGB pixel format.
kRGB565, ///< 16-bit RGB pixel format.
kGRAY8, ///< 8-bit grayscale pixel format.
kJPEG ///< JPEG compressed image format.
};
/**
* @brief Represents a frame of image data.
*/
struct Frame {
PixelFormat format; ///< The pixel format of the frame.
uint16_t width; ///< The width of the frame.
uint16_t height; ///< The height of the frame.
uint16_t orientation; ///< The orientation of the frame.
struct timeval timestamp; ///< The timestamp of the frame.
uint32_t size; ///< The size of the frame data.
uint8_t* data; ///< The frame data.
#if MA_PORTING_ESPRESSIF_ESP32
/**
* @brief Creates a Frame from an ESP32 camera frame.
* @param frame The ESP32 camera frame to convert from.
* @return A new Frame instance.
*/
static Frame fromCameraFrame(const camera_fb_t* frame);
#endif
};
/**
* @brief Represents an expected result with a success flag and message.
*/
struct Expected {
bool success; ///< Whether the operation was successful.
std::string message; ///< A message describing the result.
};
/**
* @brief Represents a video capture device.
*/
struct VideoCapture {
/**
* @brief Begins the video capture device.
* @return An Expected result indicating success or failure.
*/
Expected begin();
#if MA_PORTING_ESPRESSIF_ESP32
/**
* @brief Begins the video capture device with the given configuration.
* @param config The configuration to use.
* @return An Expected result indicating success or failure.
*/
Expected begin(const camera_config_t& config);
#endif
/**
* @brief Ends the video capture device.
* @return An Expected result indicating success or failure.
*/
std::shared_ptr<Frame> getManagedFrame();
#if MA_PORTING_ESPRESSIF_ESP32
/**
* @brief Default camera configuration for ESP32-CAM.
*/
static camera_config_t DefaultCameraConfigXIAOS3;
#endif
};
public:
/**
* @brief Constructs a new SSCMAMicroCore instance.
*/
SSCMAMicroCore();
/**
* @brief Destructs the SSCMAMicroCore instance.
*/
~SSCMAMicroCore();
/**
* @brief Begins the SSCMAMicroCore with the given configuration.
* @param config The configuration to use.
* @return An Expected result indicating success or failure.
*/
Expected begin(const Config& config);
/**
* @brief Invokes the SSCMAMicroCore with the given frame and optional configuration.
* @param frame The frame to process.
* @param config The optional invocation configuration (overrides the current configuration).
* @param user_context User-provided context to pass to callbacks.
* @return An Expected result indicating success or failure.
*/
Expected invoke(const Frame& frame, const InvokeConfig* config = nullptr, void* user_context = nullptr);
/**
* @brief Invokes the SSCMAMicroCore with the given frame and optional configuration.
* @param frame The frame to process.
* @param config The optional invocation configuration (overrides the current configuration).
* @param user_context User-provided context to pass to callbacks.
* @return An Expected result indicating success or failure.
*/
Expected invoke(std::shared_ptr<Frame> frame, const InvokeConfig* config = nullptr, void* user_context = nullptr);
/**
* @brief Registers a callback for box detection results.
* @param callback The callback function to register.
*/
void registerBoxesCallback(BoxesCallback callback);
/**
* @brief Registers a callback for class detection results.
* @param callback The callback function to register.
*/
void registerClassesCallback(ClassesCallback callback);
/**
* @brief Registers a callback for point detection results.
* @param callback The callback function to register.
*/
void registerPointsCallback(PointsCallback callback);
/**
* @brief Registers a callback for keypoint detection results.
* @param callback The callback function to register.
*/
void registerKeypointsCallback(KeypointsCallback callback);
/**
* @brief Registers a callback for performance metrics.
* @param callback The callback function to register.
*/
void registerPerfCallback(PerfCallback callback);
/**
* @brief Gets the detected boxes.
* @return The detected boxes.
*/
const std::vector<Box>& getBoxes() const {
return _boxes;
}
/**
* @brief Gets the classification results.
* @return The classification results.
*/
const std::vector<Class>& getClasses() const {
return _classes;
}
/**
* @brief Gets the detected points.
* @return The detected points.
*/
const std::vector<Point>& getPoints() const {
return _points;
}
/**
* @brief Gets the detected keypoints.
* @return The detected keypoints.
*/
const std::vector<Keypoints>& getKeypoints() const {
return _keypoints;
}
/**
* @brief Gets the performance metrics.
* @return The performance metrics.
*/
const Perf& getPerf() const {
return _perf;
}
public:
static BoxesCallback DefaultBoxesCallback; ///< The default box callback.
static ClassesCallback DefaultClassesCallback; ///< The default class callback.
static PointsCallback DefaultPointsCallback; ///< The default point callback.
static KeypointsCallback DefaultKeypointsCallback; ///< The default keypoint callback.
static PerfCallback DefaultPerfCallback; ///< The default performance callback.
private:
bool _initialized; ///< Whether the SSCMAMicroCore is initialized.
Config _config; ///< The current configuration of the SSCMAMicroCore.
BoxesCallback _boxes_callback; ///< The registered box callback.
ClassesCallback _classes_callback; ///< The registered class callback.
PointsCallback _points_callback; ///< The registered point callback.
KeypointsCallback _keypoints_callback; ///< The registered keypoint callback.
PerfCallback _perf_callback; ///< The registered performance callback.
std::vector<Box> _boxes; ///< The detected boxes.
std::vector<Class> _classes; ///< The detected classes.
std::vector<Point> _points; ///< The detected points.
std::vector<Keypoints> _keypoints; ///< The detected keypoints.
Perf _perf; ///< The performance metrics.
};
#endif //_SSCMA_MICRO_CORE_H_