In python how to run a local trained PaddleOCR Text Detection model on an image and draw the resulted bounding boxes on that image
Assistant
User
I want to use a Text Detection model not Text Recognition
Assistant
To run a locally trained PaddleOCR text detection model on an image and draw the resulted bounding boxes, you need to follow these steps:
Install PaddleOCR: Make sure you have PaddleOCR and the required dependencies installed in your Python environment. You can install it using pip:
bash
pip install paddlepaddle paddleocr
You may also need to install other dependencies like OpenCV for image handling:
bash
pip install opencv-python
Load the PaddleOCR Model: If you have trained your own model, load it by specifying the appropriate model directory.
Run Text Detection: Use the PaddleOCR's inference methods to detect text in the image.
Draw Bounding Boxes: Use OpenCV to annotate the image with bounding boxes.
Here's an example code snippet that demonstrates these steps:
python
import cv2
from paddleocr import PaddleOCR
# Initialize the PaddleOCR model (replace with your trained model path if needed)
ocr = PaddleOCR(use_angle_cls=True, model_dir='path_to_your_trained_model', lang='en') # Use 'ch' for Chinese, 'en' for English, etc.
# Load the image from file
image_path = 'your_image.jpg'
image = cv2.imread(image_path)
# Perform text detection
result = ocr.ocr(image_path, cls=True)
# Draw the bounding boxes on the image
for line in result:
for word_info in line:
# Extract the bounding box coordinates
box = word_info[0] # bounding box coordinates
points = [(int(point[0]), int(point[1])) for point in box] # Convert to integer coordinates
# Draw the bounding box on the image
cv2.polylines(image, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)
# Optionally, draw the text label
text = word_info[1][0] # recognized text
cv2.putText(image, text, (int(box[0][0]), int(box[0][1]) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# Save or show the resulting image with bounding boxes
cv2.imwrite('output_image.jpg', image)
# Alternatively, display the image
# cv2.imshow('Detected Text', image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
Explanation of the Code:
We initialize the PaddleOCR instance and specify the model directory if you have a custom-trained model.
We load an image using OpenCV.
We run OCR on the image, which returns the detection results.
For each detected text box, we extract the coordinates and draw them on the image using OpenCV's polylines function.
Optionally, we also write the recognized text above each bounding box using putText.
Finally, we save the annotated image or display it in a window.
Notes:
Ensure you replace 'path_to_your_trained_model' and 'your_image.jpg' with your actual model path and image file path.
Depending on your model training specifics, you may want to adjust the parameters (model_dir, lang).
You can also adjust drawing parameters such as color and thickness as per your preference.
If you're interested specifically in text detection (and not recognition) with PaddleOCR, you can modify the approach slightly to focus on just the detection part without involving text recognition.
In the following example, we will focus on using the text detection functionality of PaddleOCR. You will use the detector provided by PaddleOCR just for detecting text without recognizing it.
Here is how you can run a local PaddleOCR text detection model and draw the detected bounding boxes on an image:
Install Required Libraries: Ensure you have PaddleOCR and OpenCV installed as shown in the previous example.
Load and Run the Text Detection Model: You will set up the text detection specifically.
Draw the Detected Bounding Boxes: Again, you’ll use OpenCV for drawing the bounding boxes.
Here is the code illustrating these steps:
python
import cv2
import numpy as np
from paddleocr import PaddleOCR
# Initialize the PaddleOCR with text detection (no recognition)
ocr = PaddleOCR(
use_angle_cls=True,
lang='en', # Use the language you prefer
det_model_dir='path_to_your_trained_detection_model' # Path to your trained detection model
)
# Load the image into memory
image_path = 'your_image.jpg'
image = cv2.imread(image_path)
# Perform text detection
# Here we will only take 'det' (detection) output
result = ocr.ocr(image_path, cls=False) # set cls=False to skip classification
# Draw the bounding boxes on the image
for line in result:
for (box, confidence) in line: # Assume you only care about the detections
# Extract the bounding box coordinates, as a list of tuples
points = np.array(box, dtype=np.int32) # Ensure points are integers
# Draw the bounding box on the image
cv2.polylines(image, [points], isClosed=True, color=(0, 255, 0), thickness=2)
# Save or show the resulting image with bounding boxes
cv2.imwrite('detected_boxes_image.jpg', image)
# Alternatively, display the image using OpenCV
# cv2.imshow('Detected Text', image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
Explanation of Changes:
Model Initialization: We initialize PaddleOCR without specifying model_dir when using the default model, or you can specify your customized trained model path specific to detection.
Text Detection: When calling ocr.ocr(), I set cls=False to skip the classification part, focusing purely on detection.
Result Parsing: We extract only the bounding boxes from the result.
Notes:
Ensure to have the proper paths for both your trained detection model and the image.
Adjust det_model_dir to point to your specific trained text detection model if it's not the default.
The color and thickness of the lines drawn for bounding boxes can be customized as per your preferences.