-
Notifications
You must be signed in to change notification settings - Fork 2
/
temp.py
355 lines (283 loc) · 13.9 KB
/
temp.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
344
345
346
347
348
349
350
351
352
353
354
355
import os
from pathlib import Path
from typing import Union
import torch
import cv2 as cv
import numpy as np
from deep_sort_realtime.deepsort_tracker import DeepSort
from models.experimental import attempt_load
from utils.general import check_img_size
from utils.torch_utils import select_device, TracedModel
from utils.datasets import letterbox
from utils.general import non_max_suppression, scale_coords
from utils.plots import plot_one_box, plot_one_box_PIL
from copy import deepcopy
import easyocr
images_n_vids_path = "C:/PyProjects/ANPRir/ANPRir/images_vids"
image_path = os.path.join(images_n_vids_path, "pelakir_2.jpg")
video_path = os.path.join(images_n_vids_path, "test_video_short.mp4")
savepath = "C:/PyProjects/ANPRir/ANPRir/sidebar"
weights = 'weights/best.pt'
device_id = 'cpu'
image_size = 640
trace = True
# Initialize
device = select_device(device_id)
half = device.type != 'cpu' # half precision only supported on CUDA
# Load model
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(image_size, s=stride) # check img_size
if trace:
model = TracedModel(model, device, image_size)
if half:
model.half() # to FP16
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
# Load OCR
reader = easyocr.Reader(['fa'])
def detect_plate(source_image):
# Padded resize
img_size = 640
stride = 32
img = letterbox(source_image, img_size, stride=stride)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
with torch.no_grad():
# Inference
pred = model(img, augment=True)[0]
# Apply NMS
pred = non_max_suppression(pred, 0.25, 0.45, classes=0, agnostic=True)
plate_detections = []
det_confidences = []
# Process detections
for i, det in enumerate(pred): # detections per image
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], source_image.shape).round()
# Return results
for *xyxy, conf, cls in reversed(det):
coords = [int(position) for position in (torch.tensor(xyxy).view(1, 4)).tolist()[0]]
plate_detections.append(coords)
det_confidences.append(conf.item())
return plate_detections, det_confidences
def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=2.0, threshold=0):
blurred = cv.GaussianBlur(image, kernel_size, sigma)
sharpened = float(amount + 1) * image - float(amount) * blurred
sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
sharpened = sharpened.round().astype(np.uint8)
if threshold > 0:
low_contrast_mask = np.absolute(image - blurred) < threshold
np.copyto(sharpened, image, where=low_contrast_mask)
return sharpened
def crop(image, coord):
cropped_image = image[int(coord[1]):int(coord[3]), int(coord[0]):int(coord[2])]
return cropped_image
def ocr_plate(plate_region):
# Image pre-processing for more accurate OCR
cv.imwrite(os.path.join(savepath, "plate_img.png"), plate_region)
rescaled = cv.resize(plate_region, None, fx=1.2, fy=1.2, interpolation=cv.INTER_CUBIC)
grayscale = cv.cvtColor(rescaled, cv.COLOR_BGR2GRAY)
# OCR the preprocessed image
grayscale_blur = cv.medianBlur(grayscale, 1)
ret, thresh1 = cv.threshold(grayscale_blur, 120, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv.imwrite(os.path.join(savepath, "grayscale_blur.png"), grayscale_blur)
plate_text_easyocr = reader.readtext(grayscale_blur)
if plate_text_easyocr:
(bbox, text_easyocr, ocr_confidence) = plate_text_easyocr[0]
print("plate_text Easyocr ", text_easyocr)
else:
text_easyocr = "_"
ocr_confidence = 0
#if ocr_confidence == 'nan':
return text_easyocr, ocr_confidence
def get_plates_from_image(input):
if input is None:
return None
plate_detections, det_confidences = detect_plate(input)
plate_texts = []
ocr_confidences = []
detected_image = deepcopy(input)
for coords in plate_detections:
plate_region = crop(input, coords)
plate_text, ocr_confidence = ocr_plate(plate_region)
plate_texts.append(plate_text)
ocr_confidences.append(ocr_confidence)
detected_image = plot_one_box_PIL(coords, detected_image, label=plate_text, color=[0, 150, 255], line_thickness=2)
return detected_image
def pascal_voc_to_coco(x1y1x2y2):
x1, y1, x2, y2 = x1y1x2y2
return [x1, y1, x2 - x1, y2 - y1]
def get_best_ocr(preds, rec_conf, ocr_res, track_id):
for info in preds:
# Check if it is current track id
if info['track_id'] == track_id:
# Check if the ocr confidenence is maximum or not
if info['ocr_conf'] < rec_conf:
info['ocr_conf'] = rec_conf
info['ocr_txt'] = ocr_res
else:
rec_conf = info['ocr_conf']
ocr_res = info['ocr_txt']
break
return preds, rec_conf, ocr_res
def get_plates_from_video(source):
if source is None:
return None
# Create a VideoCapture object
video = cv.VideoCapture(source)
# Default resolutions of the frame are obtained. The default resolutions are system dependent.
# We convert the resolutions from float to integer.
width = int(video.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv.CAP_PROP_FPS)
# Define the codec and create VideoWriter object.
temp = f'{Path(source).stem}_temp{Path(source).suffix}'
export = cv.VideoWriter(temp, cv.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
# Intializing tracker
tracker = DeepSort(embedder_gpu=False)
# Initializing some helper variables.
preds = []
total_obj = 0
while(True):
ret, frame = video.read()
if ret == True:
# Run the ANPR algorithm
bboxes, scores = detect_plate(frame)
# Convert Pascal VOC detections to COCO
bboxes = list(map(lambda bbox: pascal_voc_to_coco(bbox), bboxes))
if len(bboxes) > 0:
# Storing all the required info in a list.
detections = [(bbox, score, 'number_plate') for bbox, score in zip(bboxes, scores)]
# Applying tracker.
# The tracker code flow: kalman filter -> target association(using hungarian algorithm) and appearance descriptor.
tracks = tracker.update_tracks(detections, frame=frame)
# Checking if tracks exist.
for track in tracks:
if not track.is_confirmed() or track.time_since_update > 1:
continue
# Changing track bbox to top left, bottom right coordinates
bbox = [int(position) for position in list(track.to_tlbr())]
for i in range(len(bbox)):
if bbox[i] < 0:
bbox[i] = 0
# Cropping the license plate and applying the OCR.
plate_region = crop(frame, bbox)
plate_text, ocr_confidence = ocr_plate(plate_region)
# Storing the ocr output for corresponding track id.
output_frame = {'track_id': track.track_id, 'ocr_txt': plate_text, 'ocr_conf': ocr_confidence}
# Appending track_id to list only if it does not exist in the list
# else looking for the current track in the list and updating the highest confidence of it.
if track.track_id not in list(set(pred['track_id'] for pred in preds)):
total_obj += 1
preds.append(output_frame)
else:
preds, ocr_confidence, plate_text = get_best_ocr(preds, ocr_confidence, plate_text, track.track_id)
# Plotting the prediction.
frame = plot_one_box_PIL(bbox, frame, label=f'{str(track.track_id)}. {plate_text}', color=[255, 150, 0], line_thickness=3)
cv.imshow("frame ", frame)
keyexit = cv.waitKey(0)
if keyexit == 27:
break
# Write the frame into the output file
export.write(frame)
else:
break
# When everything done, release the video capture and video write objects
cv.destroyAllWindows()
video.release()
export.release()
# Compressing the output video for smaller size and web compatibility.
output = f'{Path(source).stem}_detected{Path(source).suffix}'
os.system(f'ffmpeg -y -i {temp} -c:v libx264 -b:v 5000k -minrate 1000k -maxrate 8000k -pass 1 -c:a aac -f mp4 /dev/null && ffmpeg -i {temp} -c:v libx264 -b:v 5000k -minrate 1000k -maxrate 8000k -pass 2 -c:a aac -movflags faststart {output}')
os.system(f'rm -rf {temp} ffmpeg2pass-0.log ffmpeg2pass-0.log.mbtree')
return output
def get_plates_from_webcam():
# Create a VideoCapture object
video = cv.VideoCapture(0)
# Default resolutions of the frame are obtained. The default resolutions are system dependent.
# We convert the resolutions from float to integer.
width = int(video.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv.CAP_PROP_FPS)
# Define the codec and create VideoWriter object.
temp = f'cam_temp.mp4'
export = cv.VideoWriter(temp, cv.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
# Intializing tracker
tracker = DeepSort(embedder_gpu=False)
# Initializing some helper variables.
preds = []
total_obj = 0
fr_count = 0
while(True):
ret, frame = video.read()
if ret == True:
fr_count+=1
if fr_count % 10 !=0:
continue
# Run the ANPR algorithm
bboxes, scores = detect_plate(frame)
# Convert Pascal VOC detections to COCO
bboxes = list(map(lambda bbox: pascal_voc_to_coco(bbox), bboxes))
if len(bboxes) > 0:
# Storing all the required info in a list.
detections = [(bbox, score, 'number_plate') for bbox, score in zip(bboxes, scores)]
# Applying tracker.
# The tracker code flow: kalman filter -> target association(using hungarian algorithm) and appearance descriptor.
tracks = tracker.update_tracks(detections, frame=frame)
# Checking if tracks exist.
for track in tracks:
if not track.is_confirmed() or track.time_since_update > 1:
continue
# Changing track bbox to top left, bottom right coordinates
bbox = [int(position) for position in list(track.to_tlbr())]
for i in range(len(bbox)):
if bbox[i] < 0:
bbox[i] = 0
# Cropping the license plate and applying the OCR.
plate_region = crop(frame, bbox)
plate_text, ocr_confidence = ocr_plate(plate_region)
# Storing the ocr output for corresponding track id.
output_frame = {'track_id': track.track_id, 'ocr_txt': plate_text, 'ocr_conf': ocr_confidence}
# Appending track_id to list only if it does not exist in the list
# else looking for the current track in the list and updating the highest confidence of it.
if track.track_id not in list(set(pred['track_id'] for pred in preds)):
total_obj += 1
preds.append(output_frame)
else:
preds, ocr_confidence, plate_text = get_best_ocr(preds, ocr_confidence, plate_text, track.track_id)
# Plotting the prediction.
frame = plot_one_box_PIL(bbox, frame, label=f'{str(track.track_id)}. {plate_text}', color=[255, 150, 0], line_thickness=3)
cv.imshow("frame ", frame)
keyexit = cv.waitKey(0)
if keyexit == 27:
break
# Write the frame into the output file
export.write(frame)
else:
break
# When everything done, release the video capture and video write objects
cv.destroyAllWindows()
video.release()
export.release()
# Compressing the output video for smaller size and web compatibility.
output = f'cam_detected.mp4'
os.system(f'ffmpeg -y -i {temp} -c:v libx264 -b:v 5000k -minrate 1000k -maxrate 8000k -pass 1 -c:a aac -f mp4 /dev/null && ffmpeg -i {temp} -c:v libx264 -b:v 5000k -minrate 1000k -maxrate 8000k -pass 2 -c:a aac -movflags faststart {output}')
os.system(f'rm -rf {temp} ffmpeg2pass-0.log ffmpeg2pass-0.log.mbtree')
return output
# pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
# plate_image = cv.imread(image_path)
# detected_plate_image = get_plates_from_image(plate_image)
# cv.imwrite(os.path.join(savepath, "detected_plate.png"), detected_plate_image)
# cv.imshow("detected_plate_image",detected_plate_image)
# cv.waitKey(0)
# cv.destroyAllWindows
# detected_plate_image = get_plates_from_video(video_path)
detected_plate_webcam = get_plates_from_webcam()