-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlp_image.py
55 lines (51 loc) · 1.98 KB
/
lp_image.py
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
from PIL import Image
import cv2
import torch
import math
import function.utils_rotate as utils_rotate
from IPython.display import display
import os
import time
import argparse
import function.helper as helper
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True, help='path to input image')
args = ap.parse_args()
yolo_LP_detect = torch.hub.load('yolov5', 'custom', path='model/LP_detector.pt', force_reload=True, source='local')
yolo_license_plate = torch.hub.load('yolov5', 'custom', path='model/LP_ocr.pt', force_reload=True, source='local')
yolo_license_plate.conf = 0.60
img = cv2.imread(args.image)
plates = yolo_LP_detect(img, size=640)
plates = yolo_LP_detect(img, size=640)
list_plates = plates.pandas().xyxy[0].values.tolist()
list_read_plates = set()
if len(list_plates) == 0:
lp = helper.read_plate(yolo_license_plate,img)
if lp != "unknown":
cv2.putText(img, lp, (7, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
list_read_plates.add(lp)
else:
for plate in list_plates:
flag = 0
x = int(plate[0]) # xmin
y = int(plate[1]) # ymin
w = int(plate[2] - plate[0]) # xmax - xmin
h = int(plate[3] - plate[1]) # ymax - ymin
crop_img = img[y:y+h, x:x+w]
cv2.rectangle(img, (int(plate[0]),int(plate[1])), (int(plate[2]),int(plate[3])), color = (0,0,225), thickness = 2)
cv2.imwrite("crop.jpg", crop_img)
rc_image = cv2.imread("crop.jpg")
lp = ""
for cc in range(0,2):
for ct in range(0,2):
lp = helper.read_plate(yolo_license_plate, utils_rotate.deskew(crop_img, cc, ct))
if lp != "unknown":
list_read_plates.add(lp)
cv2.putText(img, lp, (int(plate[0]), int(plate[1]-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
flag = 1
break
if flag == 1:
break
cv2.imshow('frame', img)
cv2.waitKey()
cv2.destroyAllWindows()