-
Notifications
You must be signed in to change notification settings - Fork 30
/
draw.py
executable file
·107 lines (89 loc) · 2.71 KB
/
draw.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
#!/usr/bin/env python
import matplotlib.pyplot as plt
import imgviz
def draw():
img = imgviz.data.lena()
H, W = img.shape[:2]
viz = img
y1, x1 = 200, 180
y2, x2 = 400, 380
viz = imgviz.draw.rectangle(
viz, (y1, x1), (y2, x2), outline=(255, 255, 255), width=5
)
viz = imgviz.draw.text_in_rectangle(
viz,
loc="lt",
text="face",
size=30,
background=(255, 255, 255),
aabb1=(y1, x1),
aabb2=(y2, x2),
)
# eye, eye, nose, mouse, mouse
yxs = [(265, 265), (265, 330), (320, 315), (350, 270), (350, 320)]
# eye segment
viz = imgviz.draw.line(viz, yx=[yxs[0], yxs[1]], fill=(255, 255, 255), width=5)
# mouse segment
viz = imgviz.draw.line(viz, yx=[yxs[3], yxs[4]], fill=(255, 255, 255), width=5)
colors = imgviz.label_colormap(value=255)[1:]
shapes = ["star", "ellipse", "rectangle", "circle", "triangle"]
for yx, color, shape in zip(yxs, colors, shapes):
size = 20
if shape == "star":
viz = imgviz.draw.star(
viz, center=(yx[0], yx[1]), size=1.2 * size, fill=color
)
elif shape == "ellipse":
viz = imgviz.draw.ellipse(
viz,
yx1=(yx[0] - 8, yx[1] - 16),
yx2=(yx[0] + 8, yx[1] + 16),
fill=color,
)
elif shape == "circle":
viz = imgviz.draw.circle(
viz, center=(yx[0], yx[1]), diameter=size, fill=color
)
elif shape == "triangle":
viz = imgviz.draw.triangle(
viz, center=(yx[0], yx[1]), size=size, fill=color
)
elif shape == "rectangle":
viz = imgviz.draw.rectangle(
viz,
aabb1=(yx[0] - size / 2, yx[1] - size / 2),
aabb2=(yx[0] + size / 2, yx[1] + size / 2),
fill=color,
)
else:
raise ValueError("unsupport shape: {}".format(shape))
img = imgviz.draw.text_in_rectangle(
img,
loc="lt+",
text="original",
size=30,
background=(255, 255, 255),
)
viz = imgviz.draw.text_in_rectangle(
viz,
loc="lt+",
text="markers",
size=30,
background=(255, 255, 255),
)
# -------------------------------------------------------------------------
plt.figure(dpi=200)
plt.subplot(121)
plt.title("original")
plt.imshow(img)
plt.axis("off")
plt.subplot(122)
plt.title("markers")
plt.imshow(viz)
plt.axis("off")
img = imgviz.io.pyplot_to_numpy()
plt.close()
return img
if __name__ == "__main__":
from base import run_example
run_example(draw)