forked from edgeimpulse/edge-impulse-runner-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.patch
More file actions
209 lines (202 loc) · 8.02 KB
/
Copy pathapply.patch
File metadata and controls
209 lines (202 loc) · 8.02 KB
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
diff --git a/src/backends/ffi.rs b/src/backends/ffi.rs
index bf94f2c..0257f44 100644
--- a/src/backends/ffi.rs
+++ b/src/backends/ffi.rs
@@ -229,6 +229,7 @@ impl InferenceBackend for FfiBackend {
y: bb.y as i32,
width: bb.width as i32,
height: bb.height as i32,
+ object_id: bb.object_id, // Pass through object_id from FFI
})
.collect();
@@ -262,6 +263,7 @@ impl InferenceBackend for FfiBackend {
y: bb.y as i32,
width: bb.width as i32,
height: bb.height as i32,
+ object_id: bb.object_id, // Pass through object_id from FFI
})
.collect();
diff --git a/src/ffi/wrapper.rs b/src/ffi/wrapper.rs
index 7d0df31..5308563 100644
--- a/src/ffi/wrapper.rs
+++ b/src/ffi/wrapper.rs
@@ -14,11 +14,19 @@ impl fmt::Display for ClassificationResult {
impl fmt::Display for BoundingBox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(
- f,
- "{}: {:.4} (x={}, y={}, w={}, h={})",
- self.label, self.value, self.x, self.y, self.width, self.height
- )
+ if let Some(object_id) = self.object_id {
+ write!(
+ f,
+ "{}: {:.4} (x={}, y={}, w={}, h={}, id={})",
+ self.label, self.value, self.x, self.y, self.width, self.height, object_id
+ )
+ } else {
+ write!(
+ f,
+ "{}: {:.4} (x={}, y={}, w={}, h={})",
+ self.label, self.value, self.x, self.y, self.width, self.height
+ )
+ }
}
}
@@ -267,7 +275,6 @@ impl InferenceResult {
.to_string_lossy()
.into_owned()
} else {
- eprintln!("[EdgeImpulse FFI] Warning: classification label pointer is null at index {i}");
String::new()
};
ClassificationResult {
@@ -292,12 +299,6 @@ impl InferenceResult {
unsafe {
let result = &*self.result;
if result.bounding_boxes_count == 0 || result.bounding_boxes.is_null() {
- if result.bounding_boxes_count > 0 {
- eprintln!(
- "[EdgeImpulse FFI] Warning: bounding_boxes pointer is null but count is {}",
- result.bounding_boxes_count
- );
- }
return vec![];
}
let bbs = std::slice::from_raw_parts(
@@ -307,7 +308,6 @@ impl InferenceResult {
bbs.iter()
.filter_map(|bb| {
if bb.value == 0.0 {
- eprintln!("[EdgeImpulse FFI] Skipping bounding box with value 0.0");
return None;
}
let label = if !bb.label.is_null() {
@@ -315,11 +315,12 @@ impl InferenceResult {
.to_string_lossy()
.into_owned()
} else {
- eprintln!(
- "[EdgeImpulse FFI] Warning: bounding box label pointer is null"
- );
String::new()
};
+
+ // Try to extract object tracking ID safely
+ let object_id = self.extract_object_tracking_id_safe(bb, &label);
+
Some(BoundingBox {
label,
value: bb.value,
@@ -327,6 +328,7 @@ impl InferenceResult {
y: bb.y,
width: bb.width,
height: bb.height,
+ object_id,
})
})
.collect()
@@ -362,10 +364,6 @@ impl InferenceResult {
// Get the grid cells
let grid_cells = if result.visual_ad_grid_cells.is_null() {
- eprintln!(
- "[EdgeImpulse FFI] Warning: visual_ad_grid_cells pointer is null but count is {}",
- result.visual_ad_count
- );
vec![]
} else {
let cells = std::slice::from_raw_parts(
@@ -382,7 +380,6 @@ impl InferenceResult {
.to_string_lossy()
.into_owned()
} else {
- eprintln!("[EdgeImpulse FFI] Warning: visual anomaly grid cell label pointer is null");
String::new()
};
Some(BoundingBox {
@@ -392,6 +389,7 @@ impl InferenceResult {
y: cell.y,
width: cell.width,
height: cell.height,
+ object_id: None, // Visual anomaly doesn't have object tracking
})
})
.collect()
@@ -666,6 +664,8 @@ pub struct BoundingBox {
pub y: u32,
pub width: u32,
pub height: u32,
+ /// Object tracking ID (only present when object tracking is enabled)
+ pub object_id: Option<u32>,
}
#[derive(Debug, Clone)]
@@ -1132,3 +1132,39 @@ mod tests {
let _ = classifier.deinit();
}
}
+
+impl InferenceResult {
+ /// Extract object tracking ID safely without accessing potentially invalid pointers
+ #[cfg(feature = "ffi")]
+ fn extract_object_tracking_id_safe(
+ &self,
+ bb: &edge_impulse_ffi_rs::bindings::ei_impulse_result_bounding_box_t,
+ label: &str,
+ ) -> Option<u32> {
+ unsafe {
+ let result = &*self.result;
+ let tracking_output = &result.postprocessed_output.object_tracking_output;
+
+ // If no object tracking data, return None
+ if tracking_output.open_traces_count == 0 || tracking_output.open_traces.is_null() {
+ return None;
+ }
+
+ // For now, let's try a simpler approach - just use the trace ID directly
+ // without trying to match by label, since the label pointers seem to be invalid
+ let traces = std::slice::from_raw_parts(
+ tracking_output.open_traces,
+ tracking_output.open_traces_count as usize,
+ );
+
+ // Simple approach: just return the first trace ID for now
+ // This is a temporary solution until we can figure out why the label pointers are invalid
+ if !traces.is_empty() {
+ let first_trace = &traces[0];
+ return Some(first_trace.id as u32);
+ }
+
+ None
+ }
+ }
+}
diff --git a/src/types.rs b/src/types.rs
index a2b7eb6..6c30a46 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -197,6 +197,26 @@ pub struct BoundingBox {
pub x: i32,
/// Y-coordinate of the top-left corner
pub y: i32,
+ /// Object tracking ID (only present when object tracking is enabled)
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub object_id: Option<u32>,
+}
+
+/// Represents object tracking trace data extracted from the C++ SDK
+#[derive(Debug, Clone)]
+pub struct ObjectTrackingTrace {
+ /// Unique identifier for the tracked object
+ pub id: i32,
+ /// Label/class of the tracked object
+ pub label: String,
+ /// X coordinate of the bounding box
+ pub x: u32,
+ /// Y coordinate of the bounding box
+ pub y: u32,
+ /// Width of the bounding box
+ pub width: u32,
+ /// Height of the bounding box
+ pub height: u32,
}
/// Represents the normalized results of visual anomaly detection