reader = easyocr.Reader([‘en’], gpu=False)
dict_char_to_int = {‘O’: ‘0’,
‘I’: ‘1’,
‘J’: ‘3’,
‘A’: ‘4’,
‘G’: ‘6’,
‘S’: ‘5’}
dict_int_to_char = {‘0’: ‘O’,
‘1’: ‘I’,
‘3’: ‘J’,
‘4’: ‘A’,
‘6’: ‘G’,
‘5’: ‘S’}
def get_car(license_plate, vehicle_track_ids):
x1, y1, x2, y2, score, class_id = license_plate
foundIt = False
for j in range(len(vehicle_track_ids)):
xcar1, ycar1, xcar2, ycar2, car_id = vehicle_track_ids[j]
if x1 > xcar1 and y1 > ycar1 and x2 < xcar2 and y2 < ycar2:
car_indx = j
foundIt = True
break
if foundIt:
return vehicle_track_ids[car_indx]
return -1, -1, -1, -1, -1
def license_complies_format(text):
if len(text) != 10:
return False
if (text[0] in string.ascii_uppercase or text[0] in dict_int_to_char.keys()) and \
(text[1] in string.ascii_uppercase or text[1] in dict_int_to_char.keys()) and \
(text[2] in ['0','1','2','3','4','5','6','7','8','9'] or text[2] in dict_char_to_int.keys()) and \
(text[3] in ['0','1','2','3','4','5','6','7','8','9'] or text[3] in dict_char_to_int.keys()) and \
(text[4] in string.ascii_uppercase or text[4] in dict_int_to_char.keys()) and \
(text[5] in string.ascii_uppercase or text[5] in dict_int_to_char.keys()) and \
(text[6] in ['0','1','2','3','4','5','6','7','8','9'] or text[6] in dict_char_to_int.keys()) and \
(text[7] in ['0','1','2','3','4','5','6','7','8','9'] or text[7] in dict_char_to_int.keys()) and \
(text[8] in ['0','1','2','3','4','5','6','7','8','9'] or text[8] in dict_char_to_int.keys()) and \
(text[9] in ['0','1','2','3','4','5','6','7','8','9'] or text[9] in dict_char_to_int.keys()):
return True
else:
return False
def format_license(text):
license_plate_ = ‘’
mapping = {0: dict_int_to_char, 1: dict_int_to_char, 4: dict_int_to_char, 5: dict_int_to_char, 6: dict_int_to_char,
2: dict_char_to_int, 3: dict_char_to_int}
for j in [0, 1, 2, 3, 4, 5, 6]:
if text[j] in mapping[j].keys():
license_plate_ += mapping[j][text[j]]
else:
license_plate_ += text[j]
return license_plate_
def read_license_plate(license_plate_detection_crop):
detections = reader.readtext(license_plate_detection_crop)
for detection in detections:
bbox, text, score = detection
text = text.upper().replace(' ', '')
if license_complies_format(text):
return format_license(text), score
return None, None
vehicles = [7]
mot_tracker = Sort()
ret=True
while ret:
ret,frame = cap.read()
if ret:
detections = video(frame)[0]
detections_=
for detection in detections.boxes.data.tolist():
x1,y1,x2,y2,score,class_id = detection
if int(class_id) in vehicles:
detections_.append([x1, y1, x2, y2, score])
track_ids = mot_tracker.update(np.asarray(detections_))
license_plate_detections = license_plate_detector(frame)[0]
for license_plate_detection in license_plate_detections.boxes.data.tolist():
x1,y1,x2,y2,score,class_id = license_plate_detection
xcar1, ycar1, xcar2, ycar2, car_id=get_car(license_plate_detection,track_ids)
license_plate_detection_crop = frame[int(y1):int(y2), int(x1): int(x2), :]
license_plate_detection_crop_gray = cv2.cvtColor(license_plate_detection_crop, cv2.COLOR_BGR2GRAY)
_, license_plate_detection_crop_thresh = cv2.threshold(license_plate_detection_crop_gray, 64, 255, cv2.THRESH_BINARY_INV)
license_plate_text, license_plate_text_score = read_license_plate(license_plate_detection_crop_thresh)
plt.imshow(cv2.cvtColor(license_plate_detection_crop, cv2.COLOR_BGR2RGB))
plt.title(‘Original Image’)
plt.axis(‘off’)
plt.show()
plt.imshow(license_plate_detection_crop_thresh, cmap='gray')
plt.title('Thresholded Image')
plt.axis('off')
plt.show()