-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOCR.py
44 lines (34 loc) · 1.41 KB
/
OCR.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
from Load_Model import *
import easyocr
detection_threshold = 0.4
region_threshold = 0.6
category_index = label_map_util.create_category_index_from_labelmap(files['LABELMAP'])
def filter_text(region, ocr_result, region_threshold):
rectangle_size = region.shape[0]*region.shape[1]
plate = []
for result in ocr_result:
length = np.sum(np.subtract(result[0][1], result[0][0]))
height = np.sum(np.subtract(result[0][2], result[0][1]))
if length*height / rectangle_size > region_threshold:
plate.append(result[1])
return plate
def ocr_it(image, detections):
# 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)
print(text)
return text, region