-
Notifications
You must be signed in to change notification settings - Fork 2
/
animate.py
240 lines (214 loc) · 7.82 KB
/
animate.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
import copy as cp
import sys
from ipdb import set_trace as st
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import os
import glob
from PIL import Image
import _pickle as pickle
from matplotlib.ticker import (AutoMinorLocator, MultipleLocator,
FormatStrFormatter, AutoMinorLocator)
from matplotlib.collections import PatchCollection
import imageio
TILESIZE = 50
CAR_COLORS = ['blue', 'red']
LIGHT_COLORS = ['green', 'red', 'yellow']
ORIENTATIONS = {'n': 270, 'e': 0, 's': 90,'w':180, 'ne':315, 'nw':225, 'se':45, 'sw':135}
START_CROSSWALK = 1
END_CROSSWALK = 5
CROSSWALK_V = 2
CROSSWALK_LOCATIONS = dict()
for i, num in enumerate(range(2*START_CROSSWALK,2*(END_CROSSWALK+1))):
CROSSWALK_LOCATIONS.update({i: (num/2, CROSSWALK_V)})
# st()
main_dir = os.path.dirname(os.path.dirname(os.path.realpath("__file__")))
car_figs = dict()
for color in CAR_COLORS:
car_figs[color] = main_dir + '/CompositionalTesting/imglib/' + color + '_car.png'
ped_figure = main_dir + '/CompositionalTesting/imglib/pedestrian_img.png'
light_figs = dict()
for color in LIGHT_COLORS:
light_figs[color] = main_dir + '/CompositionalTesting/imglib/' + color + '_light.png'
def draw_map(map, merge = False):
if merge:
lanes = map.lanes
width = map.width
x_min = 0
x_max = width * TILESIZE
y_min = 0
y_max = lanes * TILESIZE
#x_min, x_max, y_min, y_max = get_map_corners(map)
ax.axis('equal')
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# fill in the road regions
road_tiles = []
width_tiles = np.arange(0,width+1)*TILESIZE
lanes_tiles = np.arange(0,lanes+1)*TILESIZE
for i in np.arange(lanes):
for k in np.arange(width):
tile = patches.Rectangle((width_tiles[k],lanes_tiles[i]),TILESIZE,TILESIZE,linewidth=1,facecolor='k', alpha=0.4)
road_tiles.append(tile)
ax.add_collection(PatchCollection(road_tiles, match_original=True))
plt.gca().invert_yaxis()
else:
# st()
size = max(map.keys())
x_min = 0
x_max = (size[0]+1) * TILESIZE
y_min = 0
y_max = (size[1]+1) * TILESIZE
#x_min, x_max, y_min, y_max = get_map_corners(map)
ax.axis('equal')
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# fill in the road regions
road_tiles = []
width_tiles = np.arange(0,size[0]+1)*TILESIZE
lanes_tiles = np.arange(0,size[1]+1)*TILESIZE
for i in np.arange(0,size[0]+1):
for k in np.arange(0,size[1]+1):
if map[(i,k)] != '*':
tile = patches.Rectangle((width_tiles[k],lanes_tiles[i]),TILESIZE,TILESIZE,linewidth=1,facecolor='k', alpha=0.4)
road_tiles.append(tile)
ax.add_collection(PatchCollection(road_tiles, match_original=True))
# now add crosswalk on top
crosswalk_tiles = []
for item in CROSSWALK_LOCATIONS.keys():
if item % 2 == 0:
color = 'silver'
alpha = 0.5
else:
color = 'k'
alpha = 0.5
width = CROSSWALK_LOCATIONS[item][1]*TILESIZE
lanes = CROSSWALK_LOCATIONS[item][0]*TILESIZE
tile = patches.Rectangle((width,lanes),TILESIZE,TILESIZE/2,linewidth=1,facecolor=color, alpha=alpha)
crosswalk_tiles.append(tile)
ax.add_collection(PatchCollection(crosswalk_tiles, match_original=True))
plt.gca().invert_yaxis()
def draw_timestamp(t, merge = False):
if merge:
ax.text(0.5,0.7,t, transform=plt.gcf().transFigure,fontsize='large',
bbox={"boxstyle" : "circle", "color":"white", "ec":"black"})
else:
ax.text(0.3,0.7,t, transform=plt.gcf().transFigure,fontsize='large',
bbox={"boxstyle" : "circle", "color":"white", "ec":"black"})
def draw_pedestrian(ped_data):
name, _, _, cwloc = ped_data
x_tile = CROSSWALK_LOCATIONS[cwloc][1]
y_tile = CROSSWALK_LOCATIONS[cwloc][0]
x = (x_tile) * TILESIZE
y = (y_tile) * TILESIZE - TILESIZE/2
ped_fig = Image.open(ped_figure)
ped_fig = ped_fig.rotate(180, expand=False)
offset = 0.1
ax.imshow(ped_fig, zorder=1, interpolation='bilinear', extent=[x+10, x+TILESIZE-10, y+2, y+TILESIZE-2])
def draw_car(car_data, merge = False):
if merge:
if car_data[0]=='system':
color = 'red'
else:
color = 'blue'
theta_d = 0
name, x_tile, y_tile = car_data
x = (x_tile-1) * TILESIZE
y = (y_tile-1) * TILESIZE
car_fig = Image.open(car_figs[color])
car_fig = car_fig.rotate(theta_d, expand=False)
offset = 0.1
ax.imshow(car_fig, zorder=1, interpolation='none', extent=[x+5, x+TILESIZE-5, y+5, y+TILESIZE-5])
else:
if car_data[0]=='ego':
color = 'red'
else:
color = 'blue'
# st()
name, y_tile, x_tile, orientation = car_data
theta_d = ORIENTATIONS[orientation]
x = (x_tile) * TILESIZE
y = (y_tile) * TILESIZE
car_fig = Image.open(car_figs[color])
car_fig = car_fig.rotate(theta_d, expand=False)
offset = 0.1
ax.imshow(car_fig, zorder=1, interpolation='bilinear', extent=[x+2, x+TILESIZE-2, y+2, y+TILESIZE-2])
def plot_sys_cars(agents):
for i, agent in enumerate(agents):
draw_car(agent)
def plot_tester_cars(agents):
for i, agent in enumerate(agents):
draw_car(agent)
def plot_peds(agents):
for i,agent in enumerate(agents):
draw_pedestrian(agent)
def draw_traffic_light(timestep):
light = timestep % 30
if light < 15:
color = 'green'
elif 15 <= light <= 22:
color = 'yellow'
else:
color = 'red'
theta_d = 180
light_fig = Image.open(light_figs[color])
light_fig = light_fig.rotate(theta_d, expand=False)
offset = 0.1
ax.imshow(light_fig, zorder=1, interpolation='bilinear', extent=[TILESIZE*5+17, TILESIZE*6-17, TILESIZE*2+5, TILESIZE*3-5])
def animate_images(output_dir):
# Create the frames
frames = []
imgs = glob.glob(output_dir+'plot_'"*.png")
imgs.sort()
for i in imgs:
new_frame = Image.open(i)
frames.append(new_frame)
# Save into a GIF file that loops forever
frames[0].save(output_dir + 'png_to_gif.gif', format='GIF',
append_images=frames[1:],
save_all=True,
duration=200, loop=3)
def traces_to_animation(filename, output_dir):
# extract out traces from pickle file
with open(filename, 'rb') as pckl_file:
traces = pickle.load(pckl_file)
##
t_start = 0
t_end = len(traces)
# t_start = traces[0].timestamp
# t_end = traces[-1].timestamp
map = traces[0].map
#import pdb; pdb.set_trace()
global ax
fig, ax = plt.subplots()
t_array = np.arange(t_end)
# plot map once
for t in t_array:
print(t)
plt.gca().cla()
draw_map(map)
sys_agents = traces[t].ego
tester_agents = traces[t].env
ped_agents = traces[t].peds
plot_sys_cars(sys_agents)
plot_tester_cars(tester_agents)
plot_peds(ped_agents)
draw_timestamp(t)
draw_traffic_light(t)
plot_name = str(t).zfill(5)
img_name = output_dir+'/plot_'+plot_name+'.png'
fig.savefig(img_name)
animate_images(output_dir)
def make_animation():
output_dir = os.getcwd()+'/animations/gifs/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
traces_file = os.getcwd()+'/intersection/saved_traces/sim_trace.p'
traces_to_animation(traces_file, output_dir)
if __name__ == '__main__':
make_animation()