-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
382 lines (311 loc) · 15.7 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import os
import sys
import cv2
import time
import torch
import argparse
import numpy as np
from pathlib import Path
from collections import Counter
import torch.backends.cudnn as cudnn
from utils.general import set_logging
from models.common import DetectMultiBackend
from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, Profile, check_file, check_img_size,
check_imshow, check_requirements, colorstr, cv2,
increment_path, non_max_suppression, print_args,
scale_coords, strip_optimizer, xyxy2xywh)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, time_sync
import easyocr
import psycopg2 # from database
import time
from datetime import datetime as ddt
from datetime import timezone as tz
# To store in database
# Establishing the connection
# conn = psycopg2.connect(database="Surveillance", user='postgres', password='admin12345', host='127.0.0.1', port='5432')
# cursor = conn.cursor()
##### DEFINING GLOBAL VARIABLE FOR OCR
EASY_OCR = easyocr.Reader(['en']) ### initiating easyocr
OCR_TH = 0.1
# from google_drive_ocr import GoogleOCRApplication
# app = GoogleOCRApplication('client_secret.json')
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT))
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))
# ---------------Object Tracking---------------
import skimage
from sort import *
# -----------Object Blurring-------------------
blurratio = 40
# .................. Tracker Functions .................
'''Computer Color for every box and track'''
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
def compute_color_for_labels(label):
color = [int(int(p * (label ** 2 - label + 1)) % 255) for p in palette]
return tuple(color)
"""" Calculates the relative bounding box from absolute pixel values. """
def bbox_rel(*xyxy):
bbox_left = min([xyxy[0].item(), xyxy[2].item()])
bbox_top = min([xyxy[1].item(), xyxy[3].item()])
bbox_w = abs(xyxy[0].item() - xyxy[2].item())
bbox_h = abs(xyxy[1].item() - xyxy[3].item())
x_c = (bbox_left + bbox_w / 2)
y_c = (bbox_top + bbox_h / 2)
w = bbox_w
h = bbox_h
return x_c, y_c, w, h
def recognize_plate_easyocr(img, coords, reader, region_threshold):
# separate coordinates from box
xmin, ymin, xmax, ymax = coords
# get the subimage that makes up the bounded region and take an additional 5 pixels on each side
# nplate = img[int(ymin)-5:int(ymax)+5, int(xmin)-5:int(xmax)+5]
nplate = img[int(ymin):int(ymax), int(xmin):int(xmax)] ### cropping the number plate from the whole image
ocr_result = reader.readtext(nplate)
text = filter_text(region=nplate, ocr_result=ocr_result, region_threshold=region_threshold)
if len(text) == 1:
text = text[0].upper()
return text
### to filter out wrong detections
def filter_text(region, ocr_result, region_threshold):
rectangle_size = region.shape[0] * region.shape[1]
plate = []
# print(ocr_result)
for result in ocr_result:
length = np.sum(np.subtract(result[0][1], result[0][0]))
height = np.sum(np.subtract(result[0][2], result[0][1]))
if length * height / rectangle_size > region_threshold:
plate.append(result[1])
return plate
"""Function to Draw Bounding boxes"""
def draw_boxes(img, bbox, plate_num, identities=None, categories=None, names=None, color_box=None, offset=(0, 0)):
for i, box in enumerate(bbox):
x1, y1, x2, y2 = [int(i) for i in box]
x1 += offset[0]
x2 += offset[0]
y1 += offset[1]
y2 += offset[1]
coords = [x1, y1, x2, y2]
cat = int(categories[i]) if categories is not None else 0
id = int(identities[i]) if identities is not None else 0
data = (int((box[0] + box[2]) / 2), (int((box[1] + box[3]) / 2)))
label = str(id)
if color_box:
color = compute_color_for_labels(id)
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
cv2.rectangle(img, (x1, y1 - 20), (x1 + w, y1), (255, 191, 0), -1)
cv2.putText(img, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
[255, 255, 255], 1)
# cv2.circle(img, data, 3, color,-1)
else:
(w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 191, 0), 2)
cv2.rectangle(img, (x1, y1 - 20), (x1 + w, y1), (255, 191, 0), -1)
cv2.putText(img, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
[255, 255, 255], 1)
# cv2.putText(img, plate_num, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
# [0, 0, 255], 2)
# cv2.circle(img, data, 3, (255,191,0),-1)
return img
# ..............................................................................
check_num = []
@torch.no_grad()
def detect(weights=ROOT / 'yolov5n.pt',
source=ROOT / 'yolov5/data/images',
data=ROOT / 'yolov5/data/coco128.yaml',
imgsz=(640, 640), conf_thres=0.25, iou_thres=0.45,
max_det=1000, device='cpu', view_img=False,
save_txt=False, save_conf=False, save_crop=True,
nosave=False, classes=None, agnostic_nms=False,
augment=False, visualize=False, update=False,
project=ROOT / 'runs/detect', name='exp',
exist_ok=False, line_thickness=2, hide_labels=False,
hide_conf=False, half=False, dnn=False, display_labels=False,
blur_obj=False, color_box=False, ):
save_img = not nosave and not source.endswith('.txt')
# .... Initialize SORT ....
sort_max_age = 5
sort_min_hits = 2
sort_iou_thresh = 0.2
sort_tracker = Sort(max_age=sort_max_age,
min_hits=sort_min_hits,
iou_threshold=sort_iou_thresh)
track_color_id = 0
# .........................
webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith(
('rtsp://', 'rtmp://', 'http://', 'https://'))
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True)
set_logging()
device = select_device(device)
half &= device.type != 'cpu'
device = select_device(device)
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data)
stride, names, pt, jit, onnx, engine = model.stride, model.names, model.pt, model.jit, model.onnx, model.engine
imgsz = check_img_size(imgsz, s=stride)
half &= (pt or jit or onnx or engine) and device.type != 'cpu'
if pt or jit:
model.model.half() if half else model.model.float()
if webcam:
cudnn.benchmark = True
dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt)
bs = len(dataset)
else:
dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
bs = 1
vid_path, vid_writer = [None] * bs, [None] * bs
t0 = time.time()
dt, seen = [0.0, 0.0, 0.0], 0
for path, im, im0s, vid_cap, s in dataset:
t1 = time_sync()
im = torch.from_numpy(im).to(device)
im = im.half() if half else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
t2 = time_sync()
dt[0] += t2 - t1
# Inference
visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
pred = model(im, augment=augment, visualize=visualize)
t3 = time_sync()
dt[1] += t3 - t2
# NMS
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
dt[2] += time_sync() - t3
for i, det in enumerate(pred): # per image
seen += 1
if webcam: # batch_size >= 1
p, im0, frame = path[i], im0s[i].copy(), dataset.count
s += f'{i}: '
else:
p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p)
save_path = str(save_dir / p.name)
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # im.txt
s += '%gx%g ' % im.shape[2:]
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]
imc = im0.copy() if save_crop else im0
annotator = Annotator(im0, line_width=line_thickness, example=str(names))
if len(det):
det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum()
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, "
# Write results
for *xyxy, conf, cls in reversed(det):
if blur_obj:
crop_obj = im0[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2])]
blur = cv2.blur(crop_obj, (blurratio, blurratio))
im0[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2])] = blur
else:
continue
# ..................USE TRACK FUNCTION....................
# pass an empty array to sort
dets_to_sort = np.empty((0, 6))
# NOTE: We send in detected object class too
for x1, y1, x2, y2, conf, detclass in det.cpu().detach().numpy():
dets_to_sort = np.vstack((dets_to_sort,
np.array([x1, y1, x2, y2,
conf, detclass])))
# Run SORT
tracked_dets = sort_tracker.update(dets_to_sort)
tracks = sort_tracker.getTrackers()
# loop over tracks
for track in tracks:
if color_box:
color = compute_color_for_labels(track_color_id)
track_color_id = track_color_id + 1
# draw boxes for visualization
if len(tracked_dets) > 0:
bbox_xyxy = tracked_dets[:, :4]
identities = tracked_dets[:, 8]
categories = tracked_dets[:, 4]
for i in range(len(bbox_xyxy)):
if identities[i] not in check_num:
check_num.append(identities[i])
plate_num = recognize_plate_easyocr(img=im0, coords=bbox_xyxy[i], reader=EASY_OCR,
region_threshold=OCR_TH)
plate_num=""
# print(bbox_xyxy[i])
if plate_num:
# print(plate_num)
dte = ddt.now(tz.utc)
# inserting values into the db
# cursor.execute('insert into TEST values (DEFAULT, %s, %s)', (plate_num, dte))
draw_boxes(im0, bbox_xyxy, str(plate_num), identities, categories, names, color_box)
# if view_img:
# cv2.imshow(str(p), im0)
# cv2.waitKey(1)
if save_img:
if dataset.mode == 'image':
cv2.imwrite(save_path, im0)
else:
if vid_path != save_path:
vid_path = save_path
if isinstance(vid_writer, cv2.VideoWriter):
vid_writer.release()
if vid_cap:
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else:
fps, w, h = 30, im0.shape[1], im0.shape[0]
save_path += '.mp4'
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer.write(im0)
# print("Frame Processing!")
# print("Video Exported Success")
# conn.commit()
# print("Records inserted........")
# # Closing the connection
# conn.close()
# if update:
# strip_optimizer(weights)
if vid_cap:
vid_cap.release()
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--view-img', action='store_true', help='show results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--visualize', action='store_true', help='visualize features')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default=ROOT / 'runs/detect', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
parser.add_argument('--blur-obj', action='store_true', help='Blur Detected Objects')
parser.add_argument('--color-box', action='store_true', help='Change color of every box and track')
opt = parser.parse_args()
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
print_args(vars(opt))
return opt
def main(opt):
check_requirements(exclude=('tensorboard', 'thop'))
detect(**vars(opt))
if __name__ == "__main__":
opt = parse_opt()
main(opt)
# "rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0"