-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
307 lines (197 loc) · 6.52 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/python3
# -*- coding: utf-8 -*-
# Coded By Kuduxaaa
import os, sys, easyocr, imutils, cv2, time, threading, sqlite3
import numpy as np
from matplotlib import pyplot as plt
PLATE = None
plateValue = None
class BoomBarrierController:
def __init__(self):
self.currentStatus = 'closed'
def open(self):
if self.getCurrentStatus() == 'closed':
self.currentStatus = 'open'
print('open')
# შლაგბაუნის გაღების შემდეგ ავტომატურად დაკეტვის ალორითმია დასაწრერი აქ...
def close(self):
if self.currentStatus == 'open':
self.currentStatus = 'closed'
print('closed')
def isOpen(self):
return False
def getCurrentStatus(self):
return self.currentStatus
def startBeep(self, duration):
print('Starting beep...')
class Database:
def __init__(self):
self.database = 'data/database.db'
self.connection = sqlite3.connect(self.database)
self.cursor = self.connection.cursor()
def __del__(self):
self.connection.close()
def isAllowed(self, plateNumber):
self.cursor.execute(
'SELECT * FROM cars WHERE plate_number = ?',
(plateNumber,)
)
data = self.cursor.fetchone()
if data is not None and len(data) > 0:
if data[2] == 1:
return True
return False
class thread_with_trace(threading.Thread):
def __init__(self, *args, **keywords):
threading.Thread.__init__(self, *args, **keywords)
self.killed = False
def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
def kill(self):
self.killed = True
class LPR:
def __init__(self):
self.version = 1.0
self.db = Database()
self.barrier = BoomBarrierController()
def __repr__(self):
return f'<LPR v{self.version}>'
def showImage(self, image, title='Result', mode='matplotlib'):
if mode == 'cv2':
cv2.imshow(title, image)
cv2.waitKey(9)
cv2.destroyAllWindows()
plt.imshow(
cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
)
plt.show()
def imageToGray(self, image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def imageToEdge(self, image):
bilateral = cv2.bilateralFilter(image, 11, 17, 17) # Noise reduction
edged = cv2.Canny(bilateral, 30, 200) # Edge Detection
return edged
def findContours(self, image):
points = cv2.findContours(image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(points)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
location = None
for contour in contours:
approx = cv2.approxPolyDP(contour, 10, True)
if len(approx) == 4:
location = approx
break
return location
def cropPlateNumber(self, mask, gray):
x, y = np.where(mask == 255)
x1, y1 = (np.min(x), np.min(y)) # უკიდურესი ზედა კუთხეები
x2, y2 = (np.max(x), np.max(y)) # უკიდურესი ქვედა კუთხეები
return gray[x1:x2 + 1, y1:y2 + 1]
def OpticalCharacterRecognition(self, image):
reader = easyocr.Reader(['en'])
result = reader.readtext(image)
if len(result) > 0:
return str(result[0][-2]).upper().replace('-', '').strip()
return None
def getImageWithPlateNumbers(self, image, approx, value):
result = cv2.putText(
image,
text = value,
org = (
approx[0][0][0],
approx[1][0][1] + 60
),
fontFace = cv2.FONT_HERSHEY_SIMPLEX,
fontScale = 1,
color = (0, 255, 0),
thickness = 2,
lineType = cv2.LINE_AA
)
return cv2.rectangle(image,
tuple(approx[0][0]),
tuple(approx[2][0]),
(0, 255, 0), 3)
def recognitePlateNumber(self, filename, showImage = False):
if not os.path.exists(filename):
sys.exit(f'[-] File {filename} does not found')
image = cv2.imread(filename)
image = imutils.resize(image, 600, 480)
gray = self.imageToGray(image)
edged = self.imageToEdge(gray)
approxContours = self.findContours(edged)
mask = np.zeros(gray.shape, np.uint8)
plate = cv2.drawContours(mask, [approxContours], 0, 255, -1)
plate = cv2.bitwise_and(image, image, mask=mask)
plate = self.cropPlateNumber(mask, gray)
plateValue = self.OpticalCharacterRecognition(plate)
if plateValue is None:
return False
if showImage:
imageWithPlate = self.getImageWithPlateNumbers(image, approxContours, plateValue)
self.showImage(imageWithPlate)
return plateValue
def OpticalCharacterRecognitionForVideo(self):
global plateValue
while True:
if PLATE is not None:
reader = easyocr.Reader(['en'])
result = reader.readtext(PLATE)
if len(result) > 0:
plateValue = str(result[0][-2]).upper().replace('-', '').strip()
else:
plateValue = None
def videoCapturing(self):
global PLATE, plateValue
capture = cv2.VideoCapture(0)
opticalThread = thread_with_trace(target=self.OpticalCharacterRecognitionForVideo)
opticalThread.start()
while True:
ret, image = capture.read()
image = imutils.resize(image, 600, 480)
gray = self.imageToGray(image)
edged = self.imageToEdge(gray)
mask = np.zeros(gray.shape, np.uint8)
approxContours = self.findContours(edged)
if approxContours is not None:
plate = cv2.drawContours(mask, [approxContours], 0, 255, -1)
plate = cv2.bitwise_and(image, image, mask=mask)
plate = self.cropPlateNumber(mask, gray)
if plateValue and plateValue is not None:
if self.barrier.getCurrentStatus() != 'open':
allowed = self.db.isAllowed(plateValue)
if allowed:
# თუ მანქანის ნომერი იპოვა ბაზაში
# და მისი შესვლა ნებადართულია
self.barrier.open()
else:
# თუ მანქანას არ აქვს შესვლის უფლება
print('Access Denaid!')
self.getImageWithPlateNumbers(image, approxContours, plateValue)
PLATE = plate
cv2.imshow('Video Capturing', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if opticalThread.is_alive():
opticalThread.kill()
opticalThread.join()
capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
lpr = LPR()
lpr.videoCapturing()