-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathutils.py
83 lines (64 loc) · 2.16 KB
/
utils.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
import cv2
import numpy as np
def show(img, name="disp", width=1000):
"""
name: name of window, should be name of img
img: source of img, should in type ndarray
"""
cv2.namedWindow(name, cv2.WINDOW_GUI_EXPANDED)
cv2.resizeWindow(name, width, 1000)
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def overlay_mask(image, mask, alpha=0.5):
c = (np.random.random((1, 3)) * 153 + 102).tolist()[0]
mask = np.dstack([mask.astype(np.uint8)] * 3)
mask = cv2.threshold(mask, 127.5, 255, cv2.THRESH_BINARY)[1]
inv_mask = 255 - mask
overlay = image.copy()
overlay = np.minimum(overlay, inv_mask)
color_mask = (mask.astype(np.bool) * c).astype(np.uint8)
overlay = np.maximum(overlay, color_mask).astype(np.uint8)
image = cv2.addWeighted(image, alpha, overlay, 1 - alpha, 0)
return image
def overlay_ann(image, mask, box, label, score, alpha=0.5):
c = np.random.random((1, 3))
mask_color = (c * 153 + 102).tolist()[0]
text_color = (c * 183 + 72).tolist()[0]
mask = np.dstack([mask.astype(np.uint8)] * 3)
mask = cv2.threshold(mask, 127.5, 255, cv2.THRESH_BINARY)[1]
inv_mask = 255 - mask
overlay = image.copy()
overlay = np.minimum(overlay, inv_mask)
color_mask = (mask.astype(np.bool) * mask_color).astype(np.uint8)
overlay = np.maximum(overlay, color_mask).astype(np.uint8)
image = cv2.addWeighted(image, alpha, overlay, 1 - alpha, 0)
# draw on color mask
cv2.rectangle(
image,
(box[0], box[1]),
(box[2], box[3]),
mask_color, 1
)
(label_size_width, label_size_height), base_line = \
cv2.getTextSize(
"{}".format(label),
cv2.FONT_HERSHEY_SIMPLEX,
0.3, 1
)
cv2.rectangle(
image,
(box[0], box[1] + 10),
(box[0] + label_size_width, box[1] + 10 - label_size_height),
(223, 128, 255),
cv2.FILLED
)
cv2.putText(
image,
# "{}: {:.3f}".format(label, score),
"{}".format(label),
(box[0], box[1] + 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.3, (0, 0, 0), 1
)
return image