-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·247 lines (198 loc) · 8.95 KB
/
run.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
#!/usr/bin/env python
from random import random
from planar import Vec2, BoundingBox
from speaker import Speaker
from scene import Scene
import serialize
from landmark import (
Landmark,
ObjectClass,
Color
)
from representation import (
PointRepresentation,
RectangleRepresentation,
)
import json
from pprint import pprint
import sys
import os
#from configurations import adapter
def randrange(lower,upper):
width = upper-lower
return random() * width + lower
def too_close(p1,p2):
return (abs(p1[0] - p2[0]) <= 0.035) and (abs(p1[1] - p2[1]) <= 0.045)
def load_scene(file, normalize=False):
jtoclass = {
u'Box': ObjectClass.BOX,
u'Cylinder': ObjectClass.CYLINDER,
u'Sphere': ObjectClass.SPHERE,
}
jtocolor = {
u'yellow': Color.YELLOW,
u'orange': Color.ORANGE,
u'red': Color.RED,
u'green': Color.GREEN,
u'purple': Color.PURPLE,
u'blue': Color.BLUE,
u'pink': Color.PINK,
}
json_data=open(file)
data = json.load(json_data)
json_data.close()
#pprint(data)
scene = Scene(3)
table_spec = data[u'table']
t_min = Vec2(table_spec[u'aabb'][u'min'][2], table_spec[u'aabb'][u'min'][0])
t_max = Vec2(table_spec[u'aabb'][u'max'][2], table_spec[u'aabb'][u'max'][0])
width = t_max.x - t_min.x
height = t_max.y - t_min.y
if normalize: norm_factor = width if width >= height else height
t_min = Vec2(t_min.x / norm_factor, t_min.y / norm_factor)
t_max = Vec2(t_max.x / norm_factor, t_max.y / norm_factor)
table = Landmark('table',
RectangleRepresentation(rect=BoundingBox([t_min, t_max])),
None,
ObjectClass.TABLE)
scene.add_landmark(table)
object_specs = data[u'objects']
print 'there are', len(object_specs), 'objects on the table'
for i,obj_spec in enumerate(object_specs):
o_min = Vec2(obj_spec[u'aabb'][u'min'][2], obj_spec[u'aabb'][u'min'][0])
o_max = Vec2(obj_spec[u'aabb'][u'max'][2], obj_spec[u'aabb'][u'max'][0])
width = o_max.x - o_min.x
height = o_max.y - o_min.y
o_min = Vec2(o_min.x / norm_factor, o_min.y / norm_factor)
o_max = Vec2(o_max.x / norm_factor, o_max.y / norm_factor)
obj = Landmark('object_%s' % obj_spec[u'name'],
RectangleRepresentation(rect=BoundingBox([o_min, o_max]), landmarks_to_get=[]),
None,
jtoclass[obj_spec[u'type']],
jtocolor[obj_spec[u'color-name']])
obj.representation.alt_representations = []
scene.add_landmark(obj)
camera_spec = data[u'cam']
speaker = Speaker(Vec2(camera_spec[u'loc'][2] / norm_factor, camera_spec[u'loc'][0] / norm_factor))
# speaker.visualize(scene, obj, Vec2(0,0), None, None, '')
return scene, speaker
def construct_training_scene(random=False):
speaker = Speaker(Vec2(0,0))
scene = Scene(3)
table_ll = (-0.4,0.4)
table_ur = (0.4,1.6)
if random:
x_range = (table_ll[0]+0.035, table_ur[0]-0.035)
y_range = (table_ll[1]+0.045, table_ur[1]-0.045)
centers = []
for _ in range(5):
condition = True
while condition:
new_point = (randrange(*x_range),randrange(*y_range))
condition = (sum( [too_close(new_point,p) for p in centers] ) > 0)
centers.append( new_point )
else:
centers = [(0.05, 0.9), (0.05, 0.7), (0, 0.55), (-0.3,0.7), (0.3,0.7)]
table = Landmark('table',
RectangleRepresentation(rect=BoundingBox([Vec2(*table_ll), Vec2(*table_ur)])),
None,
ObjectClass.TABLE)
obj1 = Landmark('green_cup',
RectangleRepresentation(rect=BoundingBox([Vec2(centers[0][0]-0.035,centers[0][1]-0.035),
Vec2(centers[0][0]+0.035,centers[0][1]+0.035)]), landmarks_to_get=[]),
None,
ObjectClass.CUP,
Color.GREEN)
obj2 = Landmark('blue_cup',
RectangleRepresentation(rect=BoundingBox([Vec2(centers[1][0]-0.035,centers[1][1]-0.035),
Vec2(centers[1][0]+0.035,centers[1][1]+0.035)]), landmarks_to_get=[]),
None,
ObjectClass.CUP,
Color.BLUE)
obj3 = Landmark('pink_cup',
RectangleRepresentation(rect=BoundingBox([Vec2(centers[2][0]-0.035,centers[2][1]-0.035),
Vec2(centers[2][0]+0.035,centers[2][1]+0.035)]), landmarks_to_get=[]),
None,
ObjectClass.CUP,
Color.PINK)
obj4 = Landmark('purple_prism',
RectangleRepresentation(rect=BoundingBox([Vec2(centers[3][0]-0.035,centers[3][1]-0.045),
Vec2(centers[3][0]+0.035,centers[3][1]+0.045)]), landmarks_to_get=[]),
None,
ObjectClass.PRISM,
Color.PURPLE)
obj5 = Landmark('orange_prism',
RectangleRepresentation(rect=BoundingBox([Vec2(centers[4][0]-0.035,centers[4][1]-0.045),
Vec2(centers[4][0]+0.035,centers[4][1]+0.045)]), landmarks_to_get=[]),
None,
ObjectClass.PRISM,
Color.ORANGE)
# t_rep = table.to_dict()
scene.add_landmark(table)
# scene.add_landmark(serialize.landmark_from_dict(t_rep))
for obj in (obj1, obj2, obj3, obj4, obj5):
# o_rep = obj.to_dict()
obj.representation.alt_representations = []
scene.add_landmark(obj)
# scene.add_landmark(serialize.landmark_from_dict(o_rep))
return scene, speaker
def read_scenes(dir, normalize=False):
infos = []
for root, dirs, files in os.walk(dir): # Walk directory tree
for name in files:
if '.json' in name:
infos.append( load_scene(os.path.join(root, name), normalize) )
return infos
if __name__ == '__main__':
print len(read_scenes(sys.argv[1], True))
#scene, speaker = load_scene(sys.argv[1])
exit(1)
# scene, speaker = construct_training_scene()
# lmks = [lmk for lmk in scene.landmarks.values() if not lmk.name == 'table']
# groups = adapter.adapt(lmks)
#
# for i,g in enumerate(groups):
# scene.add_landmark(Landmark('ol%d'%i, g, None, Landmark.LINE))
#perspectives = [ Vec2(5.5,4.5), Vec2(6.5,6.0)]
#speaker.talk_to_baby(scene, perspectives, how_many_each=10)
dozen = 12
couple = 1
table = scene.landmarks['table'].representation.rect
t_min = table.min_point
t_max = table.max_point
t_w = table.width
t_h = table.height
for i in range(couple * dozen):
location = Landmark( 'point', PointRepresentation(Vec2(random()*t_w+t_min.x, random()*t_h+t_min.y)), None, Landmark.POINT)
trajector = location#obj2
speaker.describe(trajector, scene, False, 1, step=0.1)
# speaker.get_all_meaning_descriptions(trajector, scene, 1)
# location = Vec2(5.68, 5.59)##Vec2(5.3, 5.5)
# speaker.demo(location, scene)
# all_desc = speaker.get_all_descriptions(location, scene, 1)
# for i in range(couple * dozen):
# speaker.communicate(scene, False)
# for desc in all_desc:
# print desc
# r = RectangleRepresentation(['table'])
# lmk = r.landmarks['l_edge']
# print lmk.get_description()
# print lmk.representation.landmarks['end'].get_description()
# print r.landmarks['ul_corner'].get_description()
# print r.landmarks['ul_corner'].distance_to( Vec2(0,0) )
# representations = [r]
# representations.extend(r.get_alt_representations())
# location = Vec2(0,0)
# landmarks_distances = []
# for representation in representations:
# for lmk in representation.get_landmarks():
# landmarks_distances.append([lmk, lmk.distance_to(location)])
# print 'Distance from POI to LLCorner landmark is %f' % r.landmarks['ll_corner'].distance_to(poi)
# print 'Distance from POI to URCorner landmark is %f' % r.landmarks['ur_corner'].distance_to(poi)
# print 'Distance from POI to LRCorner landmark is %f' % r.landmarks['lr_corner'].distance_to(poi)
# print 'Distance from POI to ULCorner landmark is %f' % r.landmarks['ul_corner'].distance_to(poi)
# print 'Distance from POI to Center landmark is %f' % r.landmarks['center'].distance_to(poi)
# print 'Distance from POI to LEdge landmark is %f' % r.landmarks['l_edge'].distance_to(poi)
# print 'Distance from POI to REdge landmark is %f' % r.landmarks['r_edge'].distance_to(poi)
# print 'Distance from POI to NEdge landmark is %f' % r.landmarks['n_edge'].distance_to(poi)
# print 'Distance from POI to FEdge landmark is %f' % r.landmarks['f_edge'].distance_to(poi)