Skip to contents

Loads sample images and their matching .txt annotation labels from a YOLO dataset directory and displays them in a multi-panel grid with overlaid segmentation polygons or bounding boxes. By default, randomly samples n images from the requested split.

Usage

yolo_dataset_preview(
  dir = "yolo_dataset",
  split = c("train", "val", "test"),
  show_bbox = TRUE,
  show_polygon = TRUE,
  show_points = FALSE,
  show_labels = TRUE,
  bbox = show_bbox,
  polygon = show_polygon,
  points = show_points,
  labels = show_labels,
  n = 4L,
  which = NULL,
  images = NULL,
  only_augmented = NULL,
  only_original = NULL,
  only_tiles = FALSE,
  pattern = NULL,
  random = is.null(which) && is.null(images),
  seed = NULL,
  mfrow = NULL,
  rainbow = TRUE,
  col = NULL,
  lwd = 1.5,
  fill = NULL,
  label_bg = FALSE,
  cex = 0.6,
  title = TRUE,
  save = FALSE,
  file_name = NULL,
  width = NULL,
  height = NULL,
  dpi = 300,
  ...
)

Arguments

dir

Character or yolo_dataset object. Path to the dataset directory (or list from yolo_dataset_export() / yolo_dataset_split()).

split

Character. The subset to preview: "train" (default), "val", or "test".

show_bbox

Logical. Whether to draw bounding boxes and annotations. Defaults to TRUE. Supported alias: bbox.

show_polygon

Logical. For segmentation datasets, whether to draw polygons. Defaults to TRUE. Supported alias: polygon.

show_points

Logical. If TRUE, draws points at the centroid of each bounding box or polygon using the same color as the box. Defaults to FALSE. Supported alias: points.

show_labels

Logical. If TRUE (default), displays class name labels. Supported aliases: labels, show_class.

bbox, polygon, points, labels

Backward-compatible aliases for show_bbox, show_polygon, show_points, and show_labels.

n

Integer. Number of sample images to preview. Defaults to 4L. Supported aliases: n_images, num_images.

which

Optional integer vector of image indices (e.g. 1:4) or character vector of image names or stems (e.g. c("img_3_aug1", "img_553_aug2")) to plot specific images.

images

Alias for which.

only_augmented

Logical. If TRUE, only previews augmented images (containing _aug). Defaults to TRUE if dir is an augmented dataset (yolo_dataset_aug), and FALSE otherwise.

only_original

Logical. If TRUE, only previews original pristine images (excluding synthetic augmentations _aug[0-9]+, synthetic backgrounds synth_bg_, external backgrounds bg_, and scrambled backgrounds scramble_). Defaults to FALSE (or NULL).

only_tiles

Logical. If TRUE, only previews tiled crop images (matching _tile_[0-9]+_[0-9]+). Defaults to FALSE.

pattern

Optional regex pattern to filter specific image names (e.g. "toras5\\.png"). Defaults to NULL.

random

Logical. If TRUE (default when which is NULL), samples random images from the split. If FALSE, picks the first n images.

seed

Optional integer seed for reproducible random selection.

mfrow

Numeric vector of length 2 giving c(rows, cols) for the multi-panel layout.

rainbow

Logical. If TRUE (default), assigns distinct rainbow colors to each object within the image (matching review previews). If FALSE, colors are assigned by class.

col

Border color for bounding boxes/polygons. If provided, overrides rainbow and default class palette.

lwd

Numeric. Line width for borders. Defaults to 1.5.

fill

Fill color for polygons with transparency (e.g. "#00FF6633"). If NULL, no fill.

label_bg

Logical. If TRUE, draws a subtle background badge behind each label. If FALSE (default), draws clean, discreet text matching review previews without blocking objects.

cex

Numeric. Text size for class name labels. Defaults to 0.6.

title

Logical or character. If TRUE (default), displays panel titles with filename and object count.

save

Logical. If TRUE, saves the generated plot to a file. Defaults to FALSE.

file_name

Optional character string. Custom file name or path to save the plot (e.g. "dataset_preview.png").

width

Numeric. Width of the output plot. If <= 50, interpreted as inches; if > 50, interpreted as pixels. Defaults to max(7, nc * 3.5).

height

Numeric. Height of the output plot. If <= 50, interpreted as inches; if > 50, interpreted as pixels. Defaults to max(6, nr * 3.5).

dpi

Numeric. Resolution in dots per inch (DPI) for raster formats (PNG, JPEG, TIFF). Defaults to 300.

...

Additional arguments.

Value

An invisible list of the sampled image file paths.

Examples

if (FALSE) { # interactive()
dataset <- yolo_dataset_export(img, dir = "dataset_cafe", model = "persam")
yolo_dataset_preview(dataset, n = 4, random = TRUE)
plot(dataset, n = 6)
plot(dataset, n = 4, save = TRUE, file_name = "preview.png")
}