erlinersi commited on
Commit
a34d487
·
1 Parent(s): 65c38f5

Delete utils/plots.py

Browse files
Files changed (1) hide show
  1. utils/plots.py +0 -89
utils/plots.py DELETED
@@ -1,89 +0,0 @@
1
- import random
2
-
3
- import cv2
4
- import matplotlib
5
- import numpy as np
6
- import torch
7
-
8
-
9
- def show_seg_result(img, result, index=0, epoch=0, batch=0, save_dir=None, is_ll=False, palette=None, is_demo=False,
10
- is_gt=False):
11
- if palette is None:
12
- palette = np.random.randint(0, 255, size=(3, 3))
13
- palette[0] = [0, 0, 0]
14
- palette[1] = [0, 255, 0]
15
- palette[2] = [255, 0, 0]
16
- palette = np.array(palette)
17
- assert palette.shape[0] == 3 # len(classes)
18
- assert palette.shape[1] == 3
19
- assert len(palette.shape) == 2
20
-
21
- if not is_demo:
22
- color_seg = np.zeros((result.shape[0], result.shape[1], 3), dtype=np.uint8)
23
- for label, color in enumerate(palette):
24
- color_seg[result == label, :] = color
25
- else:
26
- color_area = np.zeros((result[0].shape[0], result[0].shape[1], 3), dtype=np.uint8)
27
-
28
- color_area[result[0] == 1] = [0, 255, 0]
29
- color_area[result[1] == 1] = [255, 0, 0]
30
- color_seg = color_area
31
-
32
- # convert to BGR
33
- color_seg = color_seg[..., ::-1]
34
- # print(color_seg.shape)
35
- color_mask = np.mean(color_seg, 2)
36
- img[color_mask != 0] = img[color_mask != 0] * 0.5 + color_seg[color_mask != 0] * 0.5
37
- # img = img * 0.5 + color_seg * 0.5
38
- img = img.astype(np.uint8)
39
- img = cv2.resize(img, (1280, 720), interpolation=cv2.INTER_LINEAR)
40
-
41
- if not is_demo:
42
- if not is_gt:
43
- if not is_ll:
44
- cv2.imwrite(save_dir + "/batch_{}_{}_{}_da_segresult.png".format(epoch, batch, index), img)
45
- else:
46
- cv2.imwrite(save_dir + "/batch_{}_{}_{}_ll_segresult.png".format(epoch, batch, index), img)
47
- else:
48
- if not is_ll:
49
- cv2.imwrite(save_dir + "/batch_{}_{}_{}_da_seg_gt.png".format(epoch, batch, index), img)
50
- else:
51
- cv2.imwrite(save_dir + "/batch_{}_{}_{}_ll_seg_gt.png".format(epoch, batch, index), img)
52
- return img
53
-
54
-
55
- def plot_one_box(x, img, color=None, label=None, line_thickness=3):
56
- # Plots one bounding box on image img
57
- tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
58
- color = color if color else [random.randint(0, 255) for _ in range(3)]
59
- c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
60
- cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
61
- if label:
62
- tf = max(tl - 1, 1) # font thickness
63
- t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
64
- c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
65
- cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
66
- cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [0, 0, 0], thickness=tf, lineType=cv2.LINE_AA)
67
-
68
-
69
- def driving_area_mask(da_seg_out, width, height, pad_w, pad_h, ratio):
70
- da_predict = da_seg_out[:, :, pad_h:(height - pad_h), pad_w:(width - pad_w)]
71
- da_seg_mask = torch.nn.functional.interpolate(da_predict, scale_factor=int(1 / ratio), mode='bilinear')
72
- _, da_seg_mask = torch.max(da_seg_mask, 1)
73
- da_seg_mask = da_seg_mask.int().squeeze().cpu().numpy()
74
- return da_seg_mask
75
-
76
-
77
- def lane_line_mask(ll_seg_out, width, height, pad_w, pad_h, ratio):
78
- ll_predict = ll_seg_out[:, :, pad_h:(height - pad_h), pad_w:(width - pad_w)]
79
- ll_seg_mask = torch.nn.functional.interpolate(ll_predict, scale_factor=int(1 / ratio), mode='bilinear')
80
- _, ll_seg_mask = torch.max(ll_seg_mask, 1)
81
- ll_seg_mask = ll_seg_mask.int().squeeze().cpu().numpy()
82
- return ll_seg_mask
83
-
84
- def color_list():
85
- # Return first 10 plt colors as (r,g,b) https://stackoverflow.com/questions/51350872/python-from-color-name-to-rgb
86
- def hex2rgb(h):
87
- return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4))
88
-
89
- return [hex2rgb(h) for h in matplotlib.colors.TABLEAU_COLORS.values()] # or BASE_ (8), CSS4_ (148), XKCD_ (949)