-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_tkinter_classes.py
More file actions
298 lines (242 loc) · 8.17 KB
/
data_tkinter_classes.py
File metadata and controls
298 lines (242 loc) · 8.17 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
import json
import uuid
import numpy as np
from copy import deepcopy # dont know if this is the correct operation for this
class AnnotationsTkinter:
def __init__(self):
self.annotations_tkinter = {}
def __len__(self):
return len(self.annotations_tkinter)
def __getitem__(self, index):
idx = list(self.annotations_tkinter.keys())[
index
] # python 3.6+ dicts are ordered
return self.annotations_tkinter[idx], idx
def load_annotations(self, path):
with open(path, "r") as f:
annotations = json.load(f)
return self.__convert2tkinter_format(annotations)
def save_annotations(self, path):
annotations_json = self.__convert2json_format()
with open(path, "w") as f:
json.dump(annotations_json, f)
def add_annotation(
self,
unique_id,
canvas_id,
coord_norm,
shape,
):
if shape == "polygon":
self.annotations_tkinter[unique_id] = AnnotationTkinter(
coord_norm, canvas_id=canvas_id
)
elif shape == "ellipse" or shape == "circle":
self.annotations_tkinter[unique_id] = EllipseTkinter(
coord_norm, shape, canvas_id=canvas_id
)
elif shape == "rectangle":
self.annotations_tkinter[unique_id] = RectangleTkinter(
coord_norm, canvas_id=canvas_id
)
def edit_annotation(
self,
unique_id,
canvas_id,
coord_norm,
):
self.annotations_tkinter[unique_id].edit_annotation(coord_norm, canvas_id)
def delete_annotation(self, unique_id):
del self.annotations_tkinter[unique_id]
def get_coords_from_unique_id(self, unique_id):
return (
self.annotations_tkinter[unique_id].coords_norm,
self.annotations_tkinter[unique_id].shape,
)
def __convert2tkinter_format(self, annotations):
tkinter_annotations = []
copy_annotations = deepcopy(annotations)
for annotation in copy_annotations:
if "id" in annotation:
idx = annotation["id"]
else:
idx = str(uuid.uuid4())
if annotation["type"] == "ellipse" or annotation["type"] == "circle":
self.annotations_tkinter[idx] = self.__ellipse2tkinter(
annotation, annotation["type"]
)
elif annotation["type"] == "polygon":
self.annotations_tkinter[idx] = self.__polygon2tkinter(annotation)
elif annotation["type"] == "rectangle":
self.annotations_tkinter[idx] = self.__rectangle2tkinter(annotation)
else:
raise ValueError(f" Mode {annotation['type']} is not supported")
tkinter_annotations.append((self.annotations_tkinter[idx], idx))
return tkinter_annotations
@staticmethod
def __ellipse2tkinter(data, shape):
radius_x, radius_y = data["radiusX"], data["radiusY"]
center_x, center_y = (
data["center"]["x"],
data["center"]["y"],
)
if "area" in data:
area = data["area"]
else:
area = None
if "accuracy" in data:
accuracy = data["accuracy"]
else:
accuracy = None
coords = ((center_x, center_y), (center_x + radius_x, center_y + radius_y))
annotation = EllipseTkinter(
coords,
shape,
area=area,
accuracy=accuracy,
radius_x=radius_x,
radius_y=radius_y,
)
return annotation
@staticmethod
def __polygon2tkinter(data):
points = [(point["x"], point["y"]) for point in data["points"]]
if "area" in data:
area = data["area"]
else:
area = None
if "accuracy" in data:
accuracy = data["accuracy"]
else:
accuracy = None
annotation = AnnotationTkinter(
points,
area=area,
accuracy=accuracy,
)
return annotation
@staticmethod
def __rectangle2tkinter(data):
x_coord, y_coord = data["coords"]
width = data["width"]
height = data["height"]
if "area" in data:
area = data["area"]
else:
area = None
if "accuracy" in data:
accuracy = data["accuracy"]
else:
accuracy = None
coord = ((x_coord, y_coord), (x_coord + width, y_coord + height))
annotation = RectangleTkinter(
coord, area=area, accuracy=accuracy, width=width, height=height
)
return annotation
def __convert2json_format(self):
annotations_json = []
for unique_id in list(self.annotations_tkinter.keys()):
annotation, shape = self.get_coords_from_unique_id(unique_id)
if shape == "ellipse" or shape == "circle":
json_annotation = self.__ellipse2json(annotation, shape)
elif shape == "polygon":
json_annotation = self.__polygon2json(annotation)
elif shape == "rectangle":
json_annotation = self.__rectangle2json(annotation)
else:
raise ValueError(f"shape {shape} is not supported")
annotations_json.append(json_annotation)
return annotations_json
@staticmethod
def __ellipse2json(data, shape):
json_annotation = {}
json_annotation["type"] = shape
json_annotation["angleOfRotation"] = 0
coord1, coord2 = data
x0, y0 = coord1
x1, y1 = coord2
radius_x = int(np.floor(np.abs((x1 - x0))))
radius_y = int(np.floor(np.abs((y1 - y0))))
json_annotation["radiusX"] = radius_x
json_annotation["radiusY"] = radius_y
json_annotation["center"] = {}
json_annotation["center"]["x"] = x0
json_annotation["center"]["y"] = y0
return json_annotation
@staticmethod
def __polygon2json(data):
json_annotation = {}
json_annotation["type"] = "polygon"
points = []
for point in data:
json_point = {}
json_point["x"] = point[0]
json_point["y"] = point[1]
points.append(json_point)
json_annotation["points"] = points
return json_annotation
@staticmethod
def __rectangle2json(data):
json_annotation = {}
json_annotation["type"] = "rectangle"
coord1, coord2 = data
json_annotation["coords"] = coord1
json_annotation["width"] = coord2[0] - coord1[0]
json_annotation["height"] = coord2[1] - coord1[1]
return json_annotation
class AnnotationTkinter:
def __init__(
self,
coords_norm,
shape="polygon",
canvas_id=None,
area=None,
accuracy=None,
):
self.coords_norm = coords_norm
self.shape = shape
self.canvas_id = canvas_id
self.area = area
self.accuracy = accuracy
def edit_annotation(self, coord_norm, canvas_id):
self.coords_norm = coord_norm
self.canvas_id = canvas_id
class EllipseTkinter(AnnotationTkinter):
def __init__(
self,
coords_norm,
shape,
canvas_id=None,
area=None,
accuracy=None,
radius_x=None,
radius_y=None,
angle=None,
):
super().__init__(coords_norm, shape, canvas_id=None, area=None, accuracy=None)
self.radius_x = radius_x
self.radius_y = radius_y
self.angle = angle
def get_mean_radius(self):
return (self.radius_y + self.radius_x) / 2
def get_mean_diameter(self):
return (2 * self.radius_y + 2 * self.radius_x) / 2
class RectangleTkinter(AnnotationTkinter):
def __init__(
self,
coords_norm,
canvas_id=None,
area=None,
accuracy=None,
width=None,
height=None,
):
super().__init__(
coords_norm,
shape="rectangle",
canvas_id=None,
area=None,
accuracy=None,
)
self.width = width
self.height = height