Performs real-time object detection using YOLO26 (or custom YOLO ONNX models) with bounding box regression and End-to-End detection.
Usage
image_detect_dl(
img,
model = "yolo26n",
conf_threshold = 0.25,
iou_threshold = 0.4,
classes = NULL,
labels = NULL,
col = NULL,
rainbow = TRUE,
lwd = 2,
cex = 1.5,
pad = 0,
bbox = TRUE,
show_text = TRUE,
show_conf = TRUE,
show_class = TRUE,
show_id = FALSE,
show_point = FALSE,
show_box = NULL,
point_size = 1,
point_pch = 16,
point_col = NULL,
badge = TRUE,
badge_pad = 1,
return_features = FALSE,
feature_model = c("yolo", "clip-vit-b32", "dinov2"),
plot_features = FALSE,
filter_nested = FALSE,
threads = 0,
engine = c("gpu", "cpu"),
device_id = -1,
verbose = TRUE,
plot = TRUE,
dir = pliman_model_dir(),
imgsz = NULL,
max_det = 0,
...
)Arguments
- img
An
Imageobject, a 2D/3D numeric array, a path to an image file, or a list of images.- model
Model name or path to a custom ONNX file. Defaults to
"yolo26n". Options include"yolo26n","yolo26s","yolo26m","yolo26l","yolo26x".- conf_threshold
Minimum confidence score for candidate boxes (default 0.25).
- iou_threshold
IoU threshold for Non-Maximum Suppression (default 0.40).
- classes
Optional character or numeric vector of target classes to keep (e.g.,
"ship"orc("ship", "large vehicle")). All other detected classes are filtered out. Defaults toNULL(keep all).- labels
Optional character vector of class names (defaults to standard 80 COCO classes).
- col
Optional color or palette for bounding boxes.
- rainbow
Logical. If
TRUE(the default), assigns a distinct, unique color to each detected object. IfFALSE, objects of the same class share the exact same color.- lwd
Line width for bounding boxes (default 2).
- cex
Size factor for label text (default
1.5). Use smaller values (e.g.,cex = 0.2or0.5) to reduce text size for dense scenes or many objects.- pad
Padding added to expand bounding boxes around detected objects (default
0). Can be an absolute number of pixels (e.g.,pad = 10), a relative expansion fraction when between 0 and 1 (e.g.,pad = 0.05for 5% margin), or a length-2 vectorc(pad_x, pad_y).- bbox
Logical. Whether to draw bounding boxes (default
TRUE).- show_text
Logical. Whether to display text labels on bounding boxes (default
TRUE).- show_conf
Logical. Whether to display confidence scores in labels (default
TRUE).- show_class
Logical. Whether to display class names in labels (default
TRUE).- show_id
Logical. Whether to display object IDs (
#1,#2, ...) in labels (defaultFALSE).- show_point
Logical. Whether to show a point at the centroid of each detected object. Defaults to
FALSE.- show_box
Logical. Whether to draw bounding boxes. Defaults to
NULL(TRUE unlessshow_point = TRUEand all text displays areFALSE).- point_size
Numeric. Size (
cex) of the centroid point whenshow_point = TRUE. Defaults to1.- point_pch
Integer. Plotting symbol (
pch) for the centroid point. Defaults to16(filled circle).- point_col
Character. Color of the centroid point. Defaults to
NULL(matches the object color / rainbow).- badge
Logical. Whether to draw a solid background badge behind label text (default
TRUE). IfFALSE, only the text is drawn directly.- badge_pad
Padding multiplier controlling the background badge size around label text (default
1.0). Usebadge_pad = 0for a badge fitting tightly to the text.- return_features
Logical. If
TRUE, extracts deep feature representations for detected bounding boxes and the global image. Attaches"features"(an \(N \times D\) matrix of per-object deep embeddings, 512-D for CLIP or 384-D for DINOv2) and"feature_map"(dense spatial 3-component PCA false-colorImage) as attributes to the returned object. Defaults toFALSE.- feature_model
Foundation model used for feature representation when
return_features = TRUE. Options are"clip-vit-b32"(default, OpenAI CLIP ViT-B/32 producing 512-D semantic embeddings) or"dinov2"(Meta DINOv2-S ViT producing 384-D self-supervised patch embeddings).- plot_features
Logical. If
TRUEandreturn_features = TRUE, visualizes the dense spatial PCA feature map. Defaults toFALSE.- filter_nested
Logical. If
TRUE, removes larger container bounding boxes that enclose smaller boxes of the same class. Defaults toFALSE.- threads
Number of CPU threads (default 0 for auto-tuning).
- engine
Execution engine:
"cpu"or"gpu"(DirectML).- device_id
GPU device ID (default -1 for auto).
- verbose
Logical. Whether to show progress messages.
- plot
Logical. Whether to plot the original image with bounding boxes.
- dir
Model directory (default
pliman_model_dir()).- imgsz
Target input image resolution for YOLO inference (e.g.,
640,960, or1024). IfNULL(default), pliman automatically inspects the ONNX model metadata for native training resolution and falls back to640if unspecified. Useful when running models exported with dynamic resolution.- max_det
Integer. Maximum number of detections kept after non-maximum suppression (highest scores first).
0(default) keeps all detections, which is the right choice for dense scenes such as seed or grain counting.- ...
Additional arguments passed to
graphics::plot().
Value
A data frame containing detected bounding boxes (id, xmin, ymin, xmax, ymax, label, score),
with summary and counts stored as attributes. If return_features = TRUE, features (an \(N \times D\) matrix)
and feature_map (dense spatial PCA Image) are also attached as attributes.
When img is a list or a character vector with several file paths, a list of such
results is returned.
Performance
ONNX sessions are cached for the R session (see pliman_session_info()), so only the
first call pays model loading and, on GPU, DirectML compilation. For many images:
use
plot = FALSEandverbose = FALSE(drawing a full-resolution raster often costs more than the inference itself);prefer
image_detect_dl_batch(), which resolves the model once, can run in parallel and, with persistent workers frompliman_daemons(), keeps models loaded between calls;choose
imgszdeliberately: cost grows roughly with its square;engine = "gpu"gains depend on the GPU. FP16 models (yolo_export_fp16()) are faster only on GPUs with native half-precision throughput (e.g. NVIDIA Tensor Cores); on other GPUs and on CPU the casting overhead can make them slower.
Use yolo_benchmark() to measure inference speed on your hardware.
Examples
if (FALSE) { # interactive()
library(pliman)
farm <- image_pliman("farm.jpg")
# Detect the 80 COCO classes (people, animals, vehicles, ...)
boxes <- image_detect_dl(farm, model = "yolo26x")
head(boxes)
# Count objects per class
table(boxes$label)
# Keep only some classes
animals <- image_detect_dl(farm, model = "yolo26x", classes = c("cow", "horse", "sheep", "dog"))
# Same color for all boxes of a class (instead of one color per box)
boxes_cls <- image_detect_dl(farm, rainbow = FALSE)
}
