File size: 1,166 Bytes
79f9dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
os.system('wget https://huggingface.co/spaces/An-619/FastSAM/resolve/main/weights/FastSAM.pt')

import yolov5

# load model
model = yolov5.load('keremberke/yolov5m-license-plate')

# set model parameters
model.conf = 0.5  # NMS confidence threshold
model.iou = 0.25  # NMS IoU threshold
model.agnostic = False  # NMS class-agnostic
model.multi_label = False  # NMS multiple labels per box
model.max_det = 1000  # maximum number of detections per image

# set image
def license_plate_detect(img):
    # perform inference
    results = model(img, size=640)
    
    # inference with test time augmentation
    results = model(img, augment=True)
    
    # parse results
    if len(results.pred):
        predictions = results.pred[0]
        boxes = predictions[:, :4] # x1, y1, x2, y2
        scores = predictions[:, 4]
        categories = predictions[:, 5]
        return boxes

from PIL import Image
# image = Image.open(img)
import pytesseract

def read_license_number(img):
    boxes = license_plate_detect(img)
    if boxes:
        return [pytesseract.image_to_string(
                    image.crop(bbox.tolist()))
               for bbox in boxes]