EasyOCR

repository·master·Indexed 12 days ago

https://github.com/jaidedai/easyocr

A ready-to-use OCR library built on PyTorch supporting over 80 languages and various writing scripts. It utilizes the CRAFT algorithm for text detection and a CRNN-based model for recognition, with optional support for DBNet text detection via Deformable Convolutional Networks (DCN) operators.

Tokens
5.2K
Snippets
15
Records
28
Agent score
98%

What's inside EasyOCR

  1. Overview of DBNet in EasyOCR

    master

    DBNet is a text detection module adapted from DBNet++. It performs image segmentation at the pixel level, classifying whether each pixel belongs to a text region.

    Key Concepts:

    • Probability Heatmap: A tensor representing the classification confidence of each pixel.
    • Segmentation: A boolean-like tensor representing the determined text regions.
    • text_threshold: The threshold applied to the probability heatmap to define text regions.
    • detection_size: The dimensions used for the detection routine. Input images are resized to this size (and must have width/height as multiples of 32) if they do not match.

    Configuration:

    • The module uses dynamic import and class construction via config files located in ./configs/.
    • Minimum and maximum image sizes can be specified in these config files.
  2. Understand the EasyOCR output format

    master

    When detail=1 (the default), reader.readtext() returns a list of tuples. Each tuple contains:

    1. Bounding Box: A list of four [x, y] coordinates representing the corners of the detected text area.
    2. Detected Text: The string of text recognized.
    3. Confidence Level: A float representing the model's confidence in the detection.
    [([[189, 75], [469, 75], [469, 165], [189, 165]], '愚园路', 0.3754989504814148), ...]
  3. Train a custom recognition model

    master

    To train a custom recognition model for EasyOCR, you must use a fully convolutional network to support flexible text length prediction. The recommended architecture is 'None-VGG-BiLSTM-CTC'.

    Workflow:

    1. Dataset Generation: Use tools like TextRecognitionDataGenerator to create your dataset.
    2. Training: Use the deep-text-recognition-benchmark repository or the modified version provided in the EasyOCR trainer directory.
    3. Required Outputs: Training must produce three specific files with the same base name:
      • A .pth file (the trained model weights).
      • A .py file (describing the recognition network architecture).
      • A .yaml file (describing the model configuration).
  4. Compile DCN Operators Just-in-Time (JiT)

    master
    The easiest way to use DBNet is via the Just-in-Time (JiT) approach. Once prerequisites are installed, simply set dbnet18 as the detect_network for EasyOCR. The module will automatically compile the source code when needed during the first session. Note that the initial compilation may take some time.
  5. Train custom models with EasyOCR trainer

    master

    To train a custom model, use the provided Jupyter Notebook trainer.ipynb. The training process is driven by YAML configuration files that must be placed in the config_files directory. This setup allows you to define training parameters and dataset paths via YAML to customize the EasyOCR recognition models.

    # Ensure your configuration is placed in the correct directory
    # Move your custom yaml config to:
    # trainer/config_files/your_config.yaml
    
    # Then run the training notebook:
    # trainer.ipynb
  6. Install EasyOCR via pip

    master

    You can install the latest stable release of EasyOCR using pip.

    Windows Users: You must install torch and torchvision first via the official PyTorch instructions. Ensure you select the correct CUDA version for your system. If you only want to use the CPU, select CUDA = None.

    To install the latest development release, use the git URL.

    # Latest stable release
    pip install easyocr
    
    # Latest development release
    pip install git+https://github.com/JaidedAI/EasyOCR.git
  7. Use EasyOCR for text recognition in Python

    master

    To use EasyOCR, first initialize an easyocr.Reader with a list of target languages. This step loads the models into memory and should only be performed once. Then, use the .readtext() method on an image.

    Supported Input Types:

    • Filepath (string)
    • OpenCV image object (numpy array)
    • Image file as bytes
    • URL to a raw image

    Configuration Options:

    • gpu=False: Pass this to the Reader constructor to run in CPU-only mode if you do not have a GPU or have low GPU memory.
    • detail=0: Pass this to .readtext() to receive only the detected text strings, omitting bounding boxes and confidence levels.
    import easyocr
    
    # Initialize the reader (run once to load models)
    # Example for Simplified Chinese and English
    reader = easyocr.Reader(['ch_sim','en'], gpu=False)
    
    # Perform OCR with full details
    # Returns: [([[x,y],...], 'text', confidence), ...]
    result = reader.readtext('chinese.jpg')
    
    # Perform OCR with simplified output (text only)
    result_simple = reader.readtext('chinese.jpg', detail=0)
  8. Train CRAFT from scratch with SynthText

    master

    To train the CRAFT model from scratch using the SynthText dataset, use the trainSynth.py script. You must provide a configuration file name via the --yaml argument.

    Note: You can skip this step if you use a pre-trained checkpoint. To use a checkpoint, download it, place it in exp/CRAFT_clr_amp_29500.pth, and update the ckpt_path in your configuration file.

    CUDA_VISIBLE_DEVICES=0 python3 trainSynth.py --yaml=syn_train
  9. Prerequisites for DBNet DCN Operators

    master

    DBNet requires Deformable Convolutional Networks (DCN) operators to be compiled. You can choose between a CPU version (works without GPU/CUDA) or a CUDA version (significantly faster, requires CUDA-support GPU and CUDA developer toolkit).

    Requirements:

    For CPU version:

    • GCC compiler > 4.9

    For CUDA version:

    • GCC compiler > 4.9
    • CUDA Developer Toolkits > 9.0 (Tested on 11.3)

    Note: EasyOCR can function without DBNet and DCN operators by using the default CRAFT text detection module.

  10. Install GCC, CUDA, and Conda Dependencies

    master

    Follow these steps to prepare your environment for DCN compilation:

    Installing GCC (Linux/Debian/Ubuntu)

    sudo apt-get install build-essential

    Installing CUDA and NVCC via Conda

    If you use Conda, you can install the NVCC compiler using:

    conda install -c conda-forge cudatoolkit-dev

    Using Docker

    If using Docker, use development-level images (e.g., pytorch/pytorch:x.xx.x-cudax.x-cudnnx-devel) and verify installation with:

    gcc --version
    nvcc --version
  11. Train CRAFT with custom or combined datasets

    master

    To train CRAFT using a combination of datasets (e.g., SynthText + IC15) or your own custom dataset, use train.py for single-GPU setups or train_distributed.py for multi-GPU setups.

    Ensure your configuration .yaml file is placed in the config folder. To improve training speed on multi-GPU systems, ensure num_worker > 0 is set in your configuration.

    # Single GPU
    CUDA_VISIBLE_DEVICES=0 python3 train.py --yaml=custom_data_train
    
    # Multi GPU
    CUDA_VISIBLE_DEVICES=0,1 python3 train_distributed.py --yaml=custom_data_train