-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
190 lines (152 loc) · 5.14 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
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
import matplotlib.pyplot as plt
import matplotlib.patheffects as pe
import numpy as np
from skimage import measure
import random
from PIL import Image
import json
def preprocess_outputs(output):
input_scores = [x["score"] for x in output]
input_labels = [x["label"] for x in output]
input_boxes = []
for i in range(len(output)):
input_boxes.append([*output[i]["box"].values()])
input_boxes = [input_boxes]
return input_scores, input_labels, input_boxes
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_box(box, ax):
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(
plt.Rectangle((x0, y0), w, h, edgecolor="green", facecolor=(0, 0, 0, 0), lw=2)
)
def show_boxes_on_image(raw_image, boxes):
plt.figure(figsize=(10, 10))
plt.imshow(raw_image)
for box in boxes:
show_box(box, plt.gca())
plt.axis("on")
plt.show()
def show_boxes_and_labels_on_image(raw_image, boxes, labels, scores):
plt.figure(figsize=(10, 10))
plt.imshow(raw_image)
for i, box in enumerate(boxes):
show_box(box, plt.gca())
plt.text(
x=box[0],
y=box[1] - 12,
s=f"{labels[i]}: {scores[i]:,.4f}",
c="beige",
path_effects=[pe.withStroke(linewidth=4, foreground="darkgreen")],
)
plt.axis("on")
plt.show()
def show_masks_on_image(raw_image, masks):
# Create a mask image (assuming binary mask)
image_with_mask = raw_image.convert("RGBA")
for mask in masks:
mask = mask.cpu().numpy()
width, height = image_with_mask.size
mask_array = np.zeros((height, width, 4), dtype=np.uint8)
color = [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 150]
mask_array[mask, :] = color
mask_image = Image.fromarray(mask_array)
# Overlay the mask on the image
image_with_mask = Image.alpha_composite(
image_with_mask,
mask_image)
# Display the result
return image_with_mask
def show_multiple_masks_on_image(raw_image, masks, scores):
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(np.array(raw_image))
for idx in range(len(masks[0])):
mask = masks[0][idx][0].cpu().detach()
show_mask(mask, ax, random_color=True)
ax.axis("off")
plt.show()
def show_binary_mask(masks, scores):
if len(masks.shape) == 4:
masks = masks.squeeze()
if scores.shape[0] == 1:
scores = scores.squeeze()
fig, ax = plt.subplots(figsize=(10, 10))
idx = scores.tolist().index(max(scores))
mask = masks[idx].cpu().detach()
ax.imshow(np.array(masks[0, :, :]), cmap="gray")
score = scores[idx]
ax.title.set_text(f"Score: {score.item():.3f}")
ax.axis("off")
plt.show()
def make_sam_mask(boolean_mask):
contours = measure.find_contours(boolean_mask, 0.5)
mask_points = []
for contour in contours:
contour = np.flip(contour, axis=1)
segmentation = contour.ravel().tolist()
mask_points.append(segmentation)
return mask_points
def make_coco_boxes(detections_boxes):
"""Convert torch tensor Pascal VOC bboxes to COCO format for Comet annotations"""
list_boxes = detections_boxes
coco_boxes = [
[
list_boxes[0],
list_boxes[1],
(list_boxes[2] - list_boxes[0]),
(list_boxes[3] - list_boxes[1]),
]
]
return coco_boxes
def make_bbox_annots(input_scores, input_labels, input_boxes, image_metadata):
if len(input_boxes[0]) == 0:
return None
annotations = [
{
"name": "bbox annots",
"data": [],
"metadata": image_metadata,
}
]
for i in range(len(input_boxes[0])):
annotations[0]["data"].append(
{
"label": input_labels[i],
"score": round((input_scores[i] * 100), 2),
# bboxes in pascal_voc format, return in coco format for Comet annotations
"boxes": make_coco_boxes(input_boxes[0][i]),
"points": None,
}
)
annotations = json.loads(json.dumps(annotations))
return annotations
def make_mask_annots(input_masks,
input_labels,
image_metadata
):
if len(input_masks[0]) == 0:
return None
annotations = [
{
"name": "mask annots",
"data": [],
"meta_data": image_metadata,
}
]
for i in range(len(input_masks)):
annotations[0]["data"].append(
{
"label": input_labels[i],
"score": 100.00,
"points": make_sam_mask(input_masks[i]),
}
)
annotations = json.loads(json.dumps(annotations))
return annotations