### Install RapidOCR and ONNX Runtime Source: https://github.com/rapidai/rapidocr/blob/main/README.md Install the RapidOCR library along with the ONNX Runtime for efficient model inference. This is the primary installation command. ```bash pip install rapidocr onnxruntime ``` -------------------------------- ### Install paddleocr_convert Source: https://github.com/rapidai/rapidocr/wiki/support_language Install the paddleocr_convert library using pip. This is the first step for offline model conversion. ```bash pip install paddleocr_convert ``` -------------------------------- ### Install RapidOCR ONNX Runtime Source: https://github.com/rapidai/rapidocr/wiki/support_language Install the rapidocr_onnxruntime library, which is required to use ONNX models with RapidOCR. ```bash pip install rapidocr_onnxruntime ``` -------------------------------- ### Install and Use Pre-commit Hooks Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Install pre-commit to automatically format and check code before commits. Run pre-commit hooks manually to check all files. ```bash # From the python directory with your venv activated pip install pre-commit # Go to the repository root to install Git hooks (.pre-commit-config.yaml is in the root) cd .. # if you are in python/, go back to the repo root pre-commit install # From the repository root pre-commit run --all-files ``` -------------------------------- ### Set Up Python Development Environment Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Navigate to the python directory and set up a virtual environment using venv or conda. Install project dependencies and pytest for testing. ```bash cd python # Using venv python -m venv .venv source .venv/bin/activate # Linux/macOS # .venv\Scripts\activate # Windows # Or using conda conda create -n rapidocr python=3.10 conda activate rapidocr pip install -r requirements.txt pip install pytest # required to run tests # Optional: install the package in editable mode for debugging pip install -e . ``` -------------------------------- ### Install RapidOCR and ONNX Runtime Source: https://github.com/rapidai/rapidocr/blob/main/assets/RapidOCRDemo.ipynb Installs the RapidOCR library and its dependency ONNX Runtime using pip. Ensure you have pip installed and a compatible Python version. ```python !pip install rapidocr onnxruntime ``` -------------------------------- ### Install Nuitka Source: https://github.com/rapidai/rapidocr/wiki/Nuitka打包rapidocr_web操作文档 Install the Nuitka compiler using pip. This command will automatically download necessary components like mingw and ccache. ```bash pip install nuitka ``` -------------------------------- ### Verify NVIDIA Driver Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Run this command to check if the NVIDIA driver is correctly installed and accessible on the host system. ```bash nvidia-smi ``` -------------------------------- ### Clone the RapidOCR Repository Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Clone the main RapidOCR repository to your local machine. Navigate into the cloned directory to proceed with setup. ```bash git clone https://github.com/RapidAI/RapidOCR.git cd RapidOCR ``` -------------------------------- ### Write Unit Tests for RapidOCR Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Example of writing a unit test for a new feature in RapidOCR. Tests should be placed in `python/tests/` and use pytest. ```python # 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... ``` -------------------------------- ### RapidOCR Output Example Source: https://github.com/rapidai/rapidocr/blob/main/assets/RapidOCRDemo.ipynb This snippet displays the output structure of RapidOCR, including the processed image and detected bounding boxes. It's useful for understanding the raw results before further processing. ```python Output: RapidOCROutput(img=array([[[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [255, 255, 255], [255, 255, 255], [255, 255, 255]], [[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [255, 255, 255], [255, 255, 255], [255, 255, 255]], [[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [255, 255, 255], [255, 255, 255], [255, 255, 255]], ..., [[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [254, 254, 254], [254, 254, 254], [254, 254, 254]], [[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [254, 254, 254], [254, 254, 254], [254, 254, 254]], [[255, 255, 255], [255, 255, 255], [255, 255, 255], ..., [254, 254, 254], [254, 254, 254], [254, 254, 254]]], dtype=uint8), boxes=array([[[ 71., 363.], [419., 363.], [419., 382.], [ 71., 382.]], [[ 71., 392.], [410., 392.], [410., 412.], [ 71., 412.]], [[185., 466.], [317., 466.], [317., 488.], ``` -------------------------------- ### Open an interactive shell in ONNX Runtime CPU image Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Starts an interactive bash shell inside the ONNX Runtime CPU Docker container. Run this command from the repository root. ```bash make shell-onnxruntime-cpu ``` -------------------------------- ### List Docker Volumes Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Check if the 'rapidocr-models' Docker volume is created and available. This is crucial for ensuring models are not re-downloaded on each container start. ```bash docker volume ls | grep rapidocr ``` -------------------------------- ### Conventional Commits Type Examples Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Examples of common types used in Conventional Commits, such as 'feat' for new features and 'fix' for bug fixes. ```markdown Common types: | Type | Description | |------------|--------------------------------| | `feat` | New feature | | `fix` | Bug fix | | `docs` | Documentation changes | | `style` | Code style (no logic change) | | `refactor` | Refactoring | | `test` | Tests | | `chore` | Build / tooling, etc. | Examples: `fix(python): empty result under certain conditions`, `feat(python): support xxx input format`. ``` -------------------------------- ### Open an interactive shell using Docker Compose Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Starts an interactive bash shell within the specified Docker service (e.g., onnxruntime-cpu) using Docker Compose. ```bash docker compose -f docker/docker-compose.yaml run --rm onnxruntime-cpu bash ``` -------------------------------- ### Open an interactive shell in TensorRT Docker image Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Starts an interactive bash shell inside the TensorRT Docker container. Run this command from the repository root. ```bash make shell-tensorrt ``` -------------------------------- ### Basic Python Usage of RapidOCR Source: https://github.com/rapidai/rapidocr/blob/main/README.md Demonstrates how to initialize RapidOCR, process an image from a URL, print the results, and visualize them. ```python from rapidocr import RapidOCR engine = RapidOCR() img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/test_files/ch_en_num.jpg" result = engine(img_url) print(result) result.vis("vis_result.jpg") ``` -------------------------------- ### Build all Docker images Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Builds all available pre-configured Docker images for RapidOCR. Run this command from the repository root. ```bash make build-all ``` -------------------------------- ### Verify GPU access with NVIDIA Container Toolkit Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Runs a container with all GPUs enabled to verify that the NVIDIA Container Toolkit is correctly set up and GPUs are accessible. This is a prerequisite for GPU images. ```bash docker run --rm --gpus all nvidia/cuda:12.4.1-base-ubuntu22.04 nvidia-smi ``` -------------------------------- ### Build an ONNX Runtime CPU image Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Builds a Docker image for developing with ONNX Runtime on CPU. Run this command from the repository root. ```bash make build-onnxruntime-cpu ``` -------------------------------- ### Docker Build and Test Commands Source: https://github.com/rapidai/rapidocr/blob/main/README.md Provides commands to build and test RapidOCR with Docker using ONNX Runtime (CPU) or other specified engines like TensorRT. ```bash # 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 ``` -------------------------------- ### Run OCR from command line using ONNX Runtime CPU Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Opens an interactive shell in the ONNX Runtime CPU container and then runs an OCR process on a sample image using the RapidOCR class. The local 'python/' directory is mounted, allowing code changes to be reflected instantly. ```bash make shell-onnxruntime-cpu # Inside container: python -c " from rapidocr import RapidOCR engine = RapidOCR() result = engine('tests/test_files/ch_en_num.jpg') print(result) " ``` -------------------------------- ### Running Pre-commit Hooks Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Command to run all pre-commit hooks on all files in the repository. This ensures code style and formatting consistency. ```bash pre-commit run --all-files ``` -------------------------------- ### Run OCR with PyTorch engine Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Opens an interactive shell in the PyTorch Docker container and runs an OCR process, explicitly specifying PyTorch as the engine type for detection, classification, and recognition. This demonstrates how to select a specific inference engine. ```bash make shell-pytorch # Inside container: python -c " from rapidocr import RapidOCR, EngineType engine = RapidOCR(params={ 'Det.engine_type': EngineType.TORCH, 'Cls.engine_type': EngineType.TORCH, 'Rec.engine_type': EngineType.TORCH, }) result = engine('tests/test_files/ch_en_num.jpg') print(result) " ``` -------------------------------- ### Initialize RapidOCR Engine and Perform OCR Source: https://github.com/rapidai/rapidocr/blob/main/assets/RapidOCRDemo.ipynb Initializes the RapidOCR engine and performs OCR on an image from a URL. It can return bounding boxes for words and individual characters. The result can be visualized and saved to a file. ```python from rapidocr import RapidOCR engine = RapidOCR() img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516" result = engine(img_url, return_word_box=True, return_single_char_box=True) print(result) result.vis("vis_result.jpg") ``` -------------------------------- ### Build ONNX Runtime CPU image using Docker Compose Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Builds the ONNX Runtime CPU Docker image using Docker Compose. Ensure you are in the repository root. ```bash docker compose -f docker/docker-compose.yaml build onnxruntime-cpu ``` -------------------------------- ### Command-line conversion: Online recognition model Source: https://github.com/rapidai/rapidocr/wiki/support_language Convert an online PaddleOCR recognition model to ONNX format. This also requires providing the path to the language key file. ```bash $ paddleocr_convert -p https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar \ -o models \ -txt_path https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt ``` -------------------------------- ### Run a specific test file using Docker Compose Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Runs a specific test file (e.g., test_engine.py) with a particular test case (e.g., test_ppocrv5_rec_mobile) within the specified Docker service using Docker Compose. ```bash docker compose -f docker/docker-compose.yaml run --rm onnxruntime-cpu \ pytest tests/test_engine.py::test_ppocrv5_rec_mobile -v ``` -------------------------------- ### Build TensorRT Docker image Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Builds the Docker image for the TensorRT engine. Run this command from the repository root. ```bash make build-tensorrt ``` -------------------------------- ### Use custom ONNX detection and recognition models Source: https://github.com/rapidai/rapidocr/wiki/support_language Initialize RapidOCR with both custom ONNX detection and recognition models. Provide the file paths for both `det_model_path` and `rec_model_path` parameters. ```python from rapidocr_onnxruntime import RapidOCR model_path = 'local/models/japan.onnx' engine = RapidOCR(rec_model_path=model_path, det_model_path='xxx.onnx') img = '1.jpg' result, elapse = engine(img) ``` -------------------------------- ### Git Workflow for Contributions Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Standard Git commands for forking, branching, staging, committing, and pushing changes to your fork. Ensure your commit messages follow the Conventional Commits specification. ```bash git remote add myfork https://github.com/YOUR_USERNAME/RapidOCR.git git checkout -b fix/xxx git add python/ git status git commit -m "fix(python): short description" git push myfork fix/xxx ``` -------------------------------- ### Run tests for ONNX Runtime CPU image Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Executes tests within the ONNX Runtime CPU Docker image. Run this command from the repository root. ```bash make test-onnxruntime-cpu ``` -------------------------------- ### Pull Request Title and Description Guidance Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Recommendations for crafting effective pull request titles and descriptions, including referencing issues and providing verification steps. ```markdown - **Title**: Short summary (e.g. “Fix: xxx in Python”). - **Description** should include: - Related issue: `Fixes #123` or `Related to #123` if applicable. - Reason for the change and what was done. - How to verify (e.g. “`pytest tests/ -v` in the python directory passes”). ``` -------------------------------- ### Run Unit Tests with Pytest Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md Execute unit tests from the python directory using pytest. Options include running all tests, specific files, or with code coverage. ```bash # Run all tests pytest tests/ -v # Run specific test files pytest tests/test_input.py -v pytest tests/test_det_cls_rec.py -v # Run with coverage (requires pytest-cov) pytest tests/ -v --cov=rapidocr ``` -------------------------------- ### Command-line conversion: Online detection model Source: https://github.com/rapidai/rapidocr/wiki/support_language Convert an online PaddleOCR detection model to ONNX format using the paddleocr_convert command-line tool. The converted model is saved to the specified directory. ```bash $ paddleocr_convert -p https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar \ -o models ``` -------------------------------- ### Use custom ONNX recognition model Source: https://github.com/rapidai/rapidocr/wiki/support_language Load and use a custom ONNX recognition model with RapidOCR. Specify the path to the converted ONNX model file when initializing the RapidOCR engine. ```python from rapidocr_onnxruntime import RapidOCR model_path = 'local/models/japan.onnx' engine = RapidOCR(rec_model_path=model_path) img = '1.jpg' result, elapse = engine(img) ``` -------------------------------- ### Command-line conversion: Offline recognition model Source: https://github.com/rapidai/rapidocr/wiki/support_language Convert a locally stored PaddleOCR recognition model to ONNX format. This requires specifying both the model path and the path to the language key file. ```bash $ paddleocr_convert -p models/ch_PP-OCRv3_rec_infer.tar \ -o models \ -txt_path models/ppocr_keys_v1.txt ``` -------------------------------- ### Run tests for TensorRT Docker image Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Executes tests within the TensorRT Docker image. Run this command from the repository root. ```bash make test-tensorrt ``` -------------------------------- ### Command-line use: Custom ONNX recognition model Source: https://github.com/rapidai/rapidocr/wiki/support_language Use a custom ONNX recognition model from the command line with RapidOCR. The `--rec_model_path` argument specifies the path to the converted ONNX model. ```bash $ rapidocr_onnxruntime -img 1.jpg --rec_model_path local/models/japan.onnx ``` -------------------------------- ### Nuitka Compilation with Console Disabled Source: https://github.com/rapidai/rapidocr/wiki/Nuitka打包rapidocr_web操作文档 Compile the ocrweb.py application into a standalone executable using Nuitka, disabling the console window for a cleaner user experience. This command includes options to show memory usage, progress, and exclude tkinter imports. ```bash nuitka --mingw64 --standalone --show-memory --show-progress --nofollow-import-to=tkinter --windows-disable-console --output-dir=out ocrweb.py ``` -------------------------------- ### Nuitka Standalone Compilation Source: https://github.com/rapidai/rapidocr/wiki/Nuitka打包rapidocr_web操作文档 Compile the ocrweb.py application into a standalone executable using Nuitka with the MinGW64 compiler. This command includes options to show memory usage, progress, and exclude tkinter imports. ```bash cd rapidocr_web nuitka --mingw64 --standalone --show-memory --show-progress --nofollow-import-to=tkinter --output-dir=out ocrweb.py ``` -------------------------------- ### Run tests using Docker Compose Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Executes tests within the specified Docker service (e.g., onnxruntime-cpu) using Docker Compose. The --rm flag cleans up the container after execution. ```bash docker compose -f docker/docker-compose.yaml run --rm onnxruntime-cpu pytest tests/ -v ``` -------------------------------- ### Script conversion: Online recognition model Source: https://github.com/rapidai/rapidocr/wiki/support_language Convert an online PaddleOCR recognition model to ONNX format using the PaddleOCRModelConvert class. This method allows programmatic conversion and requires specifying the model URL and the key file URL. ```python from paddleocr_convert import PaddleOCRModelConvert converter = PaddleOCRModelConvert() save_dir = 'models' # online url = 'https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar' txt_url = 'https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/ppocr_keys_v1.txt' converter(url, save_dir, txt_path=txt_url) ``` -------------------------------- ### Command-line conversion: Offline detection model Source: https://github.com/rapidai/rapidocr/wiki/support_language Convert a locally stored PaddleOCR detection model to ONNX format using the paddleocr_convert command-line tool. The model is read from the specified path and saved to the output directory. ```bash $ paddleocr_convert -p models/ch_PP-OCRv3_det_infer.tar \ -o models ``` -------------------------------- ### Add Fork as Remote and Push Changes Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md If you cloned the upstream repository, add your personal fork as a remote. Then, push your committed changes to your fork's branch. ```bash git remote add YOUR_USERNAME https://github.com/YOUR_USERNAME/RapidOCR.git git push YOUR_USERNAME YOUR_BRANCH_NAME ``` -------------------------------- ### Clean up Docker build artifacts Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Removes Docker build artifacts. Run this command from the repository root. ```bash make clean ``` -------------------------------- ### BibTeX Citation for RapidOCR Source: https://github.com/rapidai/rapidocr/blob/main/README.md Use this BibTeX entry to cite the RapidOCR project in your academic research. It includes the title, authors, publication method, and year. ```bibtex @misc{RapidOCR 2021, title={{Rapid OCR}: OCR Toolbox}, author={RapidAI Team}, howpublished = {\url{https://github.com/RapidAI/RapidOCR}}, year={2021} } ``` -------------------------------- ### Script conversion: Offline recognition model Source: https://github.com/rapidai/rapidocr/wiki/support_language Convert a locally stored PaddleOCR recognition model to ONNX format using the PaddleOCRModelConvert class. This method requires specifying the local model path and the local key file path. ```python from paddleocr_convert import PaddleOCRModelConvert converter = PaddleOCRModelConvert() save_dir = 'models' model_path = 'models/ch_PP-OCRv3_rec_infer.tar' txt_path = 'models/ppocr_keys_v1.txt' converter(model_path, save_dir, txt_path=txt_path) ``` -------------------------------- ### Conventional Commits Specification Source: https://github.com/rapidai/rapidocr/blob/main/docs/CONTRIBUTING.md The required format for commit messages to facilitate automated changelog generation. Includes common types like 'feat', 'fix', 'docs', etc. ```markdown [optional scope]: [optional body] [optional footer] ``` -------------------------------- ### Clear Docker model cache volume Source: https://github.com/rapidai/rapidocr/blob/main/docker/README.md Removes the Docker volume named 'rapidocr-models' which stores cached OCR models. This forces models to be re-downloaded on next use. ```bash docker volume rm rapidocr-models ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.