-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconfig.rs
More file actions
45 lines (38 loc) · 1.55 KB
/
Copy pathconfig.rs
File metadata and controls
45 lines (38 loc) · 1.55 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
// Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
//! Configure inference with `InferenceConfig` before loading the model.
//!
//! ```bash
//! cargo run --example config
//! cargo run --example config -- path/to/image.jpg
//! ```
use ultralytics_inference::{Device, InferenceConfig, YOLOModel};
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build a configuration with the builder methods, then load the model with it.
let config = InferenceConfig::new()
.with_confidence(0.5) // keep detections at or above 0.5 confidence
.with_iou(0.45) // NMS IoU threshold
.with_imgsz(640, 640) // inference image size
.with_device(Device::Cpu); // run on CPU
let mut model = YOLOModel::load_with_config("yolo26n.onnx", config)?;
let results = match std::env::args().nth(1) {
Some(path) => model.predict(path)?,
None => model.predict_default()?,
};
for result in &results {
let Some(boxes) = &result.boxes else { continue };
println!("Found {} detections", boxes.len());
let xyxy = boxes.xyxy();
for i in 0..boxes.len() {
let cls = boxes.cls()[i] as usize;
let conf = boxes.conf()[i];
let name = result.names.get(&cls).map_or("unknown", String::as_str);
let b = xyxy.row(i);
println!(
" {name} {conf:.2} [{:.1} {:.1} {:.1} {:.1}]",
b[0], b[1], b[2], b[3]
);
}
}
Ok(())
}