-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.py
161 lines (134 loc) · 4.27 KB
/
mask.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
import numpy as np
from image import Image
class Mask():
def __init__(self, mask_data):
self.mask_data = mask_data
@property
def data(self):
return self.mask_data
@property
def shape(self):
return self.mask_data.shape
@property
def fill_ratio(self):
return np.count_nonzero(self.mask_data) / (self.mask_data.shape[0] * self.mask_data.shape[1])
def to_array(self, channels=3):
arr = np.zeros((
self.mask_data.shape[0],
self.mask_data.shape[1],
channels
))
arr[self.mask_data] = 1
return arr
def to_image(self, channels=3, color=[1,1,1]):
image_data = self.to_array(channels)
image_data_orig = image_data.copy()
if color is None:
color = np.random.random((1, 3)).tolist()[0]
for i in range(3):
image_data[:,:,i] = color[i]
image_data *= image_data_orig
return Image.from_data(image_data)
def show(self, figsize=(4,4), title=None):
if title is None:
title = "Mask"
Image.plot(self.mask_data, figsize, title)
def copy(self):
return Mask(self.mask_data.copy())
def invert(self):
return Mask(np.logical_not(self.mask_data))
def plot_overlay(self):
return self.to_image(4).plot()
def apply(self, array):
mask_temp = self.mask_data.copy()
if len(array.shape) == len(self.mask_data.shape) + 1:
mask_temp = np.expand_dims(mask_temp, -1)
return array * mask_temp
def get_writeable_data(self):
mask_data = self.mask_data.copy()
if np.max(mask_data) <= 1:
mask_data = np.clip(mask_data*255, 0, 255)
return mask_data.astype(np.uint8)
def __iadd__(self, other):
if isinstance(other, Mask):
self.mask_data += other.mask_data
elif isinstance(other, (np.ndarray)):
self.mask_data += other
elif isinstance(other, (float, int)):
self.mask_data += other
else:
return NotImplemented
return self
def __isub__(self, other):
if isinstance(other, Mask):
self.mask_data -= other.mask_data
elif isinstance(other, (np.ndarray)):
self.mask_data -= other
elif isinstance(other, (float, int)):
self.mask_data -= other
else:
return NotImplemented
return self
def __imul__(self, other):
if isinstance(other, Mask):
self.mask_data *= other.mask_data
elif isinstance(other, (np.ndarray)):
self.mask_data *= other
elif isinstance(other, (float, int)):
self.mask_data *= other
else:
return NotImplemented
return self
def __itruediv__(self, other):
if isinstance(other, Mask):
self.mask_data /= other.mask_data
elif isinstance(other, (np.ndarray)):
self.mask_data /= other
elif isinstance(other, (float, int)):
self.mask_data /= other
else:
return NotImplemented
return self
def __ipow__(self, other):
if isinstance(other, Mask):
self.mask_data **= other.mask_data
elif isinstance(other, (np.ndarray)):
self.mask_data **= other
elif isinstance(other, (float, int)):
self.mask_data **= other
else:
return NotImplemented
return self
def __eq__(self, other):
if isinstance(other, Mask):
return np.array_equal(self.mask_data, other.mask_data)
else:
return NotImplemented
class AnnotationMask(Mask):
def __init__(self, data):
super().__init__(data['segmentation'])
# self.segmentation = data['segmentation']
self.area = data['area']
self.bbox = data['bbox']
self.predicted_iou = data['predicted_iou']
self.point_coords = data['point_coords']
self.stability_score = data['stability_score']
self.crop_box = data['crop_box']
self.score = {
"predicted_iou" : self.predicted_iou,
"stability" : self.stability_score
}
def get_score(self):
return f"Predicted IOU: {round(self.predicted_iou, 4)} | Stability: {round(self.stability_score, 4)}"
def to_annotation(self):
return {
"segmentation" : self.mask_data,
"area" : self.area,
"bbox" : self.bbox,
"predicted_iou": self.predicted_iou,
"point_coords" : self.point_coords,
"stability_score": self.stability_score,
"crop_box" : self.crop_box
}
def __repr__(self) -> str:
return f'Annotation Mask | area: {self.area}'