-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLpc.py
343 lines (288 loc) · 13 KB
/
Lpc.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import cv2
import sys
import os
import datetime
import numpy as np
from openalpr import Alpr
class Lpc(object):
"""docstring for Lpc."""
gl_filepath = None
gl_original_image = None
gl_matrix = 1
gl_multiplier = 2
gl_screen_view = False
gl_debug = False
gl_output = 'image'
gl_counter = 0
gl_current_frame_pos = 0
gl_pattern = 'eu'
def __init__(self):
super(Lpc, self).__init__()
# region currently set to EU (Europe).
# Other regions include SG (Singapore) and US
self.alpr = Alpr('eu', 'openalpr.conf', 'runtime_data')
# Make sure the library loaded before continuing.
if not self.alpr.is_loaded():
print('Error loading OpenALPR')
sys.exit(1)
# Optionally, provide the library with a region for pattern matching.
# Example: md for maryland
#self.alpr.set_default_region('eu')
def config(self,
matrix=1,
multiplier=2,
screen_view=False,
debug=False,
output='image',
pattern='eu',
openALPR_config='openalpr.conf'):
self.gl_matrix = matrix
self.gl_multiplier = multiplier
self.gl_screen_view = screen_view
self.gl_debug = debug
self.gl_output = output
self.gl_pattern = pattern
self.alpr = Alpr(pattern, openALPR_config, 'runtime_data')
if self.gl_debug:
print '--------------------------------'
print '-----------DEBUG MODE-----------'
print '--------------------------------'
def unload(self):
# Call when completely done to release memory
self.alpr.unload()
def __create_save_path(self):
filename_without_ext, file_extension = os.path.splitext(
os.path.basename(self.gl_filepath))
if self.gl_current_frame_pos > 0:
file_extension = '.png'
if self.gl_output == 'video':
file_extension = '.avi'
if not os.path.exists(
os.path.dirname(self.gl_filepath) + '/censored/'):
os.makedirs(os.path.dirname(self.gl_filepath) + '/censored/')
save_path = os.path.dirname(
self.gl_filepath) + '/censored/' + filename_without_ext + '_' + str(
self.gl_current_frame_pos) + '_censored_' + str(
datetime.datetime.now().strftime(
'%Y-%m-%d_%H-%M-%S')) + file_extension
return save_path
def __write_log(self, filepath):
if os.path.exists('./logs/log.txt'):
append_write = 'a' # append if already exists
else:
append_write = 'w' # make a new file if not
max_plate_width_percent = ''
max_plate_height_percent = ''
detection_iteration_increase = ''
detection_strictness = ''
max_detection_input_width = ''
max_detection_input_height = ''
postprocess_min_confidence = ''
postprocess_confidence_skip_level = ''
# get openALPR config file info
with open('openalpr.conf') as f:
content = f.readlines()
content = [x.strip() for x in content]
for line in content:
if 'max_plate_width_percent' in line:
max_plate_width_percent = line
if 'max_plate_height_percent' in line:
max_plate_height_percent = line
if 'detection_iteration_increase' in line:
detection_iteration_increase = line
if 'detection_strictness' in line:
detection_strictness = line
if 'max_detection_input_width' in line:
max_detection_input_width = line
if 'max_detection_input_height' in line:
max_detection_input_height = line
if 'postprocess_min_confidence' in line:
postprocess_min_confidence = line
if 'postprocess_confidence_skip_level' in line:
postprocess_confidence_skip_level = line
log = open('./logs/log.txt', append_write)
log.write('-------------------------------' + '\n')
log.write('Date: ' + str(datetime.datetime.now()) + '\n')
log.write('File: ' + self.gl_filepath + '\n')
log.write('Censored File: ' + filepath + '\n')
log.write('Matrix Size: ' + str(self.gl_matrix) + '\n')
log.write('Multiplier: ' + str(self.gl_multiplier) + '\n')
log.write('Pattern: ' + str(self.gl_pattern) + '\n')
log.write('Output: ' + self.gl_output + '\n')
log.write('Total LPs found: ' + str(self.gl_counter) + '\n')
log.write('ScrennView: ' + str(self.gl_screen_view) + '\n')
log.write('Debug: ' + str(self.gl_debug) + '\n')
log.write('openALPR config:' '\n')
log.write(' ' + max_plate_width_percent + '\n')
log.write(' ' + max_plate_height_percent + '\n')
log.write(' ' + detection_iteration_increase + '\n')
log.write(' ' + detection_strictness + '\n')
log.write(' ' + max_detection_input_width + '\n')
log.write(' ' + max_detection_input_height + '\n')
log.write(' ' + postprocess_min_confidence + '\n')
log.write(' ' + postprocess_confidence_skip_level + '\n')
log.write('-------------------------------' + '\n')
log.close()
def __search_and_censor(self, ndarray_image):
if not ndarray_image.size:
print 'file does not exist'
sys.exit(1)
analyzed_file = self.alpr.recognize_ndarray(ndarray_image)
if not analyzed_file['results']:
print 'No license plate(s) detected | total: ' + str(
self.gl_counter)
# sys.exit(1)
if analyzed_file['results']:
self.gl_counter = self.gl_counter + len(analyzed_file['results'])
print 'found ' + str(len(analyzed_file['results'])
) + ' licenece plate(s) | total: ' + str(
self.gl_counter)
# loop through the results
for result in analyzed_file['results']:
# x,y coordinates of opposite corners of license plate
x1 = result['coordinates'][0]['x']
y1 = result['coordinates'][0]['y']
x2 = result['coordinates'][2]['x']
y2 = result['coordinates'][2]['y']
true_x1 = x1 / self.gl_multiplier
true_y1 = y1 / self.gl_multiplier
true_x2 = x2 / self.gl_multiplier
true_y2 = y2 / self.gl_multiplier
# blurred plate
plate = self.gl_original_image[true_y1:true_y2, true_x1:true_x2]
blured_plate = cv2.GaussianBlur(plate, (19, 19), 4, 4, 0)
self.gl_original_image[true_y1:true_y2, true_x1:
true_x2] = blured_plate
if self.gl_debug:
# pink bar
cv2.rectangle(self.gl_original_image, (true_x1, true_y1),
(true_x2, true_y2), (255, 0, 251), -1)
return self.gl_original_image
# TODO: should not return resized image but true coordinates where to censore the image
def __tile_and_merge(self, ndarray_image, matrix=1):
censored_tiles = []
if not ndarray_image.size:
print 'file does not exist'
sys.exit(1)
height = int(ndarray_image.shape[0] / self.gl_matrix)
width = int(ndarray_image.shape[1] / self.gl_matrix)
print 'tile image in ' + str(self.gl_matrix) + 'x' + str(
self.gl_matrix) + ' matrix'
for row in range(self.gl_matrix):
for col in range(self.gl_matrix):
y0 = row * height
y1 = y0 + height
x0 = col * width
x1 = x0 + width
print 'start censoring tile ' + str(row) + 'x' + str(col)
censored_tile = self.__search_and_censor(
ndarray_image[y0:y1, x0:x1])
censored_tiles.append(censored_tile)
print 'merging tiles'
tile_row_array = []
tile_row = []
count = 0
for i, c_tile in enumerate(censored_tiles):
count += 1
tile_row_array.append(c_tile)
if (count == self.gl_matrix):
tmp_row = np.concatenate(tile_row_array, axis=1)
tile_row.append(tmp_row)
tile_row_array = []
count = 0
return np.concatenate(tile_row, axis=0)
def censor_image(self, filepath):
if not type(filepath) is np.ndarray:
self.gl_filepath = filepath
img = cv2.imread(filepath)
else:
img = filepath
height = int(img.shape[0])
width = int(img.shape[1])
self.gl_original_image = img
print 'resize image (x' + str(
self.gl_multiplier) + '): ' + self.gl_filepath + ' Frame: ' + str(
self.gl_current_frame_pos)
img = cv2.resize(
img, (width * self.gl_multiplier, height * self.gl_multiplier))
print 'new image size is: ' + str(int(img.shape[1])) + 'x' + str(
int(img.shape[0])) + ' px'
if self.gl_matrix == 1:
merged_img = self.__search_and_censor(img)
elif self.gl_matrix > 1:
merged_img = self.__tile_and_merge(img, self.gl_matrix)
if self.gl_output == 'image':
save_path = self.__create_save_path()
cv2.imwrite(save_path, merged_img)
print 'saved file in: ' + save_path
self.__write_log(save_path)
# show image on screen if gl_screen_view is true
if self.gl_screen_view:
cv2.namedWindow('ImageWindow', cv2.WINDOW_NORMAL)
cv2.resizeWindow('ImageWindow', np.size(merged_img, 1),
np.size(merged_img, 0))
cv2.imshow('ImageWindow', merged_img)
cv2.waitKey(0)
elif self.gl_output == 'video':
return merged_img
def censor_video(self, filepath):
self.gl_filepath = filepath
cap = cv2.VideoCapture(filepath)
# get video file information
width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
framerate = cap.get(cv2.cv.CV_CAP_PROP_FPS)
# build output path/name
save_path = self.__create_save_path()
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter(save_path, fourcc, framerate,
(int(width), int(height)))
while not cap.isOpened():
cap = cv2.VideoCapture(filepath)
cv2.waitKey(1000)
print 'Wait for the header'
self.gl_current_frame_pos = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
flag, frame = cap.read()
if flag:
# The frame is ready and already captured
self.gl_current_frame_pos = cap.get(
cv2.cv.CV_CAP_PROP_POS_FRAMES)
# pass frame to image censoring method
censored_frame = self.censor_image(frame)
# switch between video file and image stack
if self.gl_output == 'video':
out.write(censored_frame)
if self.gl_output == 'image':
save_path = self.__create_save_path()
cv2.imwrite(save_path, censored_frame)
print 'saved file in: ' + save_path
self.__write_log(save_path)
# show frame on screen if gl_screen_view is true
if self.gl_screen_view:
cv2.namedWindow('VideoWindow', cv2.WINDOW_NORMAL)
cv2.resizeWindow('VideoWindow', np.size(censored_frame, 1),
np.size(censored_frame, 0))
cv2.imshow('VideoWindow', censored_frame)
else:
# The next frame is not ready, so we try to read it again
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,
self.gl_current_frame_pos - 1)
print 'frame is not ready'
# It is better to wait for a while for the next frame to be ready
cv2.waitKey(1000)
if cv2.waitKey(10) == 27:
break
if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(
cv2.cv.CV_CAP_PROP_FRAME_COUNT):
# If the number of captured frames is equal
# to the total number of frames, we stop
break
# release // cleanup
cap.release()
out.release()
cv2.destroyAllWindows()
self.__write_log(save_path)
print 'total license plates counted: ' + str(self.gl_counter)
print 'saved censored video file in: ' + save_path