PPOCRLabel

repository·main·Indexed 19 days ago

https://github.com/pfcclab/ppocrlabel

A semi-automatic graphic annotation tool for OCR tasks written in Python3 and PyQT5. It features built-in PP-OCR models for automated detection and recognition, supporting regular text (rectangular boxes), tables, irregular text, and Key Information Extraction (KIE). The tool allows for precise bounding box vertex control and exports annotations directly compatible with PP-OCR detection and recognition model training.

Tokens
7.6K
Snippets
19
Records
33
Agent score
15%

What's inside PPOCRLabel

  1. What is PPOCRLabelv3

    main

    PPOCRLabelv3 is a semi-automatic graphic annotation tool designed for the OCR field. It features built-in PP-OCR models to automatically detect and re-recognize data. The tool is written in Python3 and PyQT5 and supports several annotation modes:

    • Regular text annotation: Rectangular box labeling.
    • Table annotation: Specialized mode for table structures.
    • Irregular text annotation: Multi-point labeling for non-rectangular text.
    • Key information annotation (KIE): For detection, recognition, and keyword extraction.

    Annotations produced can be used directly for training PP-OCR detection and recognition models.

  2. How PPOCRLabel handles unreadable text and legacy fields

    main

    Unreadable Text (### vs *)

    For the PaddleOCR detection training pipeline, use the transcription string ### or * to identify regions that should be ignored. For maximum compatibility across both Detection and Recognition fine-tuning stages, it is recommended to always use ### for unreadable text.

    The difficult field

    The exported Label.txt contains a difficult field. This is a legacy field from labelImg. In PPOCRLabel, this field is hardcoded to False for all exported labels to ensure backward compatibility with older data pipelines and prevent unintended data exclusion.

  3. Use custom OCR models in PPOCRLabel

    main

    By default, PPOCRLabel uses ultra-lightweight Chinese and English models. You can switch languages (French, German, Korean, Japanese) via the menu bar: PaddleOCR -> Choose OCR Model.

    To use your own custom inference models, you can pass the model directories and names as command-line arguments when starting the application.

    python PPOCRLabel.py --det_model_dir {your_det_model_dir} --det_model_name {your_det_model_name} --rec_model_dir {your_rec_model_dir} --rec_model_name {your_rec_model_name}
  4. Run PPOCRLabel from source (Python script)

    main

    If you have modified the PPOCRLabel source code (e.g., to use custom built-in models) or are performing secondary development, run the application directly using the Python script instead of the installed whl package.

    cd ./PPOCRLabel
    python PPOCRLabel.py --lang ch
  5. Divide dataset into train, validation, and test sets

    main

    Use the gen_ocr_train_val_test.py script to split your labeled dataset. This script expects a specific directory structure containing crop_img, Label.txt, and rec_gt.txt.

    cd ./PPOCRLabel
    python gen_ocr_train_val_test.py --trainValTestRatio 6:2:2 --datasetRootPath ../train_data
  6. Install and run PPOCRLabel via whl package

    main

    The easiest way to use PPOCRLabel is by installing the pre-built wheel package. This method automatically handles dependencies like paddleocr.

    Windows

    pip install PPOCRLabel

    Note: If you encounter [winError 126] 找不到指定模块 related to shapely, manually download and install the shapely wheel from Gohlke's Python libs.

    Ubuntu Linux

    pip3 install PPOCRLabel
    pip3 install trash-cli
    export QT_QPA_PALTFORM = wayland

    MacOS

    pip3 install PPOCRLabel
    pip3 install opencv-contrib-python-headless==4.2.0.32
    # Windows example
    pip install PPOCRLabel
  7. Annotate text information and locations

    main

    To annotate text content and bounding boxes for OCR training, follow these steps:

    1. Open Directory: Use 文件 (File) -> 打开目录 (Open Directory) to select the folder containing your images. PPOCRLabel operates on a folder basis.
    2. Auto-Label: Click 自动标注 (Auto-Label). This uses the PP-OCR lightweight model to automatically annotate images marked with status X (unprocessed).
    3. Manual Labeling:
      • Rectangular Annotation: Press W (in English mode) to draw bounding boxes for missed text.
      • Four-point Annotation: Press Q (or 编辑 -> 四点标注) to click 4 points, then double-click the left mouse button to complete.
    4. Confirm and Re-identify:
      • After drawing/adjusting boxes, click 确认 (Confirm). Boxes are initially assigned a "待识别" (to be identified) label.
      • Click 重新识别 (Re-identify) to run the PP-OCR model on all detected boxes in the current image. Note: This will overwrite previous manual text edits.
    5. Edit Content: Click on a recognition result to manually correct text.
      • Crucial: If text is unreadable or blurry, change the label to ###. This tells the PaddleOCR training pipeline to "ignore" this area, preventing negative impact on model fine-tuning.
    6. Finalize: Click 确认 (Confirm) to switch the image status to and move to the next image.

    Shortcuts for Precise Control

    When a bounding box is selected, use these keys to move individual vertices using arrow keys:

    • z: Move 1st vertex
    • x: Move 2nd vertex
    • c: Move 3rd vertex
    • v: Move 4th vertex
    • b: Return to normal whole-box movement
  8. Build and run a standalone executable with PyInstaller

    main

    To package PPOCRLabel into a standalone executable, follow these steps to regenerate resources and run the PyInstaller build command. This is useful for distributing the tool without requiring a Python environment.

    cd ./PPOCRLabel
    # 1. Install PyInstaller
    pip install pyinstaller
    
    # 2. Regenerate resources
    pyrcc5 -o libs/resources.py resources.qrc
    
    # 3. Package the executable
    pyinstaller -c PPOCRLabel.py --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb --collect-all paddle --hidden-import=pyqt5 -p ./libs -p ./ -p ./data -p ./resources -F
    
    # 4. Run the executable (Windows example)
    PPOCRLabel.exe --lang ch
  9. Annotate tables for structure extraction

    main

    To annotate tables for conversion to Excel format, follow this workflow involving both PPOCRLabel and Excel:

    1. Initialize Table Recognition: Open the table image and click Table Recognition (upper right corner). This calls the PP-Structure model to automatically label the table and opens an Excel file.
    2. Label Cells: Every cell containing text must be marked with a bounding box. Right-click a box and select Cell Re-recognition to use the model for text within that cell.
      • Note: Even blank cells must be marked with a bounding box to maintain the correct total cell count.
    3. Define Structure in Excel: In the popped-up Excel file, mark each cell containing text with an identifier (e.g., 1) to match the merged cell structure of the original image.
    4. Adjust Cell Order: To ensure cells are ordered correctly (left-to-right, top-to-bottom):
      • Go to View -> Show Box Number to display ordinal numbers.
      • Drag the results in the Recognition Results column on the right side of the interface to match the correct sequence.
    5. Export: Close all Excel files, then click File -> Export Table Label to obtain gt.txt annotation results.
  10. Export OCR labeling results

    main

    PPOCRLabel exports data in two ways:

    Manual Export

    Use the menu File -> Export Label to manually export detection results. The results are stored in Label.txt within the opened picture folder.

    Automatic Export

    If File -> Auto export Label Mode is selected, the program exports automatically. Manual checks are stored in Label.txt.

    Recognition Training Data Export

    To export data specifically for recognition model training:

    1. Click File -> Export Recognition Results in the menu bar.
    2. The recognition training data (cropped images) will be saved in the crop_img folder.
    3. The recognition labels will be saved in rec_gt.txt.
  11. Run PPOCRLabel via Python Script

    main

    If you have modified the source code (e.g., to specify new built-in models), run the application directly using the Python script instead of the whl package.

    cd ./PPOCRLabel
    
    # Normal mode
    python PPOCRLabel.py
    
    # KIE mode
    python PPOCRLabel.py --kie True
    python PPOCRLabel.py --kie True
  12. Package PPOCRLabel with Pyinstaller

    main

    To package the application into a standalone executable using Pyinstaller, follow these steps:

    1. Install Pyinstaller.
    2. Regenerate the resources.
    3. Run the Pyinstaller command with necessary collection flags for dependencies.
    cd ./PPOCRLabel
    pip install pyinstaller
    
    # Regenerate Resources
    pyrcc5 -o libs/resources.py resources.qrc
    
    # Packaging executable programs
    pyinstaller -c PPOCRLabel.py --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb --collect-all paddle --hidden-import=pyqt5  -p ./libs -p ./ -p ./data -p ./resources -F

    After building, you can run the executable (e.g., PPOCRLabel.exe --lang ch on Windows) from the dist directory.

    pyrcc5 -o libs/resources.py resources.qrc