0

this is the code

from ultralytics import YOLO
license_plate_detector = YOLO('./model/best.pt')
license_plates = license_plate_detector('./42.jpg')

and this the output

640x608 1 number-plate, 342.0ms
Speed: 12.4ms preprocess, 342.0ms inference, 3.0ms postprocess per image at shape (1, 3, 640, 608)

i want to convert this output to image and save it to use with esayocr

the class don't have any save method so how to do this

2 Answers2

0

I think, it will helps you.

from PIL import Image
from ultralytics import YOLO
import easyocr

license_plate_detector = YOLO('./model/best.pt')

input_image = Image.open('./42.jpg')

detections = license_plate_detector(input_image)

license_plate_boxes = detections[0].boxes.data.cpu().numpy()

reader = easyocr.Reader(['en'])

for i, box in enumerate(license_plate_boxes):
    x1, y1, x2, y2, conf, cls = box
    license_plate = input_image.crop((x1, y1, x2, y2))
    
    plate_filename = f'license_plate_{i}.jpg'
    license_plate.save(plate_filename)

    results = reader.readtext(plate_filename)
    print(f"License Plate {i+1} Text: {results[0][1]}")
hanna_liavoshka
  • 115
  • 1
  • 6
Faisal Shahbaz
  • 431
  • 3
  • 12
  • thank you put i don't work show this error --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[2], line 11 7 input_image = Image.open('./42.jpg') 9 detections = license_plate_detector(input_image) ---> 11 license_plate_boxes = detections.xyxy[0].cpu().numpy() 13 reader = easyocr.Reader(['en']) 15 for i, box in enumerate(license_plate_boxes): AttributeError: 'list' object has no attribute 'xyxy' – Murtaza Nazar Aug 15 '23 at 08:33
  • Try it with a small adjustment: ```license_plate_boxes = detections[0].boxes.data.cpu().numpy()``` – hanna_liavoshka Aug 15 '23 at 09:33
  • don't work and show this error "name": "AttributeError", "message": "module 'PIL.Image' has no attribute 'ANTIALIAS'", – Murtaza Nazar Aug 15 '23 at 10:22
  • update the PIL library using pip – Faisal Shahbaz Aug 15 '23 at 12:14
  • update it and the same error appear again – Murtaza Nazar Aug 16 '23 at 05:11
0

To save the detected objects as cropped images, add the argument save_crop=True to the inference command. To save the original image with plotted boxes on it, use the argument save=True. The results will be saved to 'runs/detect/predict' or a similar folder (the exact path will be shown in the output).

from ultralytics import YOLO
license_plate_detector = YOLO('./model/best.pt')
license_plates = license_plate_detector('./42.jpg', save=True, save_crop=True)

You can specify where to save the output data using the parameters for project name project and experiment name name. To save all experiment results in the same folder use exist_ok=True.

license_plates = license_plate_detector('./42.jpg', save=True, save_crop=True, project="runs/detect", name="inference", exist_ok=True)
# runs/detect/inference

More information about inference: https://docs.ultralytics.com/modes/predict/#inference-arguments

hanna_liavoshka
  • 115
  • 1
  • 6