Skip to main content

Overview

The ModelLoader class handles loading and initialization of YOLO11s NCNN models for object detection in the robotic arm system. Source: arm_system/perception/vision/detection/model_loader.py:7

Class Definition

ModelLoader

class ModelLoader:
    def __init__(self)
Initializes the model loader and loads the YOLO11s NCNN model. Model Path: {current_directory}/models/yolo11s_ncnn_model

Attributes

model
YOLO
Loaded YOLO model instance configured for object detection
Source: arm_system/perception/vision/detection/model_loader.py:8

Methods

get_model

def get_model(self) -> YOLO
Returns the loaded YOLO model instance.
model
YOLO
Ultralytics YOLO model ready for inference
Source: arm_system/perception/vision/detection/model_loader.py:13

Model Details

YOLO11s NCNN Format

The loader uses YOLO11s in NCNN format:
  • Framework: NCNN (neural compute network)
  • Model Size: Small (11s variant)
  • Task: Object detection
  • Format: Optimized for edge devices

Directory Structure

arm_system/perception/vision/detection/
├── model_loader.py
└── models/
    └── yolo11s_ncnn_model/
        ├── model.ncnn.param
        └── model.ncnn.bin

Example Usage

from arm_system.perception.vision.detection.model_loader import ModelLoader

# Load model
loader = ModelLoader()
model = loader.get_model()

# Use model for inference
results = model.predict(image, conf=0.55, verbose=False)

# Access class names
class_names = model.names
print(f"Model supports {len(class_names)} classes")

Integration

Used by DetectionModel to load the YOLO model:
# In DetectionModel.__init__
from .model_loader import ModelLoader

self.object_model = ModelLoader().get_model()

Model Configuration

The loaded model includes: Attributes:
  • names: Dictionary mapping class IDs to class names
  • task: Set to ‘detect’ for object detection
Capabilities:
  • Object detection on images
  • Bounding box prediction
  • Class classification
  • Confidence scoring

NCNN Benefits

Why NCNN Format:
  1. Optimized for Embedded Systems: Designed for ARM processors (Raspberry Pi)
  2. Low Memory Footprint: Efficient memory usage
  3. Fast Inference: Optimized for CPU inference
  4. No GPU Required: Runs efficiently on CPU-only devices

Path Resolution

current_path = os.path.dirname(os.path.abspath(__file__))
object_model_path: str = current_path + '/models/yolo11s_ncnn_model'
The model path is resolved relative to the model_loader.py file location, ensuring portability across different deployment environments.

Complete Example

import cv2
from arm_system.perception.vision.detection.model_loader import ModelLoader

# Initialize loader
loader = ModelLoader()
model = loader.get_model()

# Load and process image
image = cv2.imread('test_image.jpg')
results = model.predict(
    image,
    conf=0.55,
    verbose=False,
    imgsz=640,
    stream=True,
    task='detect',
    half=True
)

# Process results
for result in results:
    boxes = result.boxes
    for box in boxes:
        class_id = int(box.cls[0])
        class_name = model.names[class_id]
        confidence = box.conf[0]
        print(f"{class_name}: {confidence:.2f}")

Dependencies

import os
from typing import Dict
from ultralytics import YOLO
Required Package: ultralytics (includes YOLO and NCNN support)

Error Handling

If the model path is invalid or the model files are missing, the YOLO constructor will raise an exception. Ensure the model directory exists and contains valid NCNN model files:
  • model.ncnn.param - Network architecture
  • model.ncnn.bin - Model weights

Build docs developers (and LLMs) love