### CLI Inference Examples Source: https://context7.com/ultralytics/inference/llms.txt Run inference from the command line using the `ultralytics-inference` tool. This section provides examples for installing the CLI, running with default settings, specifying models and sources (images, directories, webcam, video streams), and adjusting confidence and IoU thresholds. ```bash # Install the CLI tool cargo install --path . # Run with defaults (auto-downloads model and sample images) ultralytics-inference predict # Run on specific image with custom model ultralytics-inference predict --model yolo11n.onnx --source image.jpg # Run on directory of images ultralytics-inference predict -m yolo11n.onnx -s images/ # Run with custom thresholds ultralytics-inference predict \ --model yolo11n.onnx \ --source image.jpg \ --conf 0.5 \ --iou 0.45 # Run on video with visualization ultralytics-inference predict \ --model yolo11n.onnx \ --source video.mp4 \ --show # Run on webcam ultralytics-inference predict \ --model yolo11n.onnx \ --source 0 \ --show ``` -------------------------------- ### Preprocess Images Manually with Custom Parameters Source: https://context7.com/ultralytics/inference/llms.txt Use preprocessing utilities for custom inference pipelines by manually preparing images. This example demonstrates loading an image, applying preprocessing with custom target size, stride, and precision (FP16), and accessing the resulting tensor and original image metadata. It utilizes the `image` crate for image loading. ```rust use ultralytics_inference::preprocessing::{ preprocess_image_with_precision, PreprocessResult }; use image::DynamicImage; fn main() -> Result<(), Box> { // Load image let image = image::open("image.jpg")?; // Preprocess with custom parameters let target_size = (640, 640); // (height, width) let stride = 32; // Model stride let use_fp16 = false; // FP16 precision let preprocess_result: PreprocessResult = preprocess_image_with_precision( &image, target_size, stride, use_fp16 ); // Access preprocessed tensor println!("Tensor shape: {:?}", preprocess_result.tensor.shape()); println!("Original shape: {}x{}", preprocess_result.orig_shape.0, preprocess_result.orig_shape.1); println!("Scale factors: ({:.3}, {:.3})", preprocess_result.scale.0, preprocess_result.scale.1); println!("Padding: ({:.1}, {:.1})", preprocess_result.padding.0, preprocess_result.padding.1); // Use FP16 tensor if available if let Some(ref tensor_f16) = preprocess_result.tensor_f16 { println!("FP16 tensor shape: {:?}", tensor_f16.shape()); } Ok(()) } ``` -------------------------------- ### Process Video Files and Streams Source: https://context7.com/ultralytics/inference/llms.txt Process video files frame by frame using the 'video' feature. This example shows how to load a model, create a source from a video file or stream, iterate through frames, perform inference, and display processing information. Requires the 'video' feature to be enabled during build. ```rust #[cfg(feature = "video")] use ultralytics_inference::{YOLOModel, Source, SourceIterator}; #[cfg(feature = "video")] fn main() -> Result<(), Box> { let mut model = YOLOModel::load("yolo11n.onnx")?; // Create source from video file let source = Source::from("video.mp4"); // Or use webcam: let source = Source::from(0); // Or use RTSP stream: let source = Source::from("rtsp://...); let source_iter = SourceIterator::new(source)?; let mut frame_count = 0; let mut total_inference_time = 0.0; // Process each frame for frame_result in source_iter { let (frame, meta) = frame_result?; // Run inference let results = model.predict_image(&frame, meta.path.clone())?; for result in &results { total_inference_time += result.speed.total(); frame_count += 1; if let Some(ref boxes) = result.boxes { if !boxes.is_empty() { println!("Frame {}: {} detections", meta.frame_idx, boxes.len()); } } } // Process at video FPS rate if let Some(fps) = meta.fps { let frame_time = 1000.0 / fps; println!("FPS: {:.1}, Frame time: {:.1}ms", fps, frame_time); } } println!("\nProcessed {} frames", frame_count); println!("Average inference time: {:.1}ms/frame", total_inference_time / frame_count as f64); Ok(()) } #[cfg(not(feature = "video"))] fn main() { println!("Video support requires 'video' feature"); println!("Build with: cargo build --features video"); } ``` -------------------------------- ### Process Multiple Images from Directory in Rust Source: https://context7.com/ultralytics/inference/llms.txt Demonstrates how to process all images within a specified directory or that match a glob pattern. It utilizes `Source` and `SourceIterator` to iterate over image files, loading each one and running inference. The example shows how to access image metadata including the file path. Requires a directory named 'images/' or matching glob pattern and 'yolo11n.onnx'. ```rust use ultralytics_inference::{YOLOModel, Source, SourceIterator}; fn main() -> Result<(), Box> { let mut model = YOLOModel::load("yolo11n.onnx")?; // Create source from directory let source = Source::from("images/"); // Or use glob pattern // let source = Source::from("images/*.jpg"); // Create iterator over images let source_iter = SourceIterator::new(source)?; // Process each image for (idx, frame_result) in source_iter.enumerate() { let (image, meta) = frame_result?; println!("\nProcessing image {}/{}: {}", meta.frame_idx + 1, meta.total_frames.unwrap_or(0), meta.path); // Run inference on the image let results = model.predict_image(&image, meta.path.clone())?; for result in &results { if let Some(ref boxes) = result.boxes { println!(" Found {} detections", boxes.len()); } } } Ok(()) } ``` -------------------------------- ### Access YOLO Model Metadata Source: https://context7.com/ultralytics/inference/llms.txt Query model properties and metadata extracted from ONNX files. This example shows how to load a YOLO model and access its task type, class names, input image dimensions, stride, FP16 usage status, execution provider, and full metadata. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { let model = YOLOModel::load("yolo11n.onnx")?; // Get model task type println!("Task: {:?}", model.task()); // Get class names let names = model.names(); println!("Number of classes: {}", model.num_classes()); for (id, name) in names.iter() { println!(" Class {}: {}", id, name); } // Get input size let (height, width) = model.imgsz(); println!("Input size: {}x{}", height, width); // Get model stride println!("Stride: {}", model.stride()); // Check if using FP16 println!("Using FP16: {}", model.is_half()); // Get execution provider println!("Execution provider: {}", model.execution_provider()); // Access full metadata let metadata = model.metadata(); println!("Model metadata: {:?}", metadata); Ok(()) } ``` -------------------------------- ### Pose Estimation with Keypoints (Rust) Source: https://context7.com/ultralytics/inference/llms.txt Run pose estimation inference and access the detected keypoints. This involves loading a pose estimation model and processing the results to get keypoint coordinates. Confidence scores for keypoints may also be available. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { // Load pose estimation model (e.g., yolo11n-pose.onnx) let mut model = YOLOModel::load("yolo11n-pose.onnx")?; let results = model.predict("image.jpg")?; for result in &results { // Access pose keypoints if let Some(ref keypoints) = result.keypoints { println!("Found {} poses", keypoints.len()); // Get keypoint coordinates let xy = keypoints.xy(); // (N, K, 2) - absolute coordinates let xyn = keypoints.xyn(); // (N, K, 2) - normalized [0-1] // Get keypoint confidence scores (if available) if let Some(conf) = keypoints.conf() { println!("Keypoint confidence shape: {:?}", conf.shape()); } // Access individual poses let shape = xy.shape(); let num_poses = shape[0]; let num_keypoints = shape[1]; for i in 0..num_poses { println!("Pose {}: {} keypoints", i, num_keypoints); for k in 0..num_keypoints { let x = xy[[i, k, 0]]; let y = xy[[i, k, 1]]; println!(" Keypoint {}: ({:.1}, {:.1})", k, x, y); } } } } Ok(()) } ``` -------------------------------- ### Run YOLO Inference with Cargo CLI Source: https://github.com/ultralytics/inference/blob/main/README.md Executes YOLO model inference using the Rust CLI built with Cargo. The examples show running with default settings, specifying the model and source, processing directories, and customizing confidence and IoU thresholds. ```bash # With defaults cargo run --release -- predict # With explicit arguments cargo run --release -- predict --model yolo11n.onnx --source image.jpg # On a directory of images cargo run --release -- predict --model yolo11n.onnx --source assets/ # With custom thresholds cargo run --release -- predict -m yolo11n.onnx -s image.jpg --conf 0.5 --iou 0.45 # With visualization and custom image size cargo run --release -- predict --model yolo11n.onnx --source video.mp4 --show --imgsz 1280 ``` -------------------------------- ### Load and Run Basic Inference Source: https://context7.com/ultralytics/inference/llms.txt Demonstrates how to load a YOLO ONNX model and perform inference on an image file. It also shows how to process the detection results, including class names, confidence scores, and bounding box coordinates. ```APIDOC ## Load and Run Basic Inference ### Description Load a YOLO ONNX model and run inference on an image file. Process detection results including class names, confidence, and bounding boxes. ### Method `YOLOModel::load` and `YOLOModel::predict` ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { // Load model - metadata is read automatically from ONNX let mut model = YOLOModel::load("yolo11n.onnx")?; // Run inference on an image let results = model.predict("image.jpg")?; // Process detection results for result in &results { if let Some(ref boxes) = result.boxes { println!("Found {} detections", boxes.len()); for i in 0..boxes.len() { let cls = boxes.cls()[i] as usize; let conf = boxes.conf()[i]; let xyxy = boxes.xyxy(); let name = result.names.get(&cls) .map(|s| s.as_str()) .unwrap_or("unknown"); println!(" {} {:.2} at [{:.1}, {:.1}, {:.1}, {:.1}]", name, conf, xyxy[[i, 0]], xyxy[[i, 1]], xyxy[[i, 2]], xyxy[[i, 3]]); } } } Ok(()) } ``` ### Response #### Success Response (200) `Vec`: A vector of results, where each result contains detected objects, their bounding boxes, masks, keypoints, etc. #### Response Example ```json [ { "boxes": { "xyxy": [[100.0, 150.0, 200.0, 250.0]], "conf": [0.95], "cls": [0] }, "names": { "0": "person" }, "speed": { "preprocess": 1.5, "inference": 10.2, "postprocess": 0.8, "total": 12.5 } } ] ``` ``` -------------------------------- ### Hardware Acceleration with Device Selection Source: https://context7.com/ultralytics/inference/llms.txt Demonstrates how to select specific hardware devices for inference, including CPU, CUDA (NVIDIA GPU), CoreML (Apple Silicon), and TensorRT, and how to enable half-precision (FP16) inference on CPU. ```APIDOC ## Hardware Acceleration with Device Selection ### Description Select specific hardware devices (CPU, CUDA, CoreML, TensorRT) for inference and configure options like half-precision (FP16) for performance optimization. ### Method `InferenceConfig::new`, `InferenceConfig::with_device`, `InferenceConfig::with_half`, `YOLOModel::load_with_config`, `YOLOModel::predict`, `YOLOModel::execution_provider` ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use ultralytics_inference::{YOLOModel, InferenceConfig, Device}; fn main() -> Result<(), Box> { // Configure for NVIDIA GPU (device 0) let cuda_config = InferenceConfig::new() .with_device(Device::Cuda(0)) .with_confidence(0.5); let mut cuda_model = YOLOModel::load_with_config( "yolo11n.onnx", cuda_config )?; // Configure for Apple Silicon (MPS/CoreML) let coreml_config = InferenceConfig::new() .with_device(Device::CoreMl) .with_confidence(0.5); let mut coreml_model = YOLOModel::load_with_config( "yolo11n.onnx", coreml_config )?; // Configure for CPU with FP16 inference let cpu_config = InferenceConfig::new() .with_device(Device::Cpu) .with_half(true) // Enable FP16 (half-precision) .with_confidence(0.5); let mut cpu_model = YOLOModel::load_with_config( "yolo11n.onnx", cpu_config )?; // Run inference let results = cuda_model.predict("image.jpg")?; // Check which execution provider is being used println!("Using: {}", cuda_model.execution_provider()); Ok(()) } ``` ### Response #### Success Response (200) `Vec`: A vector of results, indicating successful inference on the selected hardware. #### Response Example ```json [ { "boxes": { "xyxy": [[100.0, 150.0, 200.0, 250.0]], "conf": [0.95], "cls": [0] }, "names": { "0": "person" }, "speed": { "preprocess": 1.5, "inference": 5.2, // Faster inference on GPU "postprocess": 0.8, "total": 7.5 } } ] ``` ``` -------------------------------- ### Run All Tests with Cargo Source: https://github.com/ultralytics/inference/blob/main/tests/README.md Executes all integration tests for the Ultralytics template crate using the standard Cargo test command. Ensure you are in the project root directory before running. ```bash cargo test ``` -------------------------------- ### Cargo CLI Help and Version Source: https://github.com/ultralytics/inference/blob/main/README.md Displays the help information and version of the Cargo-built Ultralytics inference CLI tool. These commands are useful for understanding available options and checking the tool's version. ```bash # Show help cargo run --release -- help # Show version cargo run --release -- version ``` -------------------------------- ### Rust: Configure Custom Inference Parameters for YOLO Source: https://context7.com/ultralytics/inference/llms.txt Demonstrates how to load a YOLO ONNX model with a custom inference configuration. This allows for fine-tuning parameters such as confidence threshold, IoU threshold, maximum detections, input image size, and the number of threads used for inference. It also shows how to access and print timing information for preprocessing, inference, and postprocessing. ```rust use ultralytics_inference::{YOLOModel, InferenceConfig}; fn main() -> Result<(), Box> { // Create custom configuration let config = InferenceConfig::new() .with_confidence(0.5) // Confidence threshold (0.0-1.0) .with_iou(0.45) // NMS IoU threshold .with_max_detections(100) // Maximum detections per image .with_imgsz(640, 640) // Input image size (height, width) .with_threads(8); // Number of threads (0 = auto) // Load model with custom config let mut model = YOLOModel::load_with_config("yolo11n.onnx", config)?; // Run inference let results = model.predict("image.jpg")?; // Access timing information for result in &results { println!("Preprocessing: {:.1}ms", result.speed.preprocess.unwrap_or(0.0)); println!("Inference: {:.1}ms", result.speed.inference.unwrap_or(0.0)); println!("Postprocessing: {:.1}ms", result.speed.postprocess.unwrap_or(0.0)); println!("Total: {:.1}ms", result.speed.total()); } Ok(()) } ``` -------------------------------- ### Rust: Select Hardware Device for YOLO Inference Source: https://context7.com/ultralytics/inference/llms.txt Illustrates how to configure the YOLO model to use specific hardware devices for inference, including CPU, NVIDIA GPU (CUDA), and Apple Silicon (CoreML). It also shows how to enable FP16 (half-precision) inference on the CPU and how to check the execution provider being used by the model. ```rust use ultralytics_inference::{YOLOModel, InferenceConfig, Device}; fn main() -> Result<(), Box> { // Configure for NVIDIA GPU (device 0) let cuda_config = InferenceConfig::new() .with_device(Device::Cuda(0)) .with_confidence(0.5); let mut cuda_model = YOLOModel::load_with_config( "yolo11n.onnx", cuda_config )?; // Configure for Apple Silicon (MPS/CoreML) let coreml_config = InferenceConfig::new() .with_device(Device::CoreMl) .with_confidence(0.5); let mut coreml_model = YOLOModel::load_with_config( "yolo11n.onnx", coreml_config )?; // Configure for CPU with FP16 inference let cpu_config = InferenceConfig::new() .with_device(Device::Cpu) .with_half(true) // Enable FP16 (half-precision) .with_confidence(0.5); let mut cpu_model = YOLOModel::load_with_config( "yolo11n.onnx", cpu_config )?; // Run inference let results = cuda_model.predict("image.jpg")?; // Check which execution provider is being used println!("Using: {}", cuda_model.execution_provider()); Ok(()) } ``` -------------------------------- ### Ultralytics Rust Library with Custom Configuration Source: https://github.com/ultralytics/inference/blob/main/README.md Demonstrates how to use the Ultralytics Rust library with custom inference configurations, such as setting confidence thresholds and maximum detection limits. This allows fine-tuning the prediction process. ```rust use ultralytics_inference::{YOLOModel, InferenceConfig}; fn main() -> Result<(), Box> { let config = InferenceConfig::new() .with_confidence(0.5) .with_iou(0.45) .with_max_detections(100); let mut model = YOLOModel::load_with_config("yolo11n.onnx", config)?; let results = model.predict("image.jpg")?; Ok(()) } ``` -------------------------------- ### Build with Hardware Acceleration (Bash) Source: https://github.com/ultralytics/inference/blob/main/README.md Enables hardware acceleration for the Rust project by specifying Cargo features during the build process. This allows for optimized inference on various hardware platforms. ```bash # NVIDIA GPU (CUDA) cargo build --release --features cuda # NVIDIA TensorRT cargo build --release --features tensorrt # Apple CoreML (macOS/iOS) cargo build --release --features coreml # Intel OpenVINO cargo build --release --features openvino # Multiple features cargo build --release --features "cuda,tensorrt" ``` -------------------------------- ### Run Tests (Bash) Source: https://github.com/ultralytics/inference/blob/main/README.md Commands for running tests within the Rust project. Supports running all tests, running tests with captured output, and running specific test functions. ```bash # Run all tests cargo test # Run with output cargo test -- --nocapture # Run specific test cargo test test_boxes_creation ``` -------------------------------- ### Custom Inference Configuration Source: https://context7.com/ultralytics/inference/llms.txt Shows how to configure inference parameters such as confidence threshold, IoU threshold, maximum detections, input image size, and the number of threads. ```APIDOC ## Custom Inference Configuration ### Description Configure inference parameters including confidence threshold, IoU threshold, and image size for more precise control over the detection process. ### Method `InferenceConfig::new`, `InferenceConfig::with_confidence`, `InferenceConfig::with_iou`, `InferenceConfig::with_max_detections`, `InferenceConfig::with_imgsz`, `InferenceConfig::with_threads`, `YOLOModel::load_with_config`, `YOLOModel::predict` ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use ultralytics_inference::{YOLOModel, InferenceConfig}; fn main() -> Result<(), Box> { // Create custom configuration let config = InferenceConfig::new() .with_confidence(0.5) // Confidence threshold (0.0-1.0) .with_iou(0.45) // NMS IoU threshold .with_max_detections(100) // Maximum detections per image .with_imgsz(640, 640) // Input image size (height, width) .with_threads(8); // Number of threads (0 = auto) // Load model with custom config let mut model = YOLOModel::load_with_config("yolo11n.onnx", config)?; // Run inference let results = model.predict("image.jpg")?; // Access timing information for result in &results { println!("Preprocessing: {:.1}ms", result.speed.preprocess.unwrap_or(0.0)); println!("Inference: {:.1}ms", result.speed.inference.unwrap_or(0.0)); println!("Postprocessing: {:.1}ms", result.speed.postprocess.unwrap_or(0.0)); println!("Total: {:.1}ms", result.speed.total()); } Ok(()) } ``` ### Response #### Success Response (200) `Vec`: A vector of results, containing detected objects and timing information for preprocessing, inference, and postprocessing. #### Response Example ```json [ { "boxes": { "xyxy": [[100.0, 150.0, 200.0, 250.0]], "conf": [0.95], "cls": [0] }, "names": { "0": "person" }, "speed": { "preprocess": 1.5, "inference": 10.2, "postprocess": 0.8, "total": 12.5 } } ] ``` ``` -------------------------------- ### Run Inference with CLI Source: https://github.com/ultralytics/inference/blob/main/README.md Execute object detection inference using the command-line interface. Requires specifying the model and source, with numerous optional parameters for controlling confidence, device, output, and more. ```bash cargo run --release -- predict --model --source ``` -------------------------------- ### Rust: Load and Run Basic YOLO Inference Source: https://context7.com/ultralytics/inference/llms.txt Loads a YOLO ONNX model and performs basic inference on a given image file. It automatically extracts model metadata and processes the detection results, printing the number of detections and details for each. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { // Load model - metadata is read automatically from ONNX let mut model = YOLOModel::load("yolo11n.onnx")?; // Run inference on an image let results = model.predict("image.jpg")?; // Process detection results for result in &results { if let Some(ref boxes) = result.boxes { println!("Found {} detections", boxes.len()); for i in 0..boxes.len() { let cls = boxes.cls()[i] as usize; let conf = boxes.conf()[i]; let xyxy = boxes.xyxy(); let name = result.names.get(&cls) .map(|s| s.as_str()) .unwrap_or("unknown"); println!(" {} {:.2} at [{:.1}, {:.1}, {:.1}, {:.1}]", name, conf, xyxy[[i, 0]], xyxy[[i, 1]], xyxy[[i, 2]], xyxy[[i, 3]]); } } } Ok(()) } ``` -------------------------------- ### Selecting Device for Inference with Ultralytics Rust Library Source: https://github.com/ultralytics/inference/blob/main/README.md Shows how to specify the inference device (e.g., CUDA, MPS, CPU) when using the Ultralytics Rust library. This involves creating a `Device` enum variant and passing it within the `InferenceConfig`. ```rust use ultralytics_inference::{Device, InferenceConfig, YOLOModel}; fn main() -> Result<(), Box> { // Select a device (e.g., CUDA, MPS, CPU) let device = Device::Cuda(0); // Configure the model to use this device let config = InferenceConfig::new().with_device(device); let mut model = YOLOModel::load_with_config("yolo11n.onnx", config)?; let results = model.predict("image.jpg")?; Ok(()) } ``` -------------------------------- ### Build without Annotation Support (Bash) Source: https://github.com/ultralytics/inference/blob/main/README.md Builds the Rust project in release mode, disabling default features. This results in a smaller binary size by excluding annotation-related functionalities. ```bash cargo build --release --no-default-features ``` -------------------------------- ### Export YOLO Model to ONNX Source: https://github.com/ultralytics/inference/blob/main/README.md Demonstrates how to export a YOLO model to the ONNX format. This can be done using the Ultralytics CLI or programmatically with Python. ONNX is a standard format for machine learning models, enabling interoperability. ```bash # Using Ultralytics CLI yolo export model=yolo11n.pt format=onnx ``` ```python from ultralytics import YOLO model = YOLO("yolo11n.pt") model.export(format="onnx") ``` -------------------------------- ### Accessing Detection Data in Ultralytics Rust Library Source: https://github.com/ultralytics/inference/blob/main/README.md Illustrates how to access detailed bounding box information from the inference results in the Ultralytics Rust library. This includes various coordinate formats (xyxy, xywh, normalized) and confidence scores. ```rust if let Some(ref boxes) = result.boxes { // Bounding boxes in different formats let xyxy = boxes.xyxy(); // [x1, y1, x2, y2] let xywh = boxes.xywh(); // [x_center, y_center, width, height] let xyxyn = boxes.xyxyn(); // Normalized [0-1] let xywhn = boxes.xywhn(); // Normalized [0-1] // Confidence scores and class IDs let conf = boxes.conf(); // Confidence scores let cls = boxes.cls(); // Class IDs } ``` -------------------------------- ### Access Detection Boxes in Different Formats (Rust) Source: https://context7.com/ultralytics/inference/llms.txt Retrieve bounding boxes in various coordinate formats (xyxy, xywh, normalized) along with confidence scores and class IDs. This functionality requires the ultralytics-inference crate and an ONNX model file. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { let mut model = YOLOModel::load("yolo11n.onnx")?; let results = model.predict("image.jpg")?; for result in &results { if let Some(ref boxes) = result.boxes { println!("Found {} detections", boxes.len()); // Get boxes in different formats let xyxy = boxes.xyxy(); // [x1, y1, x2, y2] absolute let xywh = boxes.xywh(); // [cx, cy, w, h] absolute let xyxyn = boxes.xyxyn(); // [x1, y1, x2, y2] normalized [0-1] let xywhn = boxes.xywhn(); // [cx, cy, w, h] normalized [0-1] // Get confidence scores and class IDs let conf = boxes.conf(); // Confidence scores [0-1] let cls = boxes.cls(); // Class IDs // Access individual detections for i in 0..boxes.len() { let class_id = cls[i] as usize; let confidence = conf[i]; let class_name = result.names.get(&class_id) .map(|s| s.as_str()) .unwrap_or("unknown"); println!("Detection {}: {} ({:.2}%)", i, class_name, confidence * 100.0); println!(" XYXY: [{:.1}, {:.1}, {:.1}, {:.1}]", xyxy[[i, 0]], xyxy[[i, 1]], xyxy[[i, 2]], xyxy[[i, 3]]); println!(" XYWH: [{:.1}, {:.1}, {:.1}, {:.1}]", xywh[[i, 0]], xywh[[i, 1]], xywh[[i, 2]], xywh[[i, 3]]); println!(" Normalized: [{:.3}, {:.3}, {:.3}, {:.3}]", xyxyn[[i, 0]], xyxyn[[i, 1]], xyxyn[[i, 2]], xyxyn[[i, 3]]); } } } Ok(()) } ``` -------------------------------- ### Generate Code Coverage Report with Cargo LLVM-Cov Source: https://github.com/ultralytics/inference/blob/main/tests/README.md Generates an HTML code coverage report for all features and the workspace. This command is recommended for local generation, especially on Linux systems. It requires the cargo-llvm-cov tool. ```bash cargo llvm-cov --all-features --workspace --html ``` -------------------------------- ### Load and Predict with Ultralytics Rust Library Source: https://github.com/ultralytics/inference/blob/main/README.md Basic usage of the Ultralytics Rust library to load a YOLO model and perform inference on an image. This involves loading the model, running predictions, and iterating through the results to access detection data. ```rust use ultralytics_inference::{YOLOModel, InferenceConfig}; fn main() -> Result<(), Box> { // Load model - metadata (classes, task, imgsz) is read automatically let mut model = YOLOModel::load("yolo11n.onnx")?; // Run inference let results = model.predict("image.jpg")?; // Process results for result in &results { if let Some(ref boxes) = result.boxes { println!("Found {} detections", boxes.len()); 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(|s| s.as_str()).unwrap_or("unknown"); println!(" {} {:.2}", name, conf); } } } Ok(()) } ``` -------------------------------- ### Instance Segmentation with Masks (Rust) Source: https://context7.com/ultralytics/inference/llms.txt Perform instance segmentation inference and access the resulting segmentation masks. This requires a segmentation-capable ONNX model and the ultralytics-inference crate. The masks are provided as a 3D array. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { // Load segmentation model (e.g., yolo11n-seg.onnx) let mut model = YOLOModel::load("yolo11n-seg.onnx")?; let results = model.predict("image.jpg")?; for result in &results { // Access segmentation masks if let Some(ref masks) = result.masks { println!("Found {} instance masks", masks.len()); println!("Mask shape: {:?}", masks.data.shape()); println!("Original image shape: {}x{}", masks.orig_shape.0, masks.orig_shape.1); // masks.data is an Array3 with shape (N, H, W) // where N is the number of instances for i in 0..masks.len() { let mask = masks.data.slice(ndarray::s![i, .., ..]); println!("Mask {} shape: {:?}", i, mask.shape()); } } // Segmentation models also return boxes if let Some(ref boxes) = result.boxes { println!("Found {} bounding boxes", boxes.len()); } } Ok(()) } ``` -------------------------------- ### Build Release Version with Cargo Source: https://github.com/ultralytics/inference/blob/main/README.md Builds the release version of the Ultralytics inference project using Cargo, Rust's build system and package manager. This command optimizes the code for performance. ```bash git clone https://github.com/ultralytics/inference.git cd inference cargo build --release ``` -------------------------------- ### Oriented Bounding Boxes (OBB) Detection in Rust Source: https://context7.com/ultralytics/inference/llms.txt Performs Oriented Bounding Box (OBB) detection to identify oriented objects and provides access to rotated box information. It demonstrates how to extract boxes in xywhr format, confidence scores, class IDs, corner points (xyxyxyxy), and axis-aligned bounding boxes (xyxy). Requires 'yolo11n-obb.onnx' and 'aerial_image.jpg'. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { // Load OBB model (e.g., yolo11n-obb.onnx) let mut model = YOLOModel::load("yolo11n-obb.onnx")?; let results = model.predict("aerial_image.jpg")?; for result in &results { // Access oriented bounding boxes if let Some(ref obb) = result.obb { println!("Found {} oriented boxes", obb.len()); // Get boxes in xywhr format [cx, cy, w, h, rotation] let xywhr = obb.xywhr(); // Get confidence scores and class IDs let conf = obb.conf(); let cls = obb.cls(); // Get corner points for rotated boxes (N, 4, 2) let corners = obb.xyxyxyxy(); // Get axis-aligned bounding boxes containing each OBB let xyxy = obb.xyxy(); for i in 0..obb.len() { let class_id = cls[i] as usize; let confidence = conf[i]; let class_name = result.names.get(&class_id) .map(|s| s.as_str()) .unwrap_or("unknown"); println!("OBB {}: {} ({:.2}%)", i, class_name, confidence * 100.0); println!(" Center: ({:.1}, {:.1})", xywhr[[i, 0]], xywhr[[i, 1]]); println!(" Size: {:.1}x{:.1}", xywhr[[i, 2]], xywhr[[i, 3]]); println!(" Rotation: {:.2} rad", xywhr[[i, 4]]); // Print corner points println!(" Corners:"); for j in 0..4 { println!(" ({:.1}, {:.1})", corners[[i, j, 0]], corners[[i, j, 1]]); } } } } Ok(()) } ``` -------------------------------- ### Image Classification with Probabilities in Rust Source: https://context7.com/ultralytics/inference/llms.txt Loads a classification model and performs image classification, providing access to class probabilities. It shows how to retrieve the top-1 and top-5 predictions with their confidence scores and class names, as well as the raw probability data. Assumes the existence of 'yolo11n-cls.onnx' and 'image.jpg'. ```rust use ultralytics_inference::YOLOModel; fn main() -> Result<(), Box> { // Load classification model (e.g., yolo11n-cls.onnx) let mut model = YOLOModel::load("yolo11n-cls.onnx")?; let results = model.predict("image.jpg")?; for result in &results { // Access classification probabilities if let Some(ref probs) = result.probs { // Get top-1 prediction let top1_class = probs.top1(); let top1_conf = probs.top1conf(); let top1_name = result.names.get(&top1_class) .map(|s| s.as_str()) .unwrap_or("unknown"); println!("Top-1: {} ({:.2}%)", top1_name, top1_conf * 100.0); // Get top-5 predictions let top5_classes = probs.top5(); let top5_confs = probs.top5conf(); println!("\nTop-5 predictions:"); for (i, (&class_id, &confidence)) in top5_classes.iter().zip(top5_confs.iter()).enumerate() { let class_name = result.names.get(&class_id) .map(|s| s.as_str()) .unwrap_or("unknown"); println!(" {}: {} ({:.2}%)", i + 1, class_name, confidence * 100.0); } // Access raw probability array println!("\nRaw probabilities shape: {:?}", probs.data.shape()); } } Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.