Skip to contents

Scans a directory of images, detects and recognizes alphanumeric text tags (e.g., plot tags, accession IDs, ear tags, seed packets), and automatically renames or copies the files based on the recognized text.

Includes advanced spatial positioning (text_pos) to select the relevant text when multiple labels appear in the image, regular expression pattern matching, conflict resolution, dry-run simulation, and full undo capabilities via image_rename_undo.

Usage

image_rename_ocr(
  dir = ".",
  pattern = NULL,
  recursive = FALSE,
  text_pos = c("confidence", "first", "top-left", "top-right", "bottom-left",
    "bottom-right", "top", "bottom", "center", "largest"),
  pattern_match = NULL,
  prefix = "",
  suffix = "",
  case = c("as_is", "upper", "lower"),
  sep = "_",
  min_chars = 1L,
  min_score = 0.6,
  sanitize = TRUE,
  keep_extension = TRUE,
  copy = FALSE,
  output_dir = NULL,
  conflict = c("rename", "skip", "overwrite"),
  dry_run = FALSE,
  save_log = TRUE,
  det_model = c("ppocrv5_det", "ppocrv5_det_server", "ppocr_det", "ppocr_det_server",
    "craft"),
  rec_model = "ppocrv5_rec_latin",
  det_thresh = 0.3,
  box_thresh = 0.6,
  unclip_ratio = 1.5,
  text_threshold = 0.7,
  link_threshold = 0.4,
  low_text = 0.4,
  engine = c("gpu", "cpu"),
  threads = 0,
  dir_models = pliman_model_dir(),
  verbose = TRUE,
  ...
)

Arguments

dir

Character string. Path to the directory containing images to scan. Default is ".".

pattern

Optional character string containing a regular expression to filter image files. If NULL (default), matches common image extensions: "\.(jpe?g|png|tif{1,2}|bmp|webp)$".

recursive

Logical. Whether to scan subdirectories recursively. Default is FALSE.

text_pos

Spatial or heuristic strategy used to select the target text when multiple text regions are detected in an image:

  • "confidence" (default): Text region with the highest recognition confidence score.

  • "first": First detected text region in scan order.

  • "top-left": Closest bounding box to the top-left canvas corner.

  • "top-right": Closest bounding box to the top-right canvas corner.

  • "bottom-left": Closest bounding box to the bottom-left canvas corner.

  • "bottom-right": Closest bounding box to the bottom-right canvas corner.

  • "top": Text region positioned highest vertically (minimum y).

  • "bottom": Text region positioned lowest vertically (maximum y).

  • "center": Closest text bounding box centroid to the image center.

  • "largest": Text region with the largest bounding box area.

pattern_match

Optional character string containing a regular expression. When provided, only recognized text strings matching this regex pattern are considered (e.g. "^G[0-9]+" or "^[A-Z]{2,4}-[0-9]{3}").

prefix

Character string to prepend to the new filename. Default is "".

suffix

Character string to append to the new filename before the extension. Default is "".

case

Character string specifying case transformation: "as_is" (default), "upper", or "lower".

sep

Character string used as separator between prefix, recognized text, and suffix. Default is "_".

min_chars

Integer. Minimum number of characters required for a recognized text to be considered valid. Default is 1L.

min_score

Numeric. Minimum recognition confidence score (0 to 1) required. Default is 0.35.

sanitize

Logical. Whether to remove illegal filesystem characters and normalize spaces. Default is TRUE.

keep_extension

Logical. Whether to preserve the original file extension. Default is TRUE.

copy

Logical. If TRUE, files are copied rather than renamed/moved, preserving original files. Default is FALSE.

output_dir

Optional character string specifying destination directory. If NULL (default), files are renamed/copied within their source directory.

conflict

Character string specifying how to resolve filename collisions:

  • "rename" (default): Append numeric suffix ("_1", "_2", etc.).

  • "skip": Do not rename or overwrite; skip the file.

  • "overwrite": Overwrite existing target file.

dry_run

Logical. If TRUE, simulates the entire OCR scanning and renaming process without modifying any files on disk, returning a preview table. Default is FALSE.

save_log

Logical. Whether to save an audit log file (.pliman_rename_log_*.rds) for subsequent recovery with image_rename_undo. Default is TRUE.

det_model

Text detection model backbone: "ppocrv5_det" (default), "ppocrv5_det_server", "ppocr_det", "ppocr_det_server", or "craft".

rec_model

Text recognition model backbone. Default is "ppocrv5_rec_latin" (see image_ocr_dl for the other options, e.g. "ppocrv5_rec" or "ppocr_rec").

det_thresh

Pixel binarization threshold for DBNet detection. Default is 0.30.

box_thresh

Bounding box confidence threshold. Default is 0.60.

unclip_ratio

Polygon expansion ratio. Default is 1.50.

text_threshold

CRAFT character threshold. Default is 0.70.

CRAFT link threshold. Default is 0.40.

low_text

CRAFT low text threshold. Default is 0.40.

engine

Compute engine: "gpu" (default) or "cpu".

threads

Number of CPU threads (0 = auto-detect).

dir_models

Directory where ONNX models are stored. Default is pliman_model_dir().

verbose

Logical. Whether to show informative progress messages. Default is TRUE.

...

Additional arguments passed to image_ocr_dl.

Value

An object of class c("image_rename_ocr", "data.frame") containing the audit log of all processed files with columns:

  • original_path: Full path to original file.

  • original_name: Original file basename.

  • detected_text: Text selected for renaming.

  • score: Recognition confidence score.

  • text_pos: Selection rule applied.

  • new_name: Final filename generated.

  • new_path: Full destination path.

  • status: Execution outcome ("renamed", "copied", "preview", "no_text_detected", "no_pattern_match", "skipped_conflict", "error").

  • action: Performed action ("rename" or "copy").

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

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

  # Dry-run preview in a folder of plot tag images
  preview <- image_rename_ocr(
    dir = "field_photos",
    text_pos = "top-left",
    pattern_match = "^G[0-9]+",
    dry_run = TRUE
  )
  print(preview)

  # Apply rename safely with audit log
  # res <- image_rename_ocr(dir = "field_photos", text_pos = "center")
  # Undo if needed:
  # image_rename_undo(res)
}