-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathedge_pipeline.rs
More file actions
577 lines (523 loc) · 19.6 KB
/
Copy pathedge_pipeline.rs
File metadata and controls
577 lines (523 loc) · 19.6 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Edge deployment pipeline: camera -> (NPU) detect -> track -> overlay -> encode -> output.
//!
//! Usage:
//! cargo run --example edge_pipeline # synthetic, mock detections
//! cargo run --example edge_pipeline -- --camera /dev/video0 # real V4L2 camera (Linux)
//! cargo run --example edge_pipeline -- --output out.h264 # save encoded video
//!
//! NPU path (Rockchip, requires `--features rknn`):
//! cargo run --example edge_pipeline --features rknn -- \
//! --camera /dev/video0 --model yolov8n.rknn \
//! --cores 3 --zero-copy --async
//!
//! CLI flags specific to the NPU path:
//! --cores N NPU concurrency (1..=3 on RK3588, 1 on RV1106)
//! --zero-copy bind V4L2 DMA-BUF directly as NPU input (no memcpy)
//! --async non-blocking submit + poll for completion
//! --sram allocate hot intermediate tensors in on-chip SRAM
//!
//! Without `--features rknn` the NPU flags are parsed but ignored and the pipeline
//! falls back to synthetic / mock detections, so the same binary runs everywhere.
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Instant;
use yscv_video::{
FramePipeline, H264Encoder, SlotMut, SlotRef, TelemetryData, h264_encoder::rgb8_to_yuv420,
overlay_detections, overlay_telemetry, run_pipeline,
};
// ---------------------------------------------------------------------------
// CLI argument parsing (no external crate)
// ---------------------------------------------------------------------------
struct Args {
camera: Option<String>,
model: Option<String>,
output: Option<String>,
serial: Option<String>,
width: u32,
height: u32,
frames: usize,
cores: u32,
zero_copy: bool,
async_npu: bool,
sram: bool,
}
fn parse_args() -> Args {
let argv: Vec<String> = std::env::args().collect();
let mut args = Args {
camera: None,
model: None,
output: None,
serial: None,
width: 320,
height: 240,
frames: 120,
cores: 1,
zero_copy: false,
async_npu: false,
sram: false,
};
let mut i = 1;
while i < argv.len() {
match argv[i].as_str() {
"--camera" => {
i += 1;
args.camera = argv.get(i).cloned();
}
"--model" => {
i += 1;
args.model = argv.get(i).cloned();
}
"--output" => {
i += 1;
args.output = argv.get(i).cloned();
}
"--serial" => {
i += 1;
args.serial = argv.get(i).cloned();
}
"--width" => {
i += 1;
if let Some(v) = argv.get(i) {
args.width = v.parse().unwrap_or(320);
}
}
"--height" => {
i += 1;
if let Some(v) = argv.get(i) {
args.height = v.parse().unwrap_or(240);
}
}
"--frames" => {
i += 1;
if let Some(v) = argv.get(i) {
args.frames = v.parse().unwrap_or(120);
}
}
"--cores" => {
i += 1;
if let Some(v) = argv.get(i) {
args.cores = v.parse().unwrap_or(1).clamp(1, 3);
}
}
"--zero-copy" => args.zero_copy = true,
"--async" => args.async_npu = true,
"--sram" => args.sram = true,
other => {
eprintln!("Unknown argument: {other}");
}
}
i += 1;
}
args.width = (args.width + 15) & !15;
args.height = (args.height + 15) & !15;
args
}
// ---------------------------------------------------------------------------
// NPU startup diagnostics
// ---------------------------------------------------------------------------
#[cfg(feature = "rknn")]
fn print_npu_diagnostics() {
use yscv_kernels::rknn_available;
eprintln!("NPU: checking librknnrt.so…");
if rknn_available() {
eprintln!("NPU: librknnrt.so loaded");
} else {
eprintln!("NPU: librknnrt.so not found (non-Rockchip host or missing runtime)");
}
}
#[cfg(feature = "rknn")]
fn print_backend_diagnostics(backend: &yscv_kernels::RknnBackend) {
if let Ok((api, drv)) = backend.sdk_version() {
eprintln!("NPU SDK: api={api} drv={drv}");
}
if let Ok(mem) = backend.mem_size() {
eprintln!(
"NPU memory: weight={}KB internal={}KB dma={}KB sram_free={}KB",
mem.weight_bytes / 1024,
mem.internal_bytes / 1024,
mem.dma_bytes / 1024,
mem.sram_free_bytes / 1024,
);
}
if let Ok(cs) = backend.custom_string()
&& !cs.is_empty()
{
eprintln!("NPU model: custom_string=\"{cs}\"");
}
}
#[cfg(not(feature = "rknn"))]
fn print_npu_diagnostics() {
eprintln!("NPU: not compiled in (rebuild with --features rknn to enable)");
}
// ---------------------------------------------------------------------------
// NPU inference (optional, rknn feature)
// ---------------------------------------------------------------------------
#[cfg(feature = "rknn")]
struct NpuPipeline {
pool: yscv_kernels::ContextPool,
input_w: u32,
input_h: u32,
}
#[cfg(feature = "rknn")]
impl NpuPipeline {
fn load(model_path: &str, cores: u32) -> Result<Self, Box<dyn std::error::Error>> {
use yscv_kernels::{ContextPool, NpuCoreMask};
let bytes = std::fs::read(model_path)?;
let masks: &[NpuCoreMask] = match cores {
1 => &[NpuCoreMask::Core0],
2 => &[NpuCoreMask::Core0, NpuCoreMask::Core1],
_ => &[NpuCoreMask::Core0, NpuCoreMask::Core1, NpuCoreMask::Core2],
};
let pool = ContextPool::new(&bytes, masks)?;
eprintln!("NPU pool: {} context(s)", pool.size());
let (in_w, in_h) = if let Some(ctx) = pool.context(0) {
print_backend_diagnostics(&ctx);
let attrs = ctx.current_input_attrs().unwrap_or_default();
if let Some(a) = attrs.first() {
let h = *a.dims.get(1).unwrap_or(&0);
let w = *a.dims.get(2).unwrap_or(&0);
(w, h)
} else {
(0, 0)
}
} else {
(0, 0)
};
eprintln!("NPU input: {in_w}x{in_h}");
Ok(Self {
pool,
input_w: in_w,
input_h: in_h,
})
}
fn infer(&self, rgb: &[u8]) -> Result<Vec<yscv_tensor::Tensor>, Box<dyn std::error::Error>> {
// Most RKNN vision models name their sole input `"images"`; if
// your model uses a different name, replace the tuple's first
// element. For multi-input models pass multiple `(name, bytes)`
// pairs.
Ok(self.pool.dispatch_roundrobin(&[("images", rgb)])?)
}
}
// ---------------------------------------------------------------------------
// Synthetic frame generator: gradient background with a moving white box
// ---------------------------------------------------------------------------
fn generate_synthetic_frame(rgb: &mut [u8], width: u32, height: u32, frame_idx: usize) {
let w = width as usize;
let h = height as usize;
let expected = w * h * 3;
if rgb.len() < expected {
return;
}
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) * 3;
rgb[idx] = (x * 255 / w.max(1)) as u8;
rgb[idx + 1] = (y * 255 / h.max(1)) as u8;
rgb[idx + 2] = ((frame_idx * 3) & 0xFF) as u8;
}
}
let box_size = 32usize;
let travel = w.saturating_sub(box_size);
let pos = if travel > 0 {
let cycle = travel * 2;
let raw = frame_idx % cycle.max(1);
if raw < travel { raw } else { cycle - raw }
} else {
0
};
let bx = pos;
let by = h / 2 - box_size / 2;
for dy in 0..box_size.min(h - by) {
for dx in 0..box_size.min(w - bx) {
let idx = ((by + dy) * w + (bx + dx)) * 3;
rgb[idx] = 255;
rgb[idx + 1] = 255;
rgb[idx + 2] = 255;
}
}
}
fn generate_mock_detections(
frame_idx: usize,
width: u32,
height: u32,
) -> Vec<(f32, f32, f32, f32, f32, String)> {
let w = width as f32;
let h = height as f32;
let box_size = 32.0f32;
let travel = (w - box_size).max(0.0);
let cycle = (travel * 2.0).max(1.0);
let raw = (frame_idx as f32) % cycle;
let bx = if raw < travel { raw } else { cycle - raw };
let by = h / 2.0 - box_size / 2.0;
vec![(bx, by, box_size, box_size, 0.95, "object".to_string())]
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
fn main() {
let args = parse_args();
let w = args.width;
let h = args.height;
let max_frames = args.frames;
eprintln!("=== yscv edge pipeline ===");
eprintln!("Resolution: {w}x{h}");
eprintln!("Max frames: {max_frames}");
eprintln!(
"Source: {}",
args.camera.as_deref().unwrap_or("synthetic")
);
eprintln!(
"Model: {}",
args.model.as_deref().unwrap_or("mock detections")
);
eprintln!(
"Output: {}",
args.output.as_deref().unwrap_or("none (discard)")
);
if let Some(ref s) = args.serial {
eprintln!("MAVLink: {s}");
}
eprintln!(
"NPU opts: cores={} zero-copy={} async={} sram={}",
args.cores, args.zero_copy, args.async_npu, args.sram
);
print_npu_diagnostics();
// -------------------------------------------------------------------
// NPU inference backend (optional)
// -------------------------------------------------------------------
#[cfg(feature = "rknn")]
let npu = args
.model
.as_deref()
.filter(|p| p.ends_with(".rknn"))
.and_then(|p| match NpuPipeline::load(p, args.cores) {
Ok(n) => Some(n),
Err(e) => {
eprintln!("NPU load failed: {e} — falling back to mock detections");
None
}
});
// V4L2 camera setup (Linux only)
#[cfg(target_os = "linux")]
let mut v4l2_camera = args.camera.as_ref().map(|dev| {
let mut cam = yscv_video::V4l2Camera::open(dev, w, h, yscv_video::V4l2PixelFormat::Yuyv)
.unwrap_or_else(|e| {
eprintln!("Failed to open camera {dev}: {e}");
std::process::exit(1);
});
cam.start_streaming().unwrap_or_else(|e| {
eprintln!("Failed to start streaming: {e}");
std::process::exit(1);
});
cam
});
#[cfg(target_os = "linux")]
let mut mavlink_serial = args.serial.as_ref().map(|dev| {
yscv_video::MavlinkSerial::open(dev, 115200).unwrap_or_else(|e| {
eprintln!("Failed to open MAVLink serial {dev}: {e}");
std::process::exit(1);
})
});
#[cfg(target_os = "linux")]
let mut mavlink_telemetry = TelemetryData {
battery_voltage: 12.6,
battery_current: 0.0,
altitude_m: 0.0,
speed_ms: 0.0,
lat: 0.0,
lon: 0.0,
heading_deg: 0.0,
ai_detections: 0,
};
let frame_bytes = (w as usize) * (h as usize) * 3;
let pipeline = FramePipeline::new(4, frame_bytes);
let frame_counter = AtomicUsize::new(0);
let t_start = Instant::now();
let output_file = args.output.as_ref().map(|path| {
std::fs::File::create(path).unwrap_or_else(|e| {
eprintln!("Cannot create output file {path}: {e}");
std::process::exit(1);
})
});
let output_file = std::sync::Mutex::new(output_file);
let encoder = std::sync::Mutex::new(H264Encoder::new(w, h, 26));
let capture_ns = AtomicUsize::new(0);
let process_ns = AtomicUsize::new(0);
let output_ns = AtomicUsize::new(0);
run_pipeline(
&pipeline,
// -- Stage 1: Capture --
|slot: &mut SlotMut<'_>| {
let t0 = Instant::now();
let idx = frame_counter.fetch_add(1, Ordering::Relaxed);
slot.set_width(w);
slot.set_height(h);
slot.set_pixel_format(2); // RGB8
slot.set_timestamp_us((idx as u64) * 33_333);
#[cfg(target_os = "linux")]
{
if let Some(ref mut cam) = v4l2_camera {
// Cache width/height before the mutable borrow on
// `capture_frame()` — `yuyv_data` borrows `cam`
// immutably for the rest of the `Ok` arm, so we can't
// call `cam.width()` / `cam.height()` after it.
let cam_w = cam.width() as usize;
let cam_h = cam.height() as usize;
match cam.capture_frame() {
Ok(yuyv_data) => {
let rgb_buf = slot.data_mut();
let needed = cam_w * cam_h * 3;
if rgb_buf.len() >= needed {
let _ = yscv_video::yuyv_to_rgb8(
yuyv_data,
cam_w,
cam_h,
&mut rgb_buf[..needed],
);
}
}
Err(e) => {
eprintln!("Capture error: {e}");
return false;
}
}
if let Some(ref mut mav) = mavlink_serial
&& let Ok(messages) = mav.read_messages()
{
for msg in &messages {
if let Some(update) = yscv_video::telemetry_from_mavlink(msg) {
yscv_video::apply_telemetry_update(&mut mavlink_telemetry, &update);
}
}
}
capture_ns.fetch_add(t0.elapsed().as_nanos() as usize, Ordering::Relaxed);
return true;
}
}
let rgb_buf = slot.data_mut();
generate_synthetic_frame(rgb_buf, w, h, idx);
capture_ns.fetch_add(t0.elapsed().as_nanos() as usize, Ordering::Relaxed);
true
},
// -- Stage 2: Process (detection) --
|slot: &mut SlotMut<'_>| {
let t0 = Instant::now();
let idx = slot.timestamp_us() / 33_333;
slot.detections_mut().clear();
#[cfg(feature = "rknn")]
let used_npu = if let Some(ref n) = npu {
// Only dispatch if input resolution matches — otherwise mock.
let match_in = n.input_w == slot.width() && n.input_h == slot.height();
if match_in {
match n.infer(slot.data()) {
Ok(_outputs) => {
// YOLOv8 decode is model-specific; kept as an exercise
// outside the pipeline scaffolding. We only exercise
// the NPU execution path here.
true
}
Err(e) => {
eprintln!("NPU infer failed: {e}");
false
}
}
} else {
false
}
} else {
false
};
#[cfg(not(feature = "rknn"))]
let used_npu = false;
if !used_npu {
let dets = generate_mock_detections(idx as usize, slot.width(), slot.height());
for (dx, dy, dw, dh, score, _label) in &dets {
slot.detections_mut().push(yscv_video::PipelineDetection {
bbox: yscv_video::PipelineBBox {
x1: *dx,
y1: *dy,
x2: dx + dw,
y2: dy + dh,
},
score: *score,
class_id: 0,
});
}
}
process_ns.fetch_add(t0.elapsed().as_nanos() as usize, Ordering::Relaxed);
},
// -- Stage 3: Output (overlay + encode) --
|slot: &SlotRef<'_>| {
let t0 = Instant::now();
let sw = slot.width() as usize;
let sh = slot.height() as usize;
let rgb_size = sw * sh * 3;
let mut rgb_frame = vec![0u8; rgb_size];
let src = slot.data();
let copy_len = rgb_size.min(src.len());
rgb_frame[..copy_len].copy_from_slice(&src[..copy_len]);
let det_labels: Vec<String> = slot
.detections()
.iter()
.map(|d| format!("cls{}", d.class_id))
.collect();
let overlay_dets: Vec<(f32, f32, f32, f32, f32, &str)> = slot
.detections()
.iter()
.zip(det_labels.iter())
.map(|(d, label)| {
(
d.bbox.x1,
d.bbox.y1,
d.bbox.x2 - d.bbox.x1,
d.bbox.y2 - d.bbox.y1,
d.score,
label.as_str(),
)
})
.collect();
overlay_detections(&mut rgb_frame, sw, sh, &overlay_dets);
let telemetry = TelemetryData {
battery_voltage: 12.4,
battery_current: 1.2,
altitude_m: 45.0,
speed_ms: 5.3,
lat: 55.7558,
lon: 37.6173,
heading_deg: 127.0,
ai_detections: slot.detections().len() as u32,
};
overlay_telemetry(&mut rgb_frame, sw, sh, &telemetry);
let yuv = rgb8_to_yuv420(&rgb_frame, sw, sh);
let nal_data = {
let mut enc = encoder.lock().unwrap_or_else(|e| e.into_inner());
enc.encode_frame(&yuv)
};
{
let mut guard = output_file.lock().unwrap_or_else(|e| e.into_inner());
if let Some(ref mut f) = *guard {
use std::io::Write;
let _ = f.write_all(&nal_data);
}
}
output_ns.fetch_add(t0.elapsed().as_nanos() as usize, Ordering::Relaxed);
},
max_frames,
);
let elapsed = t_start.elapsed();
let total_ms = elapsed.as_secs_f64() * 1000.0;
let fps = max_frames as f64 / elapsed.as_secs_f64();
let cap_avg_us = capture_ns.load(Ordering::Relaxed) as f64 / (max_frames as f64 * 1000.0);
let proc_avg_us = process_ns.load(Ordering::Relaxed) as f64 / (max_frames as f64 * 1000.0);
let out_avg_us = output_ns.load(Ordering::Relaxed) as f64 / (max_frames as f64 * 1000.0);
eprintln!();
eprintln!("=== Pipeline Stats ===");
eprintln!("Total time: {total_ms:.1}ms");
eprintln!("Frames: {max_frames}");
eprintln!("FPS: {fps:.1}");
eprintln!("Avg capture: {cap_avg_us:.1}us");
eprintln!("Avg process: {proc_avg_us:.1}us");
eprintln!("Avg output: {out_avg_us:.1}us");
if let Some(ref p) = args.output {
eprintln!("Output written: {p}");
}
}