-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_visualization.py
312 lines (251 loc) · 10.9 KB
/
data_visualization.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from PIL import Image, ImageDraw, ImageFont
import math
from utils import parse_point, parse_box
def draw_rectangle(draw, box_coords, width=2, outline_color=(0, 255, 0), is_fill=False, bg_color=(0, 255, 0), transparency=50):
if is_fill:
# Calculate the alpha value based on the transparency percentage
alpha = int((1 - transparency / 100) * 255)
# Set the fill color with the specified background color and transparency
fill_color = tuple(bg_color) + (alpha,)
draw.rectangle(box_coords, width=width, outline=outline_color, fill=fill_color)
else:
draw.rectangle(box_coords, width=width, outline=outline_color)
def draw_circle(draw, center, radius=10, width=2, outline_color=(0, 255, 0), is_fill=False, bg_color=(0, 255, 0), transparency=80):
# Calculate the bounding box coordinates for the circle
x1 = center[0] - radius
y1 = center[1] - radius
x2 = center[0] + radius
y2 = center[1] + radius
bbox = (x1, y1, x2, y2)
# Draw the circle
if is_fill:
# Calculate the alpha value based on the transparency percentage
alpha = int((1 - transparency / 100) * 255)
# Set the fill color with the specified background color and transparency
fill_color = tuple(bg_color) + (alpha,)
draw.ellipse(bbox, width=width, outline=outline_color, fill=fill_color)
else:
draw.ellipse(bbox, width=width, outline=outline_color)
def draw_text_with_bg_box(draw, text, view_port, position,
font_size=24, font_color=(0, 0, 0),
bg_padding=10, bg_color=(179, 238, 58)):
# Define the font and size for the text
try:
font = ImageFont.truetype("./NotoSerifSC-SemiBold.otf", font_size)
except:
font = ImageFont.truetype("../NotoSerifSC-SemiBold.otf", font_size)
# Calculate the bounding box of the text
text_bbox = draw.textbbox((0, 0), text, font=font)
# Extract the width and height from the bounding box
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Define the position of the text based on the specified position parameter
image_width, image_height = view_port
if position == "top-left":
text_x = 5
text_y = 5
elif position == "bottom-middle":
text_x = (image_width - text_width) // 2
text_y = image_height - text_height - 5
elif position == "top-middle":
text_x = (image_width - text_width) // 2
text_y = 5
elif position.startswith("point"):
text_x, text_y = position.split("-")[1:]
text_x, text_y = int(text_x), int(text_y)
else:
print("unsupported position")
# Draw the background box
draw_rectangle(
draw,
[(text_x, text_y), (text_x + text_width + bg_padding, text_y + text_height + bg_padding)],
outline_color=(154, 205, 50),
is_fill=True,
bg_color=bg_color
)
# Draw the text on top of the background box
draw.text((text_x + 2, text_y + 2), text, font=font, fill=font_color)
def draw_index_with_bg_box(draw, text, position,
font_size=18, font_color=(255, 255, 255),
bg_padding=10, bg_color=(66, 119, 56)):
# Define the font and size for the text
try:
font = ImageFont.truetype("./NotoSerifSC-SemiBold.otf", font_size)
except:
font = ImageFont.truetype("../NotoSerifSC-SemiBold.otf", font_size)
# Calculate the bounding box of the text
text_bbox = draw.textbbox((0, 0), text, font=font)
# Extract the width and height from the bounding box
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Define the position of the text based on the specified position parameter
text_x, text_y = position
# Draw the background box
draw_rectangle(
draw,
[(text_x, text_y), (text_x + text_width + bg_padding, text_y + text_height + bg_padding)],
outline_color=bg_color,
is_fill=True,
bg_color=bg_color
)
# Draw the text on top of the background box
draw.text((text_x + 2, text_y), text, font=font, fill=font_color)
def draw_point(draw, center, radius1=3, radius2=6, color=(0, 255, 0)):
draw_circle(draw, center, radius=radius1, outline_color=color)
draw_circle(draw, center, radius=radius2, outline_color=color)
def draw_line_with_arrow(draw, start_point, end_point, color=(0, 255, 0), width=3, arrow_size=10):
# Draw the line
x1, y1 = start_point
x2, y2 = end_point
draw.line([(x1, y1), (x2, y2)], fill=color, width=width)
# Compute the angle between the line and the x-axis
angle = math.atan2(y2 - y1, x2 - x1)
# Calculate coordinates for arrowhead
x_arrow = x2 - arrow_size * math.cos(angle + math.pi/6)
y_arrow = y2 - arrow_size * math.sin(angle + math.pi/6)
x_arrow2 = x2 - arrow_size * math.cos(angle - math.pi/6)
y_arrow2 = y2 - arrow_size * math.sin(angle - math.pi/6)
# Draw arrowhead
draw.polygon([(x2, y2), (x_arrow, y_arrow), (x_arrow2, y_arrow2)], fill=color)
def element_visual(element: str, pil_img: Image, text: str):
draw = ImageDraw.Draw(pil_img)
image_h = pil_img.height
image_w = pil_img.width
if not isinstance(element["absolute"], list):
elements = [element["absolute"]]
else:
elements = element["absolute"]
for e in elements:
box_coords = parse_box(e)
draw_rectangle(draw, box_coords, outline_color='red')
draw_text_with_bg_box(
draw,
text=text,
view_port=(image_w, image_h),
position = "top-middle",
font_size=16)
return pil_img
def elements_visual(elements: list, pil_img: Image, outline_color="red"):
draw = ImageDraw.Draw(pil_img)
for element in elements:
# draw rectangle
if "rect" in element:
rect = element['rect']
left = rect['left']
top = rect['top']
right = rect['right']
bottom = rect['bottom']
else:
rect = element['position']
left = rect['x']
top = rect['y']
right = rect['x'] + rect["width"]
bottom = rect['y'] + rect["height"]
box_coords = (left, top, right, bottom)
draw_rectangle(draw, box_coords, outline_color=outline_color)
# draw index
draw_index_with_bg_box(draw,
text=str(element["uid"] if "uid" in element else element["id"]),
position=((left + right - 16) // 2, (top + bottom - 16) // 2),
font_size=14,
font_color=(255, 255, 255),
bg_padding=6,
bg_color=(66, 119, 56))
return pil_img
def actions_visual(action_group: list, pil_img: Image, ins_cmd: str, color=(255, 48, 48), from_eval=False):
draw = ImageDraw.Draw(pil_img)
image_h = pil_img.height
image_w = pil_img.width
if isinstance(action_group, dict):
action_group = [action_group]
name_group = ""
text_group = ""
for i, action in enumerate(action_group):
name_group += "{}. {}\n".format(i+1, action["name"])
# Draw element with index
if "element" in action:
if from_eval:
box_coords = parse_box(action["element"])
else:
box_coords = parse_box(action["element"]["absolute"])
if color is not None:
draw_rectangle(draw, box_coords, outline_color=color)
else:
draw_rectangle(draw, box_coords)
draw_index_with_bg_box(draw, str(i+1), (box_coords[0], box_coords[1]-10), font_size=10, bg_padding=2)
# Draw point
if "point" in action:
if from_eval:
center = parse_point(action["point"])
else:
center = parse_point(action["point"]["absolute"])
if color is not None:
draw_point(draw, center, color=color)
else:
draw_point(draw, center)
# Draw dual_point with index
if "dual_point" in action:
if from_eval:
start_point, end_point = parse_point(action["dual_point"]['from']), parse_point(action["dual_point"]['to'])
else:
start_point, end_point = parse_point(action["dual_point"]["absolute"]['from']), parse_point(action["dual_point"]["absolute"]['to'])
if color is not None:
draw_line_with_arrow(draw, start_point, end_point, color=color)
else:
draw_line_with_arrow(draw, start_point, end_point)
draw_index_with_bg_box(draw, str(i+1), (start_point[0], start_point[1]-10), font_size=10, bg_padding=2)
# Draw scroll
if "scroll" in action:
start_point = (image_w // 2, image_h // 2)
if from_eval:
end_point = (image_w // 2 + action["scroll"]['right'], image_h // 2 + action["scroll"]['down'])
else:
end_point = (image_w // 2 + action["scroll"]["absolute"]['right'], image_h // 2 + action["scroll"]["absolute"]['down'])
if color is not None:
draw_point(draw, start_point, color=color)
draw_line_with_arrow(draw, start_point, end_point, color=color)
else:
draw_point(draw, start_point)
draw_line_with_arrow(draw, start_point, end_point)
# Draw select value
if "value" in action:
assert "element" in action
if from_eval:
box_coords = parse_box(action["element"])
else:
box_coords = parse_box(action["element"]["absolute"])
x, y = box_coords[0], box_coords[1] + 30
draw_text_with_bg_box(
draw,
text=action["value"],
view_port=(image_w, image_h),
position = f"point-{x}-{y}",
)
if "text" in action:
text_group += "{}. {}\n".format(i+1, action["text"].replace("\n", "\t"))
# Draw action_names
draw_text_with_bg_box(
draw,
text=name_group,
view_port=(image_w, image_h),
position = "top-left",
font_size=16
)
# Draw text
if text_group != "":
draw_text_with_bg_box(
draw,
text=text_group,
view_port=(image_w, image_h),
position = "bottom-middle",
font_size=16
)
# Draw instruction
draw_text_with_bg_box(
draw,
text=ins_cmd,
view_port=(image_w, image_h),
position = "top-middle",
font_size=16
)
return pil_img