0

我的脚本从特定文件夹中抓取图像,在我获得车牌后,图像被删除。我需要跳过坏图像并删除它们,否则脚本会陷入无限循环。这是我的代码示例:

def scan_image(image_name):
category_index = label_map_util.create_category_index_from_labelmap(files['LABELMAP'])

IMAGE_PATH = os.path.join(paths['IMAGE_PATH'], 'images', image_name)

img = cv2.imread(IMAGE_PATH)
image_np = np.array(img)
image_np_expanded = np.expand_dims(image_np, axis=0)
input_tensor = tf.convert_to_tensor(image_np_expanded, dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
              for key, value in detections.items()}
detections['num_detections'] = num_detections
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()

viz_utils.visualize_boxes_and_labels_on_image_array(
    image_np_with_detections,
    detections['detection_boxes'],
    detections['detection_classes'] + label_id_offset,
    detections['detection_scores'],
    category_index,
    use_normalized_coordinates=True,
    max_boxes_to_draw=5,
    min_score_thresh=.8,
    agnostic_mode=False)

detections.keys()

# Apply OCR to Detection

detection_threshold = 0.7

image = image_np_with_detections
scores = list(filter(lambda x: x > detection_threshold, detections['detection_scores']))
boxes = detections['detection_boxes'][:len(scores)]
classes = detections['detection_classes'][:len(scores)]

width = image.shape[1]
height = image.shape[0]

# Apply ROI filtering and OCR

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

for result in ocr_result:
    print(np.sum(np.subtract(result[0][2], result[0][1])))
    print(result[1])

region_threshold = 0.05
filter_text(region, ocr_result, region_threshold)
region_threshold = 0.6
text, region = ocr_it(image_np_with_detections, detections, detection_threshold, region_threshold)
plate = (text, region)
return plate


def analyze():
files = os.listdir(os.path.join(paths['IMAGE_PATH'], 'images'))
for file in files:
    print(file)
    full_file_path = os.path.join(paths['IMAGE_PATH'], 'images', file)
    print(full_file_path)
    if os.path.exists(full_file_path):
        plate = scan_image(file)
        text = plate[0]
        region = plate[1]
        save_results(text, region, 'detection_results.csv', 'Detection_Images')
        sendLog('Nr: ' + ''.join(text))
        try:
            os.remove(full_file_path)
            print('File ' + file + ' was analyzed and removed from input folder')
        except OSError:
            print("Error while deleting file ", full_file_path)


def script_thread():
while True:
    try:
        analyze()
    except Exception as e:
        logging.error("scriptThread " + str(e))

ThrS = threading.Thread(target=script_thread) ThrS.start()

这是脚本开始分析不良图像时我得到的输出,其中我的模型无法检测到车牌:

ERROR:root:scriptThread local variable 'ocr_result' referenced before assignment
Test.png
Tensorflow/workspace/images/images/Test.png
ERROR:root:scriptThread local variable 'ocr_result' referenced before assignment
Test.png
Tensorflow/workspace/images/images/Test.png
ERROR:root:scriptThread local variable 'ocr_result' referenced before assignment
Test.png
Tensorflow/workspace/images/images/Test.png
Test.png
Tensorflow/workspace/images/images/Test.png
ERROR:root:scriptThread local variable 'ocr_result' referenced before assignment
4

0 回答 0