Skip to the content.
English 한국어 日本語 Tiếng Việt

R Example

https://github.com/bobhyun/TS-ANPR/tree/main/examples/R/anpr

1. Copying the Engine Files

[Note] In this example, the engine file is extracted to the examples/bin/ directory to share it with other examples. However, for actual deployment, the engine file is typically copied to the directory where the application’s executable file is located.

2. Prerequisites

  1. Install R and system dependencies

    Ubuntu/Debian:

    sudo apt-get update
    # Install R and development tools
    sudo apt-get install -y r-base r-base-dev
    # Install system libraries for R packages
    sudo apt-get install -y libcurl4-openssl-dev libmagick++-dev
    

    CentOS/RHEL/Fedora:

    # CentOS/RHEL 8+ / Fedora
    sudo dnf install -y R R-devel
    sudo dnf install -y libcurl-devel ImageMagick-c++-devel
    
    # CentOS/RHEL 7
    sudo yum install -y R R-devel
    sudo yum install -y libcurl-devel ImageMagick-c++-devel
    

    Windows:

    • Download R from https://cran.r-project.org/bin/windows/base/
    • Install Rtools from https://cran.r-project.org/bin/windows/Rtools/
  2. Install required R packages

    # Install R6 package (required)
    sudo Rscript -e 'install.packages("R6", repos="https://cloud.r-project.org")'
    
    # Install magick package (optional, for readPixelBuffer)
    sudo Rscript -e 'install.packages("magick", repos="https://cloud.r-project.org")'
    

    Note: On Windows, run the Rscript commands without sudo.

  3. Compile the C wrapper

    Linux/macOS:

    cd examples/R/anpr
    R CMD SHLIB src/tsanpr_r.c -o src/tsanpr_r.so
    

    Windows:

    cd examples/R/anpr
    R CMD SHLIB src/tsanpr_r.c -o src/tsanpr_r.dll
    

3. How to Run

cd examples/R/anpr

# Run the ANPR example
Rscript anpr.R

From R console:

setwd("examples/R/anpr")
source("anpr.R")

Interactive mode:

# Load the TSANPR wrapper
source("tsanpr.R")

# Initialize TSANPR
engine_path <- "../../bin/linux-x86_64/libtsanpr.so"  # Adjust for your platform
tsanpr <- TSANPR$new(engine_path)

# Initialize engine
tsanpr$anpr_initialize("text;country=KR")

# Process an image
result <- tsanpr$anpr_read_file("../../img/KR/licensePlate.jpg", "text", "")
print(result)

4. Features

5. API Reference

TSANPR Class

The TSANPR R6 class provides the following methods:

TSANPR <- R6Class("TSANPR",
  public = list(
    initialize = function(library_path),
    anpr_initialize = function(mode),
    anpr_read_file = function(img_file_name, output_format, options),
    anpr_read_pixels = function(pixels, width, height, stride, pixel_format, output_format, options),
    is_loaded = function()
  )
)

Recognition Options

Option Description
"" Single license plate recognition (default)
"vm" Recognize multiple license plates attached to vehicles
"vmb" Recognize multiple license plates (including motorcycles)
"vms" Recognize with surround detection
"dms" Detect multiple surrounding objects (vehicles)
"dmsr" Detect objects and recognize license plates
"dmsri<coords>" Recognize within Region of Interest

Output Formats

"text", "json", "yaml", "xml", "csv"

6. Implementation Notes

This example uses a C wrapper (src/tsanpr_r.c) to bridge R’s FFI with the TSANPR library:

Dynamic Library Loading:

R Integration:

The R code loads the compiled C wrapper, which in turn dynamically loads and calls the TSANPR library functions.

7. Troubleshooting

Compilation Issues:

Library Loading Issues:

Package Dependencies: