How to use PaddleOCR from code using onnxruntime? #14771
Replies: 1 comment 1 reply
-
You can use PaddleOCR with 1. Install Required DependenciesFirst, you need to make sure you have installed pip install onnxruntime numpy opencv-python 2. Load the ONNX ModelsYou need to load both the text detection and recognition models. import onnxruntime as ort
import numpy as np
import cv2
# Load detection and recognition models
det_model_path = "path_to_detection_model.onnx"
rec_model_path = "path_to_recognition_model.onnx"
det_session = ort.InferenceSession(det_model_path)
rec_session = ort.InferenceSession(rec_model_path) 3. Preprocess Image for DetectionPaddleOCR uses specific preprocessing steps before feeding the image into the model. def preprocess_image(image_path, target_size=(640,640)):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
original_shape = image.shape[:2]
# Resize and normalize the image
image = cv2.resize(image, target_size)
image = image.astype(np.float32) / 255.0
image = np.transpose(image, (2, 0, 1)) # Convert to CHW format
image = np.expand_dims(image, axis=0) # Add batch dimension
return image, original_shape
image_path = "path_to_your_image.jpg"
image, original_shape = preprocess_image(image_path) 4. Perform Text DetectionRun the detection model and apply post-processing. input_name = det_session.get_inputs()[0].name
outputs = det_session.run(None, {input_name: image})
# The output here needs to be processed to extract bounding boxes
# The processing logic depends on the specific PaddleOCR model used 5. Adjust
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have downloaded the detection and recognition .onnx files. How do I use them from code? I would like to change the
det_db_unclip_ratio
parameter as well.Beta Was this translation helpful? Give feedback.
All reactions