upadhyaysuraj's picture
Upload 11 files
57d8fc8 verified
import cv2
import matplotlib.pyplot as plt
from ultralytics import YOLO
import pytesseract
from pytesseract import Output
def predict_and_plot(model, path_test_car):
"""
Predicts and plots the bounding boxes on the given test image using the trained YOLO model.
Also performs OCR on the detected bounding boxes to extract text.
Parameters:
model (YOLO): The trained YOLO model.
path_test_car (str): Path to the test image file.
"""
# Perform prediction on the test image using the model
results = model.predict(path_test_car, device='CPU')
# Load the image using OpenCV
image = cv2.imread(path_test_car)
# Convert the image from BGR (OpenCV default) to RGB (matplotlib default)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Extract the bounding boxes and labels from the results
for result in results:
for box in result.boxes:
# Get the coordinates of the bounding box
x1, y1, x2, y2 = map(int, box.xyxy[0])
# Get the confidence score of the prediction
confidence = box.conf[0]
# Draw the bounding box on the image
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Draw the confidence score near the bounding box
cv2.putText(image, f'{confidence*100:.2f}%', (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
# Crop the bounding box from the image for OCR
roi = image[y1:y2, x1:x2]
# Perform OCR on the cropped image
text = pytesseract.image_to_string(roi, config='--psm 6')
print(f"Detected text: {text}")
# Plot the image with bounding boxes
plt.imshow(image)
plt.axis('off') # Hide the axis
plt.show() # Display the image
if __name__ == "__main__":
model = YOLO('best.pt')
predict_and_plot(model, "Dataset/images/Cars9.png")