Object Detection¶
Use create_detector with any registered object detection model. The model registry selects the inference backend,
class labels, and default confidence threshold.
Set batch_size above one to batch list inputs when the selected ONNX model has a dynamic batch dimension. Models
exported with a fixed batch size of one automatically retain serial inference.
Local ONNX models require their backend and class labels:
from open_image_models import create_detector
detector = create_detector(
"/path/to/model.onnx",
backend="rf_detr",
class_labels=["vehicle", "License Plate"],
)
Creates an object detector from a registered model or local ONNX file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
DetectionModelName | str | PathLike[str]
|
Registered model name or path to a local ONNX model. |
required |
backend
|
DetectorBackend | None
|
Inference backend. Required only for local models. |
None
|
class_labels
|
ClassLabels | None
|
Contiguous labels or a class-ID mapping. Required only for local models. |
None
|
conf_thresh
|
float | None
|
Confidence threshold. Uses the model default when omitted. |
None
|
batch_size
|
int
|
Maximum inference batch size for models with a dynamic batch dimension. |
1
|
providers
|
Sequence[str | tuple[str, dict]] | None
|
ONNX Runtime providers in order of decreasing precedence. |
None
|
sess_options
|
SessionOptions | None
|
Advanced ONNX Runtime session options. |
None
|
Returns:
| Type | Description |
|---|---|
ObjectDetector
|
A detector configured for the selected model or local file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If local model metadata is missing or registered model metadata is overridden. |
FileNotFoundError
|
If a local model file does not exist. |
Source code in open_image_models/detection/factory.py
Core API¶
The core module provides base classes and protocols for object detection models, including essential data structures like BoundingBox and DetectionResult.
🔧 Core Components¶
The following components are shared by all detection backends:
BoundingBox: Represents a bounding box for detected objects.DetectionResult: Stores label, confidence, and bounding box for a detection.ObjectDetector: Protocol definingpredictanddisplay_predictions.
ClassLabels
module-attribute
¶
Class labels as a contiguous sequence or explicit class-ID mapping.
BoundingBox
dataclass
¶
Represents an axis-aligned 2D bounding box defined by two corner points.
area
property
¶
Returns:
| Type | Description |
|---|---|
int
|
The bounding box area, or zero if the box is empty. |
aspect_ratio
property
¶
Returns:
| Type | Description |
|---|---|
float
|
The width-to-height ratio, or zero if the box is empty. |
is_empty
property
¶
Returns:
| Type | Description |
|---|---|
bool
|
|
xyxy
property
¶
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int]
|
The coordinates as |
center
property
¶
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
The center coordinates as |
from_xywh
classmethod
¶
from_xywh(
x: int, y: int, width: int, height: int
) -> BoundingBox
Creates a bounding box from top-left coordinates, width, and height.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
int
|
X-coordinate of the left edge. |
required |
y
|
int
|
Y-coordinate of the top edge. |
required |
width
|
int
|
Width of the bounding box. |
required |
height
|
int
|
Height of the bounding box. |
required |
Returns:
| Type | Description |
|---|---|
BoundingBox
|
A bounding box in |
Source code in open_image_models/detection/core/base.py
from_cxcywh
classmethod
¶
from_cxcywh(
center_x: float,
center_y: float,
width: float,
height: float,
) -> BoundingBox
Creates a bounding box from center coordinates, width, and height.
Coordinates are rounded outward so the integer box contains the full floating-point region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center_x
|
float
|
X-coordinate of the center. |
required |
center_y
|
float
|
Y-coordinate of the center. |
required |
width
|
float
|
Width of the bounding box. |
required |
height
|
float
|
Height of the bounding box. |
required |
Returns:
| Type | Description |
|---|---|
BoundingBox
|
A bounding box with integer coordinates. |
Source code in open_image_models/detection/core/base.py
intersection ¶
intersection(other: BoundingBox) -> Optional[BoundingBox]
Computes the intersection with another bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BoundingBox
|
The bounding box to intersect with this box. |
required |
Returns:
| Type | Description |
|---|---|
Optional[BoundingBox]
|
The positive-area intersection, or |
Source code in open_image_models/detection/core/base.py
intersects ¶
intersects(other: BoundingBox) -> bool
Checks whether this bounding box intersects another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BoundingBox
|
The bounding box to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in open_image_models/detection/core/base.py
contains_point ¶
Checks whether a point lies within this bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
X-coordinate of the point. |
required |
y
|
float
|
Y-coordinate of the point. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in open_image_models/detection/core/base.py
contains ¶
contains(other: BoundingBox) -> bool
Checks whether this bounding box fully contains another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BoundingBox
|
The bounding box to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in open_image_models/detection/core/base.py
iou ¶
iou(other: BoundingBox) -> float
Computes the Intersection-over-Union (IoU) with another bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BoundingBox
|
The bounding box to compare with this box. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The IoU in the range |
Source code in open_image_models/detection/core/base.py
intersection_over_area ¶
intersection_over_area(other: BoundingBox) -> float
Computes the intersection divided by this bounding box's area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BoundingBox
|
The bounding box to compare with this box. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The fraction of this box covered by |
Source code in open_image_models/detection/core/base.py
enclosing ¶
enclosing(other: BoundingBox) -> BoundingBox
Computes the smallest bounding box containing both boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BoundingBox
|
The bounding box to enclose with this box. |
required |
Returns:
| Type | Description |
|---|---|
BoundingBox
|
A bounding box spanning this box and |
Source code in open_image_models/detection/core/base.py
to_xywh ¶
Converts the bounding box to top-left, width, and height format.
Returns:
| Type | Description |
|---|---|
tuple[int, int, int, int]
|
The bounding box as |
Source code in open_image_models/detection/core/base.py
to_cxcywh ¶
Converts the bounding box to center, width, and height format.
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float]
|
The bounding box as |
Source code in open_image_models/detection/core/base.py
as_slices ¶
Converts the bounding box to NumPy-compatible image slices.
NumPy excludes the stop coordinate, so pixels at x2 and y2 are not included.
Returns:
| Type | Description |
|---|---|
tuple[slice, slice]
|
A |
Source code in open_image_models/detection/core/base.py
clamp ¶
clamp(max_width: int, max_height: int) -> BoundingBox
Clamps the bounding box coordinates to frame boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_width
|
int
|
Maximum x-coordinate, normally the frame width. |
required |
max_height
|
int
|
Maximum y-coordinate, normally the frame height. |
required |
Returns:
| Type | Description |
|---|---|
BoundingBox
|
A bounding box whose coordinates lie within |
Source code in open_image_models/detection/core/base.py
is_inside ¶
Checks whether all coordinates lie within frame boundaries.
This method only checks coordinate bounds; it does not require the box to have positive area. Use is_valid
when both conditions are needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_width
|
int
|
Width of the frame. |
required |
frame_height
|
int
|
Height of the frame. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in open_image_models/detection/core/base.py
is_valid ¶
Checks whether the bounding box is non-empty and inside a frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_width
|
int
|
Width of the frame. |
required |
frame_height
|
int
|
Height of the frame. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
otherwise |
Source code in open_image_models/detection/core/base.py
DetectionResult
dataclass
¶
DetectionResult(
label: str, confidence: float, bounding_box: BoundingBox
)
Represents the result of an object detection.
from_detection_data
classmethod
¶
from_detection_data(
bbox_data: tuple[int, int, int, int],
confidence: float,
label: str,
) -> DetectionResult
Creates a DetectionResult instance from bounding box data, confidence, and a class label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox_data
|
tuple[int, int, int, int]
|
Bounding box coordinates as |
required |
confidence
|
float
|
Detection confidence score. |
required |
label
|
str
|
Detected class label. |
required |
Returns:
| Type | Description |
|---|---|
DetectionResult
|
A detection result containing the supplied data. |
Source code in open_image_models/detection/core/base.py
ObjectDetector ¶
Bases: Protocol
predict ¶
predict(
images: Any,
) -> list[DetectionResult] | list[list[DetectionResult]]
Perform object detection on one or multiple images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Any
|
A single image as a numpy array, a single image path as a string, a list of images as numpy arrays, or a list of image file paths. |
required |
Returns:
| Type | Description |
|---|---|
list[DetectionResult] | list[list[DetectionResult]]
|
A list of DetectionResult for a single image input, |
list[DetectionResult] | list[list[DetectionResult]]
|
or a list of lists of DetectionResult for multiple images. |
Source code in open_image_models/detection/core/base.py
display_predictions ¶
Run object detection on the input image and display the predictions on the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
An input image as a numpy array. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The image with bounding boxes and labels drawn on it. |
Source code in open_image_models/detection/core/base.py
ModelInputShape
dataclass
¶
Relevant dimensions read from a detector's NCHW input shape.
normalize_class_labels ¶
normalize_class_labels(
class_labels: ClassLabels,
) -> dict[int, str]
Normalize class labels into an explicit class-ID mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_labels
|
ClassLabels
|
Contiguous labels or a mapping from model class IDs to labels. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
A class-ID-to-label dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no labels are supplied or a class ID or label is invalid. |
Source code in open_image_models/detection/core/base.py
inspect_model_input_shape ¶
inspect_model_input_shape(
input_shape: Sequence[Any],
) -> ModelInputShape
Validates a detector input shape and determines its batching capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_shape
|
Sequence[Any]
|
ONNX model input shape in NCHW format. |
required |
Returns:
| Type | Description |
|---|---|
ModelInputShape
|
Static image dimensions and whether the batch dimension is dynamic. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the shape is not NCHW, does not use three channels, has dynamic spatial dimensions, or has a fixed batch size other than one. |
Source code in open_image_models/detection/core/base.py
resolve_image_inputs ¶
Normalize supported detector inputs into loaded image arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Any
|
A single image array/path or a list of image arrays/paths. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[ndarray], bool]
|
A tuple of loaded BGR images and whether the original input was a single image. |
Source code in open_image_models/detection/core/base.py
draw_detection_results ¶
draw_detection_results(
image: ndarray, detections: list[DetectionResult]
) -> ndarray
Draw detection results on an image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
Input image to mutate. |
required |
detections
|
list[DetectionResult]
|
Detection results to draw. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The input image with bounding boxes and labels drawn on it. |
Source code in open_image_models/detection/core/base.py
Open Image Models HUB.
DetectionModelName
module-attribute
¶
DetectionModelName = Literal[
"rf-detr-nano-384-coco",
"rf-detr-small-512-coco",
"rf-detr-medium-576-coco",
"rf-detr-large-704-coco",
"yolo-v9-s-608-license-plate-end2end",
"yolo-v9-t-640-license-plate-end2end",
"yolo-v9-t-512-license-plate-end2end",
"yolo-v9-t-416-license-plate-end2end",
"yolo-v9-t-384-license-plate-end2end",
"yolo-v9-t-256-license-plate-end2end",
]
Names of the available object detection models.
LicensePlateModelName
module-attribute
¶
LicensePlateModelName = Literal[
"yolo-v9-s-608-license-plate-end2end",
"yolo-v9-t-640-license-plate-end2end",
"yolo-v9-t-512-license-plate-end2end",
"yolo-v9-t-416-license-plate-end2end",
"yolo-v9-t-384-license-plate-end2end",
"yolo-v9-t-256-license-plate-end2end",
]
Names of the available license plate detection models.
DetectionModelSpec
dataclass
¶
DetectionModelSpec(
url: str,
backend: DetectorBackend,
class_labels: ClassLabels,
default_conf_thresh: float,
)
Configuration required to construct a detector for a trained model.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
URL of the ONNX model file. |
backend |
DetectorBackend
|
Inference backend used by the model. |
class_labels |
ClassLabels
|
Labels corresponding to the model's class IDs. |
default_conf_thresh |
float
|
Default confidence threshold for predictions. |
DETECTION_MODELS
module-attribute
¶
DETECTION_MODELS: dict[
DetectionModelName, DetectionModelSpec
] = {
"rf-detr-nano-384-coco": DetectionModelSpec(
url=f"{BASE_URL}/rf-detr-nano-384-coco.onnx",
backend="rf_detr",
class_labels=COCO_CLASSES,
default_conf_thresh=0.5,
),
"rf-detr-small-512-coco": DetectionModelSpec(
url=f"{BASE_URL}/rf-detr-small-512-coco.onnx",
backend="rf_detr",
class_labels=COCO_CLASSES,
default_conf_thresh=0.5,
),
"rf-detr-medium-576-coco": DetectionModelSpec(
url=f"{BASE_URL}/rf-detr-medium-576-coco.onnx",
backend="rf_detr",
class_labels=COCO_CLASSES,
default_conf_thresh=0.5,
),
"rf-detr-large-704-coco": DetectionModelSpec(
url=f"{BASE_URL}/rf-detr-large-704-coco.onnx",
backend="rf_detr",
class_labels=COCO_CLASSES,
default_conf_thresh=0.5,
),
"yolo-v9-s-608-license-plate-end2end": DetectionModelSpec(
url=f"{BASE_URL}/yolo-v9-s-608-license-plates-end2end.onnx",
backend="yolo_v9",
class_labels=("License Plate",),
default_conf_thresh=0.25,
),
"yolo-v9-t-640-license-plate-end2end": DetectionModelSpec(
url=f"{BASE_URL}/yolo-v9-t-640-license-plates-end2end.onnx",
backend="yolo_v9",
class_labels=("License Plate",),
default_conf_thresh=0.25,
),
"yolo-v9-t-512-license-plate-end2end": DetectionModelSpec(
url=f"{BASE_URL}/yolo-v9-t-512-license-plates-end2end.onnx",
backend="yolo_v9",
class_labels=("License Plate",),
default_conf_thresh=0.25,
),
"yolo-v9-t-416-license-plate-end2end": DetectionModelSpec(
url=f"{BASE_URL}/yolo-v9-t-416-license-plates-end2end.onnx",
backend="yolo_v9",
class_labels=("License Plate",),
default_conf_thresh=0.25,
),
"yolo-v9-t-384-license-plate-end2end": DetectionModelSpec(
url=f"{BASE_URL}/yolo-v9-t-384-license-plates-end2end.onnx",
backend="yolo_v9",
class_labels=("License Plate",),
default_conf_thresh=0.25,
),
"yolo-v9-t-256-license-plate-end2end": DetectionModelSpec(
url=f"{BASE_URL}/yolo-v9-t-256-license-plates-end2end.onnx",
backend="yolo_v9",
class_labels=("License Plate",),
default_conf_thresh=0.25,
),
}
Detection models available through create_detector.