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

Crystal example

https://github.com/bobhyun/TS-ANPR/tree/main/examples/Crystal/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 Crystal (version 1.0.0 or later recommended)

    Windows:

    # Using Scoop
    scoop install crystal
    
    # Or download from https://crystal-lang.org/install/on_windows/
    

    Linux:

    # Ubuntu/Debian
    curl -fsSL https://crystal-lang.org/install.sh | sudo bash
    
    # Or using package manager
    sudo apt-get install crystal
    
  2. Verify installation

    crystal --version
    

3. How to Run

  1. Navigate to the Crystal example directory

    cd Crystal/anpr
    
  2. Install dependencies

    # Install required shards (StumpyPNG and StumpyJPEG for image decoding)
    shards install
    

    Windows Note: Shards requires symlinks. If you encounter a symlink error, you have two options:

    • Option 1 (Recommended): Enable Developer Mode in Windows Settings
      • Go to Settings → Privacy & Security → For developers → Developer Mode → ON
      • See: https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development
    • Option 2: Run PowerShell or Command Prompt as Administrator
      # Run as Administrator
      shards install
      
  3. Run the example

    Using Shards (recommended):

    # Build and run
    shards build
    ./bin/anpr
    
    # Build with optimizations
    shards build --release
    

    Direct compilation:

    # Run directly
    crystal run anpr.cr
    
    # Or compile and run
    crystal build anpr.cr
    ./anpr
    
    # Compile with optimizations
    crystal build --release anpr.cr
    

4. Notes

5. Features

6. Image Processing Methods

This example demonstrates three different ways to process images:

  1. read_image_file: Direct file processing using anpr_read_file (fastest method)
  2. read_encoded_image: Pass encoded image bytes with pixel format “encoded”
  3. read_pixel_buffer: Decode image to raw RGB pixel data using StumpyPNG/StumpyJPEG, then pass to anpr_read_pixels
    • PNG files: Decoded using StumpyPNG
    • JPEG files: Decoded using StumpyJPEG, with automatic fallback to encoded format if decoding fails

To switch between methods, modify the anpr_func variable in the code:

# Choose one of the following:
anpr_func = ->read_image_file(TSANPR, String, String, String)
# anpr_func = ->read_encoded_image(TSANPR, String, String, String)
# anpr_func = ->read_pixel_buffer(TSANPR, String, String, String)

Note: The read_pixel_buffer function automatically handles JPEG decoding failures by falling back to encoded format, ensuring robust operation across different JPEG variants.

7. API Reference

TSANPR Class

The TSANPR class provides the following methods:

Constructor:

Core Methods:

Recognition Options

Output Formats