in scripts/interactive_demo.py [0:0]
def interactive_demo(config, env):
# Set the width and height of the screen [width, height]
pygame.init()
size = (728, 256)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Interactive Demo")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
frames = list()
audios = list()
observation = env.reset()
rgb_image = np.swapaxes(observation['rgb'], 0, 1)
# screen.blit(pygame.surfarray.make_surface(rgb_image), (0, 0))
pygame.display.flip()
# -------- Main Program Loop -----------
keys = []
while not done:
# --- Main event loop
def wait():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
action = None
if event.key == pygame.K_w: # w
action = HabitatSimActions.MOVE_FORWARD
keys.append('w')
elif event.key == pygame.K_a: # a
action = HabitatSimActions.TURN_LEFT
keys.append('a')
elif event.key == pygame.K_d: # d
action = HabitatSimActions.TURN_RIGHT
keys.append('d')
elif event.key == pygame.K_f: # f
action = HabitatSimActions.STOP
keys.append('f')
if action is not None:
return action
action = wait()
# --- Game logic should go here
observation, reward, done, info = env.step(**{'action': action})
if env.get_done(None):
# observation = env.reset()
break
if config.TASK_CONFIG.SIMULATOR.CONTINUOUS_VIEW_CHANGE and 'intermediate' in observation:
for obs in observation['intermediate']:
frame = observations_to_image(obs, info)
frames.append(frame)
frame = observations_to_image(observation, info)
frames.append(frame)
frame = np.swapaxes(frame, 0, 1)
audio = observation['audiogoal']
audios.append(audio)
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill((255, 255, 255))
screen.blit(pygame.surfarray.make_surface(frame), (0, 0))
# smaller_frame = block_reduce(frame, block_size=(down_sampling, down_sampling, 1), func=np.mean)
# screen.blit(pygame.surfarray.make_surface(smaller_frame), (0, 0))
# play sound
# temp_file = 'data/temp/temp.wav'
# sr = config.TASK_CONFIG.SIMULATOR.AUDIO.RIR_SAMPLING_RATE
# audio = np.int16(audio * 32767).T
# wavfile.write(temp_file, sr, audio)
# pygame.mixer.music.load(temp_file)
# pygame.mixer.music.play(-1)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(1)
# Close the window and quit.
pygame.quit()
env.close()
print('Keys: {}'.format(','.join(keys)))
# write frames and audio into videos
video_dir = 'data/visualizations/demo'
video_name = 'demo'
fps = config.TASK_CONFIG.SIMULATOR.VIEW_CHANGE_FPS \
if config.TASK_CONFIG.SIMULATOR.CONTINUOUS_VIEW_CHANGE else 1
images_to_video_with_audio(frames, video_dir, video_name, audios,
sr=config.TASK_CONFIG.SIMULATOR.AUDIO.RIR_SAMPLING_RATE, fps=fps)