Search code examples
pythonyoloyolov8yolov7

YOLOV8: how to save the output of model


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


Solution

  • 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]}")