-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
858 lines (712 loc) · 33.7 KB
/
utils.py
File metadata and controls
858 lines (712 loc) · 33.7 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
import torch
import math
import cv2
import os
import glob
import time
import torchvision
import random
from pathlib import Path
import numpy as np
import torch.nn as nn
from tqdm import tqdm
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
from YOLOP_arch import Hardswish, autopad, Detect, YOLOP
from utils_stream import LoadStreams
normalize = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
transform=transforms.Compose([
transforms.ToTensor(),
normalize,
])
img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.dng']
vid_formats = ['.mov', '.avi', '.mp4', '.mpg', '.mpeg', '.m4v', '.wmv', '.mkv']
class DepthSeperabelConv2d(nn.Module):
"""
DepthSeperable Convolution 2d with residual connection
"""
def __init__(self, inplanes, planes, kernel_size=3, stride=1, downsample=None, act=True):
super(DepthSeperabelConv2d, self).__init__()
self.depthwise = nn.Sequential(
nn.Conv2d(inplanes, inplanes, kernel_size, stride=stride, groups=inplanes, padding=kernel_size//2, bias=False),
nn.BatchNorm2d(inplanes, momentum=BN_MOMENTUM)
)
# self.depthwise = nn.Conv2d(inplanes, inplanes, kernel_size, stride=stride, groups=inplanes, padding=1, bias=False)
# self.pointwise = nn.Conv2d(inplanes, planes, 1, bias=False)
self.pointwise = nn.Sequential(
nn.Conv2d(inplanes, planes, 1, bias=False),
nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
)
self.downsample = downsample
self.stride = stride
try:
self.act = Hardswish() if act else nn.Identity()
except:
self.act = nn.Identity()
def forward(self, x):
#residual = x
out = self.depthwise(x)
out = self.act(out)
out = self.pointwise(out)
if self.downsample is not None:
residual = self.downsample(x)
out = self.act(out)
return out
class SharpenConv(nn.Module):
# SharpenConv convolution
def __init__(self, c1, c2, k=3, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
super(SharpenConv, self).__init__()
sobel_kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32')
kenel_weight = np.vstack([sobel_kernel]*c2*c1).reshape(c2,c1,3,3)
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
self.conv.weight.data = torch.from_numpy(kenel_weight)
self.conv.weight.requires_grad = False
self.bn = nn.BatchNorm2d(c2)
try:
self.act = Hardswish() if act else nn.Identity()
except:
self.act = nn.Identity()
def forward(self, x):
return self.act(self.bn(self.conv(x)))
def fuseforward(self, x):
return self.act(self.conv(x))
class MCnet(nn.Module):
def __init__(self, block_cfg, **kwargs):
super(MCnet, self).__init__()
layers, save= [], []
self.nc = 1
self.detector_index = -1
self.det_out_idx = block_cfg[0][0]
self.seg_out_idx = block_cfg[0][1:]
# Build model
for i, (from_, block, args) in enumerate(block_cfg[1:]):
block = eval(block) if isinstance(block, str) else block # eval strings
if block is Detect:
self.detector_index = i
block_ = block(*args)
block_.index, block_.from_ = i, from_
layers.append(block_)
save.extend(x % i for x in ([from_] if isinstance(from_, int) else from_) if x != -1) # append to savelist
assert self.detector_index == block_cfg[0][0]
self.model, self.save = nn.Sequential(*layers), sorted(save)
self.names = [str(i) for i in range(self.nc)]
# set stride、anchor for detector
Detector = self.model[self.detector_index] # detector
if isinstance(Detector, Detect):
s = 128 # 2x min stride
# for x in self.forward(torch.zeros(1, 3, s, s)):
# print (x.shape)
with torch.no_grad():
model_out = self.forward(torch.zeros(1, 3, s, s))
detects, _, _= model_out
Detector.stride = torch.tensor([s / x.shape[-2] for x in detects]) # forward
# print("stride"+str(Detector.stride ))
Detector.anchors /= Detector.stride.view(-1, 1, 1) # Set the anchors for the corresponding scale
check_anchor_order(Detector)
self.stride = Detector.stride
self._initialize_biases()
initialize_weights(self)
def forward(self, x):
cache = []
out = []
det_out = None
Da_fmap = []
LL_fmap = []
for i, block in enumerate(self.model):
if block.from_ != -1:
x = cache[block.from_] if isinstance(block.from_, int) else [x if j == -1 else cache[j] for j in block.from_] #calculate concat detect
x = block(x)
if i in self.seg_out_idx: #save driving area segment result
m=nn.Sigmoid()
out.append(m(x))
if i == self.detector_index:
det_out = x
cache.append(x if block.index in self.save else None)
out.insert(0,det_out)
return out
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
# https://arxiv.org/abs/1708.02002 section 3.3
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
# m = self.model[-1] # Detect() module
m = self.model[self.detector_index] # Detect() module
for mi, s in zip(m.m, m.stride): # from
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
def check_anchor_order(m):
# Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
a = m.anchor_grid.prod(-1).view(-1) # anchor area
da = a[-1] - a[0] # delta a
ds = m.stride[-1] - m.stride[0] # delta s
if da.sign() != ds.sign(): # same order
print('Reversing anchor order')
m.anchors[:] = m.anchors.flip(0)
m.anchor_grid[:] = m.anchor_grid.flip(0)
def initialize_weights(model):
for m in model.modules():
t = type(m)
if t is nn.Conv2d:
pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif t is nn.BatchNorm2d:
m.eps = 1e-3
m.momentum = 0.03
elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
# elif t in [nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
m.inplace = True
def get_net():
m_block_cfg = YOLOP
model = MCnet(m_block_cfg)
return model
def time_synchronized():
torch.cuda.synchronize() if torch.cuda.is_available() else None
return time.time()
def letterbox_for_img(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
shape = img.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup: # only scale down, do not scale up (for better test mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
if auto: # minimum rectangle
dw, dh = np.mod(dw, 32), np.mod(dh, 32) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_AREA)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return img, ratio, (dw, dh)
class LoadImages: # for inference
def __init__(self, path, img_size=640):
p = str(Path(path)) # os-agnostic
p = os.path.abspath(p) # absolute path
if '*' in p:
files = sorted(glob.glob(p, recursive=True)) # glob
elif os.path.isdir(p):
files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
elif os.path.isfile(p):
files = [p] # files
else:
raise Exception('ERROR: %s does not exist' % p)
images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats]
videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats]
ni, nv = len(images), len(videos)
self.img_size = img_size
self.files = images + videos
self.nf = ni + nv # number of files
self.video_flag = [False] * ni + [True] * nv
self.mode = 'images'
if any(videos):
self.new_video(videos[0]) # new video
else:
self.cap = None
assert self.nf > 0, 'No images or videos found in %s. Supported formats are:\nimages: %s\nvideos: %s' % \
(p, img_formats, vid_formats)
def __iter__(self):
self.count = 0
return self
def __next__(self):
if self.count == self.nf:
raise StopIteration
path = self.files[self.count]
if self.video_flag[self.count]:
# Read video
self.mode = 'video'
ret_val, img0 = self.cap.read()
if not ret_val:
self.count += 1
self.cap.release()
if self.count == self.nf: # last video
raise StopIteration
else:
path = self.files[self.count]
self.new_video(path)
ret_val, img0 = self.cap.read()
h0, w0 = img0.shape[:2]
self.frame += 1
# print('\n video %g/%g (%g/%g) %s: ' % (self.count + 1, self.nf, self.frame, self.nframes, path), end='')
else:
# Read image
self.count += 1
img0 = cv2.imread(path, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) # BGR
#img0 = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)
assert img0 is not None, 'Image Not Found ' + path
# print('image %g/%g %s: \n' % (self.count, self.nf, path), end='')
h0, w0 = img0.shape[:2]
# Padded resize
img, ratio, pad = letterbox_for_img(img0, new_shape=self.img_size, auto=True)
h, w = img.shape[:2]
shapes = (h0, w0), ((h / h0, w / w0), pad)
# Convert
#img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
# cv2.imwrite(path + '.letterbox.jpg', 255 * img.transpose((1, 2, 0))[:, :, ::-1]) # save letterbox image
return path, img, img0, self.cap, shapes
def new_video(self, path):
self.frame = 0
self.cap = cv2.VideoCapture(path)
self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
def __len__(self):
return self.nf # number of files
def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, labels=()):
"""Performs Non-Maximum Suppression (NMS) on inference results
Returns:
detections with shape: nx6 (x1, y1, x2, y2, conf, cls)
"""
nc = prediction.shape[2] - 5 # number of classes
xc = prediction[..., 4] > conf_thres # candidates
# Settings
min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height
max_det = 300 # maximum number of detections per image
max_nms = 30000 # maximum number of boxes into torchvision.ops.nms()
time_limit = 10.0 # seconds to quit after
redundant = True # require redundant detections
multi_label = nc > 1 # multiple labels per box (adds 0.5ms/img)
merge = False # use merge-NMS
t = time.time()
output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0]
for xi, x in enumerate(prediction): # image index, image inference
# Apply constraints
# x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height
x = x[xc[xi]] # confidence
# Cat apriori labels if autolabelling
if labels and len(labels[xi]):
l = labels[xi]
v = torch.zeros((len(l), nc + 5), device=x.device)
v[:, :4] = l[:, 1:5] # box
v[:, 4] = 1.0 # conf
v[range(len(l)), l[:, 0].long() + 5] = 1.0 # cls
x = torch.cat((x, v), 0)
# If none remain process next image
if not x.shape[0]:
continue
# Compute conf
x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf
# Box (center x, center y, width, height) to (x1, y1, x2, y2)
box = xywh2xyxy(x[:, :4])
# Detections matrix nx6 (xyxy, conf, cls)
if multi_label:
i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T
x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1)
else: # best class only
conf, j = x[:, 5:].max(1, keepdim=True)
x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres]
# Filter by class
if classes is not None:
x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]
# Apply finite constraint
# if not torch.isfinite(x).all():
# x = x[torch.isfinite(x).all(1)]
# Check shape
n = x.shape[0] # number of boxes
if not n: # no boxes
continue
elif n > max_nms: # excess boxes
x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence
# Batched NMS
c = x[:, 5:6] * (0 if agnostic else max_wh) # classes
boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores
i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS
if i.shape[0] > max_det: # limit detections
i = i[:max_det]
if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean)
# update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix
weights = iou * scores[None] # box weights
x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes
if redundant:
i = i[iou.sum(1) > 1] # require redundancy
output[xi] = x[i]
if (time.time() - t) > time_limit:
print(f'WARNING: NMS time limit {time_limit}s exceeded')
break # time limit exceeded
return output
def xywh2xyxy(x):
# Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
y = torch.zeros_like(x) if isinstance(x, torch.Tensor) else np.zeros_like(x)
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
return y
def show_seg_result(img, result, index, epoch, save_dir=None, is_ll=False,palette=None,is_demo=False,is_gt=False):
# img = mmcv.imread(img)
# img = img.copy()
# seg = result[0]
if palette is None:
palette = np.random.randint(
0, 255, size=(3, 3))
palette[0] = [0, 0, 0]
palette[1] = [0, 255, 0]
palette[2] = [255, 0, 0]
palette = np.array(palette)
assert palette.shape[0] == 3 # len(classes)
assert palette.shape[1] == 3
assert len(palette.shape) == 2
if not is_demo:
color_seg = np.zeros((result.shape[0], result.shape[1], 3), dtype=np.uint8)
for label, color in enumerate(palette):
color_seg[result == label, :] = color
else:
color_area = np.zeros((result[0].shape[0], result[0].shape[1], 3), dtype=np.uint8)
# for label, color in enumerate(palette):
# color_area[result[0] == label, :] = color
color_area[result[0] == 1] = [0, 255, 0]
color_area[result[1] ==1] = [255, 0, 0]
color_seg = color_area
# convert to BGR
color_seg = color_seg[..., ::-1]
# print(color_seg.shape)
color_mask = np.mean(color_seg, 2)
img[color_mask != 0] = img[color_mask != 0] * 0.5 + color_seg[color_mask != 0] * 0.5
# img = img * 0.5 + color_seg * 0.5
img = img.astype(np.uint8)
img = cv2.resize(img, (1280,720), interpolation=cv2.INTER_LINEAR)
if not is_demo:
if not is_gt:
if not is_ll:
cv2.imwrite(save_dir+"/batch_{}_{}_da_segresult.png".format(epoch,index), img)
else:
cv2.imwrite(save_dir+"/batch_{}_{}_ll_segresult.png".format(epoch,index), img)
else:
if not is_ll:
cv2.imwrite(save_dir+"/batch_{}_{}_da_seg_gt.png".format(epoch,index), img)
else:
cv2.imwrite(save_dir+"/batch_{}_{}_ll_seg_gt.png".format(epoch,index), img)
return img
def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
# Rescale coords (xyxy) from img1_shape to img0_shape
if ratio_pad is None: # calculate from img0_shape
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
else:
gain = ratio_pad[0][0]
pad = ratio_pad[1]
coords[:, [0, 2]] -= pad[0] # x padding
coords[:, [1, 3]] -= pad[1] # y padding
coords[:, :4] /= gain
clip_coords(coords, img0_shape)
return coords
def clip_coords(boxes, img_shape):
# Clip bounding xyxy bounding boxes to image shape (height, width)
boxes[:, 0].clamp_(0, img_shape[1]) # x1
boxes[:, 1].clamp_(0, img_shape[0]) # y1
boxes[:, 2].clamp_(0, img_shape[1]) # x2
boxes[:, 3].clamp_(0, img_shape[0]) # y2
#### VIMP FUNCTIONS below this
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
# Plots one bounding box on image img
tl = line_thickness or round(0.0001 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
if(abs(x[0]-x[2]) * abs(x[1]-x[3]) > 120000):
cv2.rectangle(img, c1, c2, [0,0,255], thickness=tl, lineType=cv2.LINE_AA)
return 1
else:
cv2.rectangle(img, c1, c2, [0,255,0], thickness=tl, lineType=cv2.LINE_AA)
return 0
def get_center_line(lines):
left_lines = [] # Like /
right_lines = [] # Like \
# calculate weighted average slope of lines of +- slope separately.
total_dist_lt = 0
total_dist_rt = 0
for line in lines:
for x1,y1,x2,y2 in line:
cur_dist = round(((x1-x2)**2 + (y1-y2)**2)**0.5, 2)
if x1 == x2:
pass #Vertical Lines
else:
m = (y2 - y1) / (x2 - x1)
m = m*cur_dist
if m < 0:
total_dist_lt += cur_dist
left_lines.append(m)
elif m >= 0:
total_dist_rt += cur_dist
right_lines.append(m)
# print(left_lines, right_lines)
# left_line_slope
if len(left_lines) == 0 and len(right_lines) == 0:
return (600,700),(600,400), 1000
elif len(left_lines) == 0:
slope = sum(right_lines)/total_dist_rt
c = 700 - slope*600
x2 = int((400 - c)/slope)
return (600,700),(x2,400), slope
elif len(right_lines) == 0:
slope = sum(left_lines)/total_dist_lt
c = 700 - slope*600
x2 = int((400 - c)/slope)
return (600,700),(x2,400), slope
"""
### slope of angle bisector of the lane lines is center line.
### doing some math will give the below formula. we get 2 angle bisectors ofc and
We choose the max of the 2 slopes since the one close to straight line
i.e. slope = inf is probably correct. In other words no sharp turns.
"""
m1 = sum(left_lines)/total_dist_lt
m2 = sum(right_lines)/total_dist_rt
slope_angle_bisector_1 = ((m1*m2 - 1) + np.sqrt((m1*m2-1)**2 + (m1+m2)**2))/(m1+m2)
slope_angle_bisector_2 = ((m1*m2 - 1) - np.sqrt((m1*m2-1)**2 + (m1+m2)**2))/(m1+m2)
#take the slope which is more strainght-er
if abs(slope_angle_bisector_1) > abs(slope_angle_bisector_2):
slope = slope_angle_bisector_1
else:
slope = slope_angle_bisector_2
#if slope is almost straight make it straight completely
# if -10<slope<10:
# slope = 1000
c = 700 - slope*600
x2 = int((400 - c)/slope)
return (600,700),(x2,400), slope
def calculate_steering_angle(slope):
temp = round(np.arctan(slope), 4)
# sign_ = 1
# if temp<0:
# sign_ = -1
# temp = 90-abs(temp)
return temp
def get_lane_lines(image, opt):
image = np.float32(cv2.merge((image,image,image)))
gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
# Define a kernel size and apply Gaussian smoothing
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
# Define our parameters for Canny and apply
low_threshold = 90
high_threshold = 360
edges = cv2.Canny(np.uint8(blur_gray), low_threshold, high_threshold)
# Next we'll create a masked edges image using cv2.fillPoly()
mask = np.zeros_like(edges)
ignore_mask_color = 255
# This time we are defining a four sided polygon to mask
imshape = image.shape
vertices = np.array([[(opt.lane_boundary_bottom_offset,imshape[0]),(opt.lane_boundary_top_offset, opt.lane_boundary_top), (imshape[1]-opt.lane_boundary_top_offset, opt.lane_boundary_top), (imshape[1]-opt.lane_boundary_bottom_offset,imshape[0])]], dtype=np.int32)
cv2.fillPoly(mask, vertices, ignore_mask_color)
masked_edges = cv2.bitwise_and(edges, mask)
# Define the Hough transform parameters
# Make a blank the same size as our image to draw on
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = opt.threshold # minimum number of votes (intersections in Hough grid cell)
min_line_length = opt.min_line_length #minimum number of pixels making up a line
max_line_gap = opt.max_line_gap # maximum gap in pixels between connectable line segments
line_image = np.copy(image)*0 # creating a blank to draw lines on
# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
if lines is None:
return None, 0 #NEED TO FIX THIS ANGLE AND SET THE CORRECT ONE (0 ig)
center_line_coor1, center_line_coor2, slope = get_center_line(lines)
#print("Slope of line is = ", slope)
#print("angle to rotate = ", np.arctan(slope)*180/3.14)
angle_to_rotate = calculate_steering_angle(slope)
cv2.line(line_image,center_line_coor1, center_line_coor2,(0,255,0),10)
# Iterate over the output "lines" and draw lines on a blank image
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
# Create a "color" binary image to combine with line image
color_edges = np.dstack((edges, edges, edges))
# Draw the lines on the edge image
lines_edges = cv2.addWeighted(color_edges.astype(np.uint8), 0.8, line_image.astype(np.uint8), 1, 0)
lines_edges = cv2.polylines(lines_edges,vertices, True, (0,0,255), 10)
# add text to image
lines_edges = cv2.putText(lines_edges, 'Steering angle: {0}'.format(angle_to_rotate),
(50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, opt.text_color, 2, cv2.LINE_AA)
return lines_edges, angle_to_rotate
#### for image and video files
def detect_static(opt):
# Load model
device = opt.device
model = get_net()
checkpoint = torch.load(opt.weights, map_location= device)
model.load_state_dict(checkpoint['state_dict'])
model = model.to(device)
# OPTIONAL: half precision to make faster processing
half = device.type != 'cpu' # half precision only supported on CUDA
if half and opt.implement_half_precision:
model.half() # to FP16
# Set Dataloader
if opt.source.isnumeric():
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(opt.source, img_size=opt.img_size)
bs = len(dataset) # batch_size
else:
dataset = LoadImages(opt.source, img_size=opt.img_size)
bs = 1 # batch_size
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors= [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
# Steps for half precision
if opt.implement_half_precision:
img = torch.zeros((1, 3, opt.img_size, opt.img_size), device=device) # init img
_ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
model.eval()
vid_path, vid_writer = None, None
for i, (path, img, img_det, vid_cap,shapes) in tqdm(enumerate(dataset),total = len(dataset)):
if isinstance(path, list):
save_path = opt.save_dir+'/'
else:
save_path = str(opt.save_dir +'/'+ Path(path).name)
img = transform(img).to(device)
if opt.implement_half_precision:
img = img.half() if half else img.float() # uint8 to fp16/32
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
det_out, da_seg_out,ll_seg_out = model(img)
inf_out, _ = det_out
# Apply NMS
det_pred = non_max_suppression(inf_out, conf_thres=opt.conf_thres, iou_thres=opt.iou_thres, classes=None, agnostic=False)
det=det_pred[0]
# Set basic variables
_, _, height, width = img.shape
h,w,_=img_det.shape
pad_w, pad_h = shapes[1][1]
pad_w = int(pad_w)
pad_h = int(pad_h)
ratio = shapes[1][0][1]
if ratio>1:
ratio = 1
#### Drivable area mask
da_predict = da_seg_out[:, :, pad_h:(height-pad_h),pad_w:(width-pad_w)]
da_seg_mask = torch.nn.functional.interpolate(da_predict, scale_factor=int(1/ratio), mode='bilinear')
_, da_seg_mask = torch.max(da_seg_mask, 1)
da_seg_mask = da_seg_mask.int().squeeze().cpu().numpy()
#### Lanes detected mask
ll_predict = ll_seg_out[:, :,pad_h:(height-pad_h),pad_w:(width-pad_w)]
ll_seg_mask = torch.nn.functional.interpolate(ll_predict, scale_factor=int(1/ratio), mode='bilinear')
_, ll_seg_mask = torch.max(ll_seg_mask, 1)
ll_seg_mask = ll_seg_mask.int().squeeze().cpu().numpy()
lanes_in_roi, angle_to_rotate = get_lane_lines((ll_seg_mask*255).astype(np.uint8), opt)
#### apply both masks
img_det = cv2.resize(img_det, (da_seg_mask.shape[1], da_seg_mask.shape[0]), interpolation = cv2.INTER_AREA)
img_det = show_seg_result(img_det, (da_seg_mask, ll_seg_mask), _, _, is_demo=True)
#### Object detection
obstacle_in_way = False
if len(det):
det[:,:4] = scale_coords(img.shape[2:],det[:,:4],img_det.shape).round()
for *xyxy,conf,cls in reversed(det):
label_det_pred = f'{names[int(cls)]} {conf:.2f}'
if obstacle_in_way == False:
obstacle_in_way = plot_one_box(xyxy, img_det , label=label_det_pred, color=colors[int(cls)], line_thickness=2)
else:
_ = plot_one_box(xyxy, img_det , label=label_det_pred, color=colors[int(cls)], line_thickness=2)
if obstacle_in_way:
img_det = cv2.putText(img_det, 'STOP (OBSTACLE in the WAY)',
(50,100), cv2.FONT_HERSHEY_SIMPLEX, 1, opt.text_color, 2, cv2.LINE_AA)
if lanes_in_roi is not None:
img_det = cv2.addWeighted(img_det, 0.8, lanes_in_roi, 1.0, 0.0)
if dataset.mode == 'images':
cv2.imwrite(save_path,img_det)
elif dataset.mode == 'video':
if vid_path != save_path: # new video
vid_path = save_path
if isinstance(vid_writer, cv2.VideoWriter):
vid_writer.release() # release previous video writer
fourcc = 'mp4v' # output video codec
fps = vid_cap.get(cv2.CAP_PROP_FPS)
print("The fps of video is:", fps)
h,w,_=img_det.shape
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (w, h))
vid_writer.write(img_det)
print('Results saved to %s' % Path(opt.save_dir))
return angle_to_rotate, obstacle_in_way
#### for streams
def detect_streams(opt):
# Load model
device = opt.device
model = get_net()
checkpoint = torch.load(opt.weights, map_location= device)
model.load_state_dict(checkpoint['state_dict'])
model = model.to(device)
# OPTIONAL: half precision to make faster processing
half = device.type != 'cpu' # half precision only supported on CUDA
if half and opt.implement_half_precision:
model.half() # to FP16
# Set Dataloader
if opt.source.isnumeric():
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(opt.source, img_size=opt.img_size)
bs = len(dataset) # batch_size
else:
dataset = LoadImages(opt.source, img_size=opt.img_size)
bs = 1 # batch_size
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors= [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))]
# Steps for half precision
if opt.implement_half_precision:
img = torch.zeros((1, 3, opt.img_size, opt.img_size), device=device) # init img
_ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
model.eval()
vid_path, vid_writer = None, None
for i, (path, img, img_det, vid_cap,shapes) in tqdm(enumerate(dataset),total = len(dataset)):
img = transform(img).to(device)
if opt.implement_half_precision:
img = img.half() if half else img.float() # uint8 to fp16/32
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
det_out, da_seg_out,ll_seg_out = model(img)
inf_out, _ = det_out
# Apply NMS
det_pred = non_max_suppression(inf_out, conf_thres=opt.conf_thres, iou_thres=opt.iou_thres, classes=None, agnostic=False)
det=det_pred[0]
# Set basic variables
_, _, height, width = img.shape
h,w,_=img_det.shape
pad_w, pad_h = shapes[1][1]
pad_w = int(pad_w)
pad_h = int(pad_h)
ratio = shapes[1][0][1]
if ratio>1:
ratio = 1
#### Drivable area mask
da_predict = da_seg_out[:, :, pad_h:(height-pad_h),pad_w:(width-pad_w)]
da_seg_mask = torch.nn.functional.interpolate(da_predict, scale_factor=int(1/ratio), mode='bilinear')
_, da_seg_mask = torch.max(da_seg_mask, 1)
da_seg_mask = da_seg_mask.int().squeeze().cpu().numpy()
#### Lanes detected mask
ll_predict = ll_seg_out[:, :,pad_h:(height-pad_h),pad_w:(width-pad_w)]
ll_seg_mask = torch.nn.functional.interpolate(ll_predict, scale_factor=int(1/ratio), mode='bilinear')
_, ll_seg_mask = torch.max(ll_seg_mask, 1)
ll_seg_mask = ll_seg_mask.int().squeeze().cpu().numpy()
lanes_in_roi, angle_to_rotate = get_lane_lines((ll_seg_mask*255).astype(np.uint8), opt)
#### Apply both masks
img_det = cv2.resize(img_det, (da_seg_mask.shape[1], da_seg_mask.shape[0]), interpolation = cv2.INTER_AREA)
img_det = show_seg_result(img_det, (da_seg_mask, ll_seg_mask), _, _, is_demo=True)
#### Object detection
obstacle_in_way = False
if len(det):
det[:,:4] = scale_coords(img.shape[2:],det[:,:4],img_det.shape).round()
for *xyxy,conf,cls in reversed(det):
label_det_pred = f'{names[int(cls)]} {conf:.2f}'
if obstacle_in_way == False:
obstacle_in_way = plot_one_box(xyxy, img_det , label=label_det_pred, color=colors[int(cls)], line_thickness=2)
else:
_ = plot_one_box(xyxy, img_det , label=label_det_pred, color=colors[int(cls)], line_thickness=2)
if obstacle_in_way:
img_det = cv2.putText(img_det, 'STOP (OBSTACLE in the WAY)',
(50,100), cv2.FONT_HERSHEY_SIMPLEX, 1, opt.text_color, 2, cv2.LINE_AA)
if lanes_in_roi is not None:
img_det = cv2.addWeighted(img_det, 0.8, lanes_in_roi, 1.0, 0.0)
# dataset.mode == 'stream'
yield img_det, angle_to_rotate, obstacle_in_way