ONNX-based OCR (PP-OCRv5)

0.0.14 · active · verified Thu Apr 16

`onnxocr-ppocrv5` is a Python library providing an ONNX-based inference pipeline for Baidu's PP-OCRv5. It aims for efficient optical character recognition by leveraging ONNX Runtime for high-performance inference. The current version is 0.0.14, and releases appear to be infrequent, typically focusing on specific model updates or bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `OnnxOCR` model and perform OCR on a local image file. The model automatically downloads necessary ONNX files on the first run, which requires an internet connection and sufficient disk space.

import os
from onnxocr_ppocrv5 import OnnxOCR

# NOTE: Replace with a path to your actual image file
# For demonstration, we assume an image 'example.png' exists in the current directory
# In a real application, you might download or provide an actual image path.
image_path = os.environ.get('OCR_IMAGE_PATH', 'example.png')

# The first initialization will automatically download necessary models (~300-500 MB)
# This requires an internet connection and can take some time.
print("Initializing OnnxOCR model... (models will download on first run)")
ocr = OnnxOCR()
print("OCR model initialized.")

# Perform OCR on an image file path
# Ensure the image_path points to a valid image accessible by the script.
if os.path.exists(image_path):
    try:
        result = ocr(image_path)
        print("OCR Result:")
        for box_info in result:
            # Each box_info is a dictionary with 'box' and 'text'
            print(f"  Text: {box_info['text']}, Box: {box_info['box']}")
    except Exception as e:
        print(f"Error performing OCR: {e}")
        print("Please ensure the image_path is valid and the image is not corrupt.")
else:
    print(f"Error: Image file not found at '{image_path}'. Please provide a valid image path.")

view raw JSON →