Skip to content

Commit 8d0263a

Browse files
authored
Always use RGB bit-shifting approach for image processing (#16)
1 parent a5f72a7 commit 8d0263a

File tree

2 files changed

+24
-51
lines changed

2 files changed

+24
-51
lines changed

src/common.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,23 @@ where
210210
id,
211211
min_score,
212212
} => {
213-
format!("{}.min_score={}", id, min_score)
213+
format!("{id}.min_score={min_score}")
214214
}
215215
edge_impulse_runner::types::ModelThreshold::AnomalyGMM {
216216
id,
217217
min_anomaly_score,
218218
} => {
219-
format!("{}.min_anomaly_score={}", id, min_anomaly_score)
219+
format!("{id}.min_anomaly_score={min_anomaly_score}")
220220
}
221221
edge_impulse_runner::types::ModelThreshold::ObjectTracking {
222222
id,
223223
threshold,
224224
..
225225
} => {
226-
format!("{}.threshold={}", id, threshold)
226+
format!("{id}.threshold={threshold}")
227227
}
228228
edge_impulse_runner::types::ModelThreshold::Unknown { id, unknown } => {
229-
format!("{}.unknown={}", id, unknown)
229+
format!("{id}.unknown={unknown}")
230230
}
231231
})
232232
.collect();
@@ -253,7 +253,7 @@ pub fn create_inference_message(
253253
result_json: String,
254254
timing_ms: u32,
255255
) -> gst::Structure {
256-
gst::Structure::builder(format!("edge-impulse-{}-inference-result", element_type))
256+
gst::Structure::builder(format!("edge-impulse-{element_type}-inference-result"))
257257
.field("timestamp", timestamp)
258258
.field("type", result_type)
259259
.field("result", result_json)
@@ -267,7 +267,7 @@ pub fn create_error_message(
267267
timestamp: gst::ClockTime,
268268
error: String,
269269
) -> gst::Structure {
270-
gst::Structure::builder(format!("edge-impulse-{}-inference-result", element_type))
270+
gst::Structure::builder(format!("edge-impulse-{element_type}-inference-result"))
271271
.field("timestamp", timestamp)
272272
.field("type", "error")
273273
.field("error", error)

src/video/imp.rs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -484,54 +484,27 @@ impl BaseTransformImpl for EdgeImpulseVideoInfer {
484484
}
485485
};
486486

487-
// Convert frame data to features based on channel count and input format
488-
let features = if channels == 3 {
489-
if format == Some(VideoFormat::Gray8) {
490-
// Convert grayscale to RGB features
491-
let mut features = Vec::with_capacity((width * height) as usize);
492-
for &gray in frame_data {
493-
// Pack grayscale value into RGB format
494-
let packed = (gray as u32) << 16 | (gray as u32) << 8 | (gray as u32);
495-
features.push(packed as f32);
496-
}
497-
features
498-
} else {
499-
// RGB: Pack RGB values into single numbers
500-
let mut features = Vec::with_capacity((width * height) as usize);
501-
for chunk in frame_data.chunks_exact(3) {
502-
if let [r, g, b] = chunk {
503-
// Pack RGB values into a single number: (r << 16) + (g << 8) + b
504-
let packed = (*r as u32) << 16 | (*g as u32) << 8 | (*b as u32);
505-
features.push(packed as f32);
506-
}
507-
}
508-
features
487+
// Always pass in full RGB array in the form of pixels like 0xff0000
488+
// DSP handles normalization and splitting into 1 or 3 channels
489+
let mut features = Vec::with_capacity((width * height) as usize);
490+
491+
if format == Some(VideoFormat::Gray8) {
492+
// For grayscale images, create RGB values by repeating the grayscale value
493+
for &pixel in frame_data {
494+
// Create 24-bit RGB value: 0xRRGGBB where R=G=B=pixel
495+
let feature = ((pixel as u32) << 16) | ((pixel as u32) << 8) | (pixel as u32);
496+
features.push(feature as f32);
509497
}
510498
} else {
511-
// Grayscale model: Convert input to grayscale if needed
512-
let mut features = Vec::with_capacity((width * height) as usize);
513-
if format == Some(VideoFormat::Gray8) {
514-
// Already grayscale, just pack the values
515-
for &gray in frame_data {
516-
let packed = (gray as u32) << 16 | (gray as u32) << 8 | (gray as u32);
517-
features.push(packed as f32);
518-
}
519-
} else {
520-
// Convert RGB to grayscale and pack
521-
for chunk in frame_data.chunks_exact(3) {
522-
if let [r, g, b] = chunk {
523-
// Convert RGB to grayscale using standard weights
524-
let gray = (0.299 * (*r as f32)
525-
+ 0.587 * (*g as f32)
526-
+ 0.114 * (*b as f32)) as u8;
527-
// Pack grayscale value into RGB format
528-
let packed = (gray as u32) << 16 | (gray as u32) << 8 | (gray as u32);
529-
features.push(packed as f32);
530-
}
499+
// For RGB images, combine channels into 24-bit RGB values
500+
for chunk in frame_data.chunks_exact(3) {
501+
if let [r, g, b] = chunk {
502+
// Create 24-bit RGB value: 0xRRGGBB
503+
let feature = ((*r as u32) << 16) | ((*g as u32) << 8) | (*b as u32);
504+
features.push(feature as f32);
531505
}
532506
}
533-
features
534-
};
507+
}
535508

536509
// Run inference
537510
let start = std::time::Instant::now();
@@ -625,7 +598,7 @@ impl BaseTransformImpl for EdgeImpulseVideoInfer {
625598
y: y as u32,
626599
width: width as u32,
627600
height: height as u32,
628-
label: format!("{:.3}", value),
601+
label: format!("{value:.3}"),
629602
});
630603
}
631604
}

0 commit comments

Comments
 (0)