RapidOCR Documentation

repository·main·Indexed 27 days ago

https://github.com/rapidai/rapidocr

An open-source, high-speed OCR tool designed for multi-platform and multi-language deployment. RapidOCR converts PaddleOCR models into ONNX format for efficient inference across various devices. It supports multiple inference engines including ONNX Runtime (CPU/GPU), NVIDIA TensorRT, PaddlePaddle, Intel OpenVINO, PyTorch, and MNN. The library provides a Python package installable via pip, as well as Docker environments for development and deployment.

Tokens
9K
Snippets
19
Records
65
Agent score
92%

What's inside RapidOCR

  1. Configure code formatting with pre-commit hooks

    main

    RapidOCR uses pre-commit to automatically run code formatters like black and autoflake before commits. This ensures code style consistency.

    1. Install pre-commit within your activated virtual environment in the python directory.
    2. Install the Git hooks at the repository root.

    Commands:

    # Inside the 'python' directory with virtualenv activated
    pip install pre-commit
    
    # Return to the repository root to install hooks
    cd ..
    pre-commit install

    To manually run the checks on all files before committing, run this from the repository root:

    pre-commit run --all-files
    pip install pre-commit
    cd ..
    pre-commit install
    pre-commit run --all-files
  2. Use RapidOCRCSharp for .NET integration

    main
    For .NET developers looking to integrate RapidOCR, use the RapidOCRCSharp project. Detailed documentation, installation instructions, and usage patterns for the C# wrapper can be found in its dedicated repository.
  3. Build and test RapidOCR with Docker

    main

    Docker environments are available for various inference engines. You can build and test using ONNX Runtime (CPU) or use other engines like tensorrt, paddle, openvino, pytorch, or mnn.

    # Build and test with ONNX Runtime (CPU)
    make build-onnxruntime-cpu
    make test-onnxruntime-cpu
    
    # Or use any engine: onnxruntime-gpu, tensorrt, paddle, openvino, pytorch, mnn
    make build-tensorrt
    make shell-tensorrt
  4. Write unit tests for RapidOCR

    main

    New tests should be placed in python/tests/ with the naming convention test_*.py. Use pytest and place any required test assets (like images) in python/tests/test_files/.

    When writing tests, ensure they reliably verify changes and avoid depending on undocumented external services (use mocks where necessary).

    # tests/test_xxx.py
    import pytest
    from pathlib import Path
    
    root_dir = Path(__file__).resolve().parent.parent
    tests_dir = root_dir / "tests" / "test_files"
    
    @pytest.fixture()
    def engine():
        from rapidocr import RapidOCR
        return RapidOCR()
    
    def test_your_new_feature(engine):
        img_path = tests_dir / "ch_en_num.jpg"
        result = engine(img_path)
        assert result is not None
        # more assertions...
  5. Run unit tests with pytest

    main

    Tests should be executed from the python directory. You can run all tests, specific files, or check coverage.

    Commands:

    # Run all tests
    pytest tests/ -v
    
    # Run specific test files
    pytest tests/test_input.py -v
    pytest tests/test_det_cls_rec.py -v
    
    # Check test coverage (requires pytest-cov)
    pytest tests/ -v --cov=rapidocr
    pytest tests/ -v
  6. Use Docker Compose directly for RapidOCR

    main

    If you prefer not to use the Makefile, you can use docker compose targeting the docker/docker-compose.yaml file.

    • Build: docker compose -f docker/docker-compose.yaml build <service>
    • Run tests: docker compose -f docker/docker-compose.yaml run --rm <service> pytest tests/ -v
    • Run a single test: docker compose -f docker/docker-compose.yaml run --rm <service> pytest tests/test_engine.py -k "<engine_name>" -v
    • Interactive shell: docker compose -f docker/docker-compose.yaml run --rm <service> bash

    Note: Replace <service> with the engine name, e.g., onnxruntime-cpu.

  7. Write and structure new unit tests

    main

    When adding new features or fixing bugs, you must include corresponding unit tests.

    • Location: Place test files in python/tests/ with the naming convention test_*.py.
    • Resources: Place images or other test assets in python/tests/test_files/.
    • Framework: Use pytest.

    Example Test Structure:

    # tests/test_xxx.py
    import pytest
    from pathlib import Path
    
    root_dir = Path(__file__).resolve().parent.parent
    tests_dir = root_dir / "tests" / "test_files"
    
    @pytest.fixture()
    def engine():
        from rapidocr import RapidOCR
        return RapidOCR()
    
    def test_your_new_feature(engine):
        img_path = tests_dir / "ch_en_num.jpg"
        result = engine(img_path)
        assert result is not None
        # Add more assertions...
    import pytest
    from pathlib import Path
    
    root_dir = Path(__file__).resolve().parent.parent
    tests_dir = root_dir / "tests" / "test_files"
    
    @pytest.fixture()
    def engine():
        from rapidocr import RapidOCR
        return RapidOCR()
    
    def test_your_new_feature(engine):
        img_path = tests_dir / "ch_en_num.jpg"
        result = engine(img_path)
        assert result is not None
  8. Set up the RapidOCR Python development environment

    main

    To contribute to the Python portion of RapidOCR, clone the repository and configure a virtual environment within the python directory.

    Prerequisites:

    • Python >= 3.6 (3.8+ recommended)
    • Git
    • GitHub account

    Setup Steps:

    1. Clone the repository:
      git clone https://github.com/RapidAI/RapidOCR.git
      cd RapidOCR
    2. Navigate to the Python directory:
      cd python
    3. Create and activate a virtual environment (using venv or conda):
      # Using venv
      python -m venv .venv
      source .venv/bin/activate   # Linux/macOS
      # .venv\Scripts\activate    # Windows
      
      # Using conda
      conda create -n rapidocr python=3.10
      conda activate rapidocr
    4. Install dependencies and pytest for testing. It is recommended to install the current package in editable mode (-e) to ensure local changes take effect immediately:
      pip install -r requirements.txt
      pip install pytest
      pip install -e .
    git clone https://github.com/RapidAI/RapidOCR.git
    cd RapidOCR
    cd python
    python -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
    pip install pytest
    pip install -e .