nicknochnack / RealTimeAutomaticNumberPlateRecognition

123 stars 139 forks source link

ocr_it only returns the first box in boxes #7

Open jontio opened 1 year ago

jontio commented 1 year ago

If an image has more than one number plate then ocr_it will only return one of them...

def ocr_it(image, detections, detection_threshold, region_threshold):

    # Scores, boxes and classes above threhold
    scores = list(filter(lambda x: x> detection_threshold, detections['detection_scores']))
    boxes = detections['detection_boxes'][:len(scores)]
    classes = detections['detection_classes'][:len(scores)]

    # Full image dimensions
    width = image.shape[1]
    height = image.shape[0]

    # Apply ROI filtering and OCR
    for idx, box in enumerate(boxes):
        roi = box*[height, width, height, width]
        region = image[int(roi[0]):int(roi[2]),int(roi[1]):int(roi[3])]
        reader = easyocr.Reader(['en'])
        ocr_result = reader.readtext(region)

        text = filter_text(region, ocr_result, region_threshold)

        plt.imshow(cv2.cvtColor(region, cv2.COLOR_BGR2RGB))
        plt.show()
        print(text)
        return text, region

The return shouldn't be there. It should be more like...

def ocr_it(image, detections, detection_threshold, region_threshold):

    # We may have more than one number plate in an image
    texts = []
    regions = []

    # Scores, boxes and classes above threhold
    scores = list(filter(lambda x: x> detection_threshold, detections['detection_scores']))
    boxes = detections['detection_boxes'][:len(scores)]
    classes = detections['detection_classes'][:len(scores)]

    # Full image dimensions
    width = image.shape[1]
    height = image.shape[0]

    # Apply ROI filtering and OCR
    for idx, box in enumerate(boxes):
        roi = box*[height, width, height, width]
        region = image[int(roi[0]):int(roi[2]),int(roi[1]):int(roi[3])]
        reader = easyocr.Reader(['en'])
        ocr_result = reader.readtext(region)

        text = filter_text(region, ocr_result, region_threshold)

        if(text!=[]):
            texts.append(text[0])
            regions.append(region)

    return texts, regions

That means the function save_results won't work and also needs to be change to something like...

def save_result(text, region, csv_filename, folder_path):
    img_name = '{}.jpg'.format(uuid.uuid1())

    cv2.imwrite(os.path.join(folder_path, img_name), region)

    with open(csv_filename, mode='a', newline='') as f:
        csv_writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow([img_name, text])

def save_results(texts, regions, csv_filename, folder_path):
    for idx, region in enumerate(regions):
        save_result(texts[idx], region, csv_filename, folder_path)

There is also a hard coded detection threshold in the live detection that I changed to the variable detection_threshold

I realize if you do a merge then your video will be out of step with the code but I thought that I would send you one anyway.

Cheers, Jonti