-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
115 lines (94 loc) · 3.52 KB
/
main.py
File metadata and controls
115 lines (94 loc) · 3.52 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import cv2
from matplotlib import pyplot as plt
import numpy as np
import imutils
import easyocr
import sys, getopt
import logging
logger = logging.getLogger(__name__)
MIN_PLATE_NUM_LEN = 6
def show_img(img):
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
def find_contours(img):
# Find contour step - rectangular shape of license plate
c_points = cv2.findContours(img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Return as tree, simplify line points
contours = imutils.grab_contours(c_points)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10] # sort, return first 10 contours
return contours
def pre_process_image(img):
# Greyscale step
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Edge detection step
b_filter = cv2.bilateralFilter(grey_img, 11, 17, 17) # nosie reduction
edge = cv2.Canny(b_filter, 30, 200) # edge detection
return edge, grey_img, find_contours(edge)
def crop_image(img, mask):
(x, y) = np.where(mask==255)
(x1, y1) = (np.min(x), np.min(y))
(x2, y2) = (np.max(x), np.max(y))
return img[x1:x2+1, y1:y2+1]
def maybe_find_license_plate_text(raw_img, grey_img, contours):
# find rectangles
locations = []
for contour in contours:
approx = cv2.approxPolyDP(contour, 10, True) # approx polygon in contour
if len(approx) == 4:
locations.append(approx) # found a rectangle location
# print(locations)
for location in locations:
mask = np.zeros(grey_img.shape, np.uint8)
new_img = cv2.drawContours(mask, [location], 0, 255, -1)
new_img = cv2.bitwise_and(raw_img, raw_img, mask=mask)
cropped_img = crop_image(grey_img, mask)
# instantiate easyocr reader with english language
reader = easyocr.Reader(['en'])
# pass cropped image to reader
result = reader.readtext(cropped_img)
try:
text = result[-1][-2]
if len(text) >= MIN_PLATE_NUM_LEN:
# valid license plate text found
return text, location
else:
raise IndexError()
except IndexError:
pass
raise Exception("No license plate found :(")
def draw_res(raw_img, text, location):
font = cv2.FONT_HERSHEY_SIMPLEX
res = cv2.putText(raw_img, text=text, org=(location[0][0][0], location[1][0][1]+100), fontFace=font, fontScale=1, color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
res = cv2.rectangle(raw_img, tuple(location[0][0]), tuple(location[2][0]), (0, 255,0), 3)
return res
def main(argv):
inputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:",["input="])
except getopt.GetoptError:
print('main.py -i <inputfile>')
sys.exit(2)
if len(opts) == 0:
print('main.py -i <inputfile>')
sys.exit()
for opt, arg in opts:
if opt == '-h':
print('main.py -i <inputfile>')
sys.exit()
elif opt in ("-i", "--input"):
inputfile = arg
else:
print('main.py -i <inputfile>')
sys.exit()
# Import image
raw_img = cv2.imread(inputfile)
# Get preprocessed images, contours
grey_img, edge_img, contours = pre_process_image(raw_img)
# Try to get license plate and text
try:
text, location = maybe_find_license_plate_text(raw_img, grey_img, contours)
except Exception as error:
print(error)
quit()
show_img(draw_res(raw_img, text, location))
if __name__ == "__main__":
main(sys.argv[1:])