-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils_NumberPlateRecognition.py
159 lines (126 loc) · 5.13 KB
/
Utils_NumberPlateRecognition.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import cv2
from cityCode import cityCode_dict
from datetime import datetime
import numpy as np
def check1Line(img, detail):
date = False
tolerance = int (img.shape[0] / 7)
yline1 = 0
numberPlate = ""
datePlate = ""
for i in range (len(detail)):
# Check Line
if i == 0:
yline1 = detail[i][0][0][1]
numberPlate = detail[i][1]
else:
if date == False:
yMin = yline1 - tolerance
yMax = yline1 + tolerance
if yMin < detail[i][0][0][1] < yMax:
numberPlate = numberPlate + " " + detail[i][1]
else:
date = True
if date == True:
datePlate = datePlate + detail[i][1]
img = createRectangle(img, detail[i][0])
return numberPlate, datePlate
def createRectangle(img, points):
color = (0,255,255)
thickness = 1
cv2.line(img, points[0], points[1], color, thickness)
cv2.line(img, points[1], points[2], color, thickness)
cv2.line(img, points[2], points[3], color, thickness)
cv2.line(img, points[3], points[0], color, thickness)
return img
def corrrectCity(numberPlatePoints):
numberPlatePoints = list(numberPlatePoints)
for i in range (2): # 2 First Digit
if numberPlatePoints[i] == "0":
numberPlatePoints[i] = "D"
elif numberPlatePoints[i] == "5":
numberPlatePoints[i] = "S"
elif numberPlatePoints[i] == "6":
numberPlatePoints[i] = "G"
elif numberPlatePoints[i] == "8":
numberPlatePoints[i] = "B"
numberPlatePoints = "".join(numberPlatePoints)
return numberPlatePoints
def correctCapital(numberPlate):
numberPlate = list(numberPlate)
numberPlate = [x.upper() for x in numberPlate]
numberPlate = "".join(numberPlate)
return numberPlate
def checkCity(numberPlate):
cityCode = numberPlate.split()[0]
if cityCode in cityCode_dict:
city = cityCode_dict[cityCode]
else:
city = "Error City"
return cityCode, city
def checkOddEven(numberPlate):
status = "None"
for x in numberPlate:
try:
x = int(x)
if x%2 == 0:
status = "Even"
else:
status = "Odd"
except ValueError:
pass
return status
def correctNumber(datePlate):
datePlate = list(datePlate)
for i in range (len(datePlate)):
if datePlate[i] == "D" or datePlate[i] == "O" or datePlate[i] == "o":
datePlate[i] = "0"
elif datePlate[i] == "S" or datePlate[i] == "s":
datePlate[i] = "5"
elif datePlate[i] == "G":
datePlate[i] = "6"
elif datePlate[i] == "B":
datePlate[i] = "8"
datePlate = "".join(datePlate)
datePlate = datePlate.replace(" ", "")
#for r in ((".", " "), ("*", " "), (",", " "), ("'", " ")):
#datePlate = datePlate.replace(*r)
return datePlate
def showText(img, numberPlate,datePlate,code,city,oddEven,month,year):
color = (255,255,0)
colorG = (0,255,0)
colorR = (0,0,255)
thickness = 2
scale = 2
distance = int(img.shape[0] / 7)
currentDay = datetime.now().day
currentMonth = datetime.now().month
currentYear = datetime.now().year
timeNow = str(currentDay) + "/" + str(currentMonth) + "/" + str(currentYear)
status = ""
if currentDay%2 == 0:
status = "Even"
else:
status = "Odd"
statusText = "Status: " + status
#Black
img[0:int(1 * distance)+5 , img.shape[1] - 170:img.shape[1]] = np.zeros(((1*distance)+5 , 170, 3), np.uint8)
cautionStatus = ""
if status != oddEven:
cautionStatus = " (VIOLATION!)"
cautionExpired = ""
if year < currentYear:
cautionExpired = " (VIOLATION!)"
elif year == currentYear:
if month <= currentMonth:
cautionExpired = " (VIOLATION!)"
textExpired = "Expired: " + str(month) + "/" + str(year)
cv2.putText(img, timeNow, (img.shape[1] - 170, int(0.5 * distance)), cv2.FONT_HERSHEY_PLAIN, 1.5, color, thickness)
cv2.putText(img, statusText, (img.shape[1] - 170, int(1 * distance)), cv2.FONT_HERSHEY_PLAIN, 1.5, color, thickness)
cv2.putText(img, numberPlate, (20, img.shape[0] - int(2.1 * distance)), cv2.FONT_HERSHEY_PLAIN, scale, color, thickness)
cv2.putText(img, "City: " + str(city), (20, img.shape[0] - int(1.5 * distance)), cv2.FONT_HERSHEY_PLAIN, 1.5, colorG, thickness)
cv2.putText(img, "Type: " + oddEven, (20, img.shape[0] - 1 * distance), cv2.FONT_HERSHEY_PLAIN, 1.5, colorG, thickness)
cv2.putText(img, textExpired, (20, img.shape[0] - int(0.5 * distance)), cv2.FONT_HERSHEY_PLAIN, 1.5, colorG, thickness)
cv2.putText(img, cautionStatus, (20+135, img.shape[0] - int(1 * distance)), cv2.FONT_HERSHEY_PLAIN, 1, colorR, thickness)
cv2.putText(img, cautionExpired, (20+225, img.shape[0] - int(0.5 * distance)), cv2.FONT_HERSHEY_PLAIN, 1, colorR, 2)
return img