File size: 3,185 Bytes
0a2675c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from model1 import np, YOLO, processor, model

lp_detection = YOLO("models/yolov8n_lp_det.pt")

# processor = TrOCRProcessor.from_pretrained('models/processor')
# model = VisionEncoderDecoderModel.from_pretrained('models/model')

# set special tokens used for creating the decoder_input_ids from the labels
model.config.decoder_start_token_id = processor.tokenizer.cls_token_id
model.config.pad_token_id = processor.tokenizer.pad_token_id
# make sure vocab size is set correctly
model.config.vocab_size = model.config.decoder.vocab_size

# set beam search parameters
model.config.eos_token_id = processor.tokenizer.sep_token_id
model.config.max_length = 20
model.config.early_stopping = True
model.config.no_repeat_ngram_size = 3
model.config.length_penalty = 2.0
model.config.num_beams = 4


# function to detect licence plates in the given car images
def detect_lp(inputs):
    lps = []
    # running the license plate detection model with 50% confidence threshold
    lp_results = lp_detection.predict(source=inputs, conf=0.5, verbose=False)
    # iterating through each output (num of outputs will be same as num of inputs)
    for lp_result in lp_results:
        # finding the bounding boxes of the license plate detected
        lp_boxes = lp_result.boxes.xyxy.tolist()    
        # iterating through each license plate detected
        for lp_box in lp_boxes:
            # cropping license plate  image from the car image
            lp = lp_result.orig_img[int(lp_box[1]):int(lp_box[3]), int(lp_box[0]):int(lp_box[2])]
            lps.append(lp)
            # breaking as we only want to detect one licence plate per car
            break
        
        # if no licence plate is detected then we are adding a black image 
        if len(lp_boxes) == 0:
            lps.append(np.zeros((100,100,3), np.uint8))
            
    return lps

# function to detect licence plate number in the given licence plate images
def detect_lp_text(inputs):
    plate_number = []
    # iterating through each licence plate
    for input in inputs:
        # finding the number/text in licence plate
        pixel_values = processor(input, return_tensors="pt").pixel_values
        generated_ids = model.generate(pixel_values)
        generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]


        # if no text is found in the licence plate, then adding a default text not found
        if len(generated_text) == 0:
            plate_number.append("not found")
        else:
            # adding the licence plate number to a list
            plate_number.append(generated_text)
            
    return plate_number

def run(inputs):
        
    # for future, to handle multiple inputs
    # currently using just one input
    inputs = inputs[0]
    
    # detecting licence plates from the input images
    # returns licence plate images, if it cant find a license plate a black image is returned
    lps = detect_lp(inputs)
    
    # detecting licence plate number from licence plate images
    # returns text from the licence plate images, if none is detected "not found" text is returned
    lp_text = detect_lp_text(lps)
    
    return lps, lp_text