Skip to contents

Runs deep learning object detection (e.g., YOLO models) on a batch of images defined by a directory path, a filename pattern, a vector of file paths, or a list of Image objects. Supports sequential or parallel processing (via mirai), and returns a consolidated predictions dataset along with per-image and per-class summaries.

Usage

image_detect_dl_batch(
  data,
  pattern = NULL,
  path = NULL,
  parallel = FALSE,
  sahi = FALSE,
  slice_height = 640,
  slice_width = 640,
  overlap_height_ratio = 0.2,
  overlap_width_ratio = 0.2,
  match_metric = "IOS",
  match_threshold = 0.5,
  postprocess_type = "GREEDY_NMM",
  show_point = FALSE,
  keep_images = FALSE,
  save_image = FALSE,
  dir_processed = "./proc",
  prefix = "proc_",
  verbose = TRUE,
  imgsz = NULL,
  as_job = FALSE,
  ...
)

image_detect_batch(
  data,
  pattern = NULL,
  path = NULL,
  parallel = FALSE,
  sahi = FALSE,
  slice_height = 640,
  slice_width = 640,
  overlap_height_ratio = 0.2,
  overlap_width_ratio = 0.2,
  match_metric = "IOS",
  match_threshold = 0.5,
  postprocess_type = "GREEDY_NMM",
  show_point = FALSE,
  keep_images = FALSE,
  save_image = FALSE,
  dir_processed = "./proc",
  prefix = "proc_",
  verbose = TRUE,
  imgsz = NULL,
  as_job = FALSE,
  ...
)

yolo_detect_batch(
  data,
  pattern = NULL,
  path = NULL,
  parallel = FALSE,
  sahi = FALSE,
  slice_height = 640,
  slice_width = 640,
  overlap_height_ratio = 0.2,
  overlap_width_ratio = 0.2,
  match_metric = "IOS",
  match_threshold = 0.5,
  postprocess_type = "GREEDY_NMM",
  show_point = FALSE,
  keep_images = FALSE,
  save_image = FALSE,
  dir_processed = "./proc",
  prefix = "proc_",
  verbose = TRUE,
  imgsz = NULL,
  as_job = FALSE,
  ...
)

Arguments

data

Input data. Can be:

  • A character string representing a directory path containing images (e.g., "flax_grains").

  • A character pattern to match image files in path or the current directory (e.g., "-", "^img_").

  • A character vector of image file paths.

  • A list of Image objects or numeric arrays.

pattern

Optional regex pattern to filter image filenames when data is a directory path.

path

Optional directory path to search when data is a pattern. Defaults to ".".

parallel

Logical. If TRUE, runs inference in parallel across images using mirai. Defaults to FALSE.

sahi

Logical. If TRUE, uses SAHI (Slicing Aided Hyper Inference) for tiled detection on large or high-resolution images. Defaults to FALSE.

slice_height

Integer. Height of sliding window slices when sahi = TRUE. Defaults to 640.

slice_width

Integer. Width of sliding window slices when sahi = TRUE. Defaults to 640.

overlap_height_ratio

Numeric scalar between 0 and 0.5. Vertical slice overlap fraction when sahi = TRUE. Defaults to 0.20.

overlap_width_ratio

Numeric scalar between 0 and 0.5. Horizontal slice overlap fraction when sahi = TRUE. Defaults to 0.20.

match_metric

Character string. Box merging overlap metric used when sahi = TRUE ("IOS" or "IOU"). Defaults to "IOS".

match_threshold

Numeric scalar. Overlap threshold for merging slice detections when sahi = TRUE. Defaults to 0.50.

postprocess_type

Character string. Post-processing algorithm used when sahi = TRUE ("GREEDY_NMM" or "NMS"). Defaults to "GREEDY_NMM".

show_point

Logical. Whether to show a point at the centroid of each detected object. Defaults to FALSE.

keep_images

Logical. Whether to keep the full original images attached as attributes in $results. Defaults to FALSE to prevent excessive RAM consumption during large batches (saving gigabytes of memory).

save_image

Logical. Whether to save annotated prediction images with bounding boxes to disk. Defaults to FALSE.

dir_processed

Character string. Directory where annotated images should be saved when save_image = TRUE. Defaults to "./proc".

prefix

Character string. Prefix prepended to saved output filenames. Defaults to "proc_".

verbose

Logical. Whether to show progress bars, timing, and formatted CLI summaries. Defaults to TRUE.

imgsz

Target input image resolution for YOLO inference (e.g., 640, 960, or 1024). If NULL (default), inferred automatically or defaults to 640.

as_job

Logical. If TRUE and running inside RStudio, runs the batch detection process in an RStudio Background Job. Defaults to FALSE.

...

Additional arguments passed to image_detect_dl() (e.g., model, conf_threshold, iou_threshold, classes, labels, col, rainbow, lwd, cex, badge, engine, device_id).

Value

An S3 object of class c("dl_detect_batch", "list") containing:

  • predictions: A consolidated data.frame of all detected bounding boxes across all images, with column img identifying the source image.

  • summary: A per-image data.frame summarizing total objects detected, mean confidence scores, and individual counts for each detected class.

  • class_summary: A summary data.frame across the entire batch with total counts, frequencies, average per image, and mean confidence scores per class.

  • results: A named list of raw individual detection outputs from image_detect_dl().

Performance

The model is resolved once and every image reuses the cached ONNX session. With parallel = TRUE each worker process loads its own copy of the model, so:

  • on CPU (engine = "cpu") and threads = 0, each worker gets physical cores / workers ONNX threads to avoid oversubscribing the CPU;

  • on GPU (engine = "gpu") concurrency is capped at getOption("pliman.gpu_workers", 2), because every worker opens its own GPU session (GPU memory grows with the number of workers);

  • temporary workers are started and stopped on every call. Start persistent workers with pliman_daemons() to keep models loaded across calls;

  • pass file paths rather than in-memory images, so only paths are sent to the workers.

Examples

if (FALSE) { # interactive()
library(pliman)

# 1. Batch detection in a specific folder (Sequential)
res <- image_detect_dl_batch(
  data = "flax_grains",
  model = "yolo26n",
  conf_threshold = 0.35
)
head(res$summary)
head(res$predictions)

# 2. Batch detection using filename pattern in parallel
res_par <- image_detect_dl_batch(
  data = "-",
  path = "flax_grains",
  parallel = TRUE
)
}