-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.py
165 lines (113 loc) · 4.2 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
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
import struct
from struct import unpack
import numpy as np
import gizeh as gz
from os import path
def unpack_drawing(file_handle):
key_id, = unpack('Q', file_handle.read(8))
country_code, = unpack('2s', file_handle.read(2))
recognized, = unpack('b', file_handle.read(1))
timestamp, = unpack('I', file_handle.read(4))
n_strokes, = unpack('H', file_handle.read(2))
image = []
for i in range(n_strokes):
n_points, = unpack('H', file_handle.read(2))
fmt = str(n_points) + 'B'
x = unpack(fmt, file_handle.read(n_points))
y = unpack(fmt, file_handle.read(n_points))
image.append((x, y))
return {
'key_id': key_id,
'country_code': country_code,
'recognized': recognized,
'timestamp': timestamp,
'image': image
}
def unpack_drawings(filename):
with open(filename, 'rb') as f:
while True:
try:
yield unpack_drawing(f)
except struct.error:
break
def get_objects(boxes, classes):
objs = []
objs_p = []
LABELS =open("yolo/coco.names").read().strip().split("\n")
boxes2 = []
boxes_p = []
i=-1
for y_class in classes:
i+=1
if(path.exists('dataset/'+str(LABELS[y_class])+'.bin')):
arr = unpack_drawings('dataset/'+str(LABELS[y_class])+'.bin')
boxes2.append(boxes[i])
num = np.random.randint(0,50)
for drawing in arr:
objs.append(drawing)
break
elif(LABELS[y_class] == 'person'):
face = unpack_drawings('dataset/smiley face.bin')
tshirt = unpack_drawings('dataset/t-shirt.bin')
pants = unpack_drawings('dataset/pants.bin')
arr_p = [face, tshirt, pants]
obj = []
for k in arr_p:
m=0
for drawing in k:
if(m==9):
obj.append(drawing)
break
m+=1
objs_p.append(obj)
boxes_p.append(boxes[i])
else:
continue
return boxes2, objs, boxes_p, objs_p
def drawing(boxes, objs, colors, classes):
surface = gz.Surface(width=720, height=560) # in pixels
rect = gz.rectangle(lx=720, ly=560, xy=(360,280), fill=(1,1,1))
rect.draw(surface)
LABELS =open("yolo/coco.names").read().strip().split("\n")
colors = [colors[i] for i in range(len(colors)) if LABELS[classes[i]] != 'person']
#print(len(colors))
i=0
for strokes in objs:
lines_list = []
for stroke in strokes['image']:
x, y = stroke
x = tuple([(z*boxes[i][2])//255 for z in x])
y = tuple([(z*boxes[i][3])//255 for z in y])
x = tuple([z+boxes[i][0] for z in x])
y = tuple([z+boxes[i][1] for z in y])
points = list(zip(x, y))
line = gz.polyline(points=points, stroke=[0,0,0], stroke_width=2, fill=colors[i])
lines_list.append(line)
lines = gz.Group(lines_list)
lines.draw(surface)
i+=1
#surface.write_to_png("gallery/circle.png")
return surface
def draw_person(surface, boxes, objs, colors, classes):
LABELS =open("yolo/coco.names").read().strip().split("\n")
colors = [colors[i] for i in range(len(colors)) if LABELS[classes[i]] == 'person']
i=0
for obj in objs:
k = 0
for strokes in obj:
lines_list = []
for stroke in strokes['image']:
x, y = stroke
x = tuple([(z*boxes[i][2])//255 for z in x])
y = tuple([(z*boxes[i][3])//255 for z in y])
x = tuple([z+boxes[i][0] for z in x])
y = tuple([z+boxes[i][1]+((k*boxes[i][3])//255) for z in y])
points = list(zip(x, y))
#print(colors[i])
line = gz.polyline(points=points, stroke=[0,0,0], stroke_width=2, fill=colors[i])
lines_list.append(line)
lines = gz.Group(lines_list)
lines.draw(surface)
k+=200
i+=1
surface.write_to_png("gallery/circle.png")