I am unable to get the text from license_plate_text variable but when i plot it, its able to detect the license plate and display it

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()

Hi @Isaac_Natarajan, I am unable to identify which part of the code mentioned above that is used for fetching text from the image. Please provide that particular code only. Thank You.

license_plate_text, license_plate_text_score = read_license_plate(license_plate_detection_crop_thresh)

This is the part where I am unable to run ‘license_plate_test’

Hi @Isaac_Natarajan, You can get the text from the number_plate by using easyocr. For example, I have tried with a sample image
text_extraction

using easyocr i get the output as

import easyocr
detections = reader.readtext('/content/text_extraction.jpeg')
bbox, text, score = detections[0]
print(text) #ty12hh5766

Also in the above function you mentioned if len(text) != 10: if this condition fails by default the function will return none and this none goes to this if license_complies_format(text): condition present in the read_license_plate function and this condition fails to enter and you get the output as none,none. Thank You.

If you don’t mind can I get the GitHub of the code you have done. Please because I am doing this as my college project

Btw you using it for a image is it working for video as well?

Hi @Isaac_Natarajan, To work with video, You have to get the image frames from the video and pass it to the easyocr to get the text. Thank You.

Hi @Isaac_Natarajan, please refer to this gist for code example. Thank You.

Hopefully I did it anyways thankyou for your precious time.