carla/PythonAPI/examples/synchronous_mode.py

208 lines
6.0 KiB
Python
Raw Permalink Normal View History

2019-03-01 06:09:26 +08:00
#!/usr/bin/env python
2019-03-29 17:58:05 +08:00
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
2019-03-01 06:09:26 +08:00
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
import glob
import os
import sys
try:
2019-03-29 02:15:13 +08:00
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
2019-03-01 06:09:26 +08:00
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import random
try:
import pygame
except ImportError:
raise RuntimeError('cannot import pygame, make sure pygame package is installed')
try:
import numpy as np
except ImportError:
raise RuntimeError('cannot import numpy, make sure numpy package is installed')
try:
import queue
except ImportError:
import Queue as queue
2019-07-01 20:25:11 +08:00
class CarlaSyncMode(object):
"""
Context manager to synchronize output from different sensors. Synchronous
mode is enabled as long as we are inside this context
with CarlaSyncMode(world, sensors) as sync_mode:
while True:
data = sync_mode.tick(timeout=1.0)
"""
2019-07-03 00:56:57 +08:00
def __init__(self, world, *sensors, **kwargs):
2019-07-01 20:25:11 +08:00
self.world = world
self.sensors = sensors
self.frame = None
2019-07-03 00:56:57 +08:00
self.delta_seconds = 1.0 / kwargs.get('fps', 20)
2019-07-01 20:25:11 +08:00
self._queues = []
2019-07-03 00:56:57 +08:00
self._settings = None
2019-07-01 20:25:11 +08:00
def __enter__(self):
2019-07-03 00:56:57 +08:00
self._settings = self.world.get_settings()
self.frame = self.world.apply_settings(carla.WorldSettings(
no_rendering_mode=False,
synchronous_mode=True,
fixed_delta_seconds=self.delta_seconds))
2019-07-01 20:25:11 +08:00
def make_queue(register_event):
q = queue.Queue()
register_event(q.put)
self._queues.append(q)
make_queue(self.world.on_tick)
for sensor in self.sensors:
make_queue(sensor.listen)
return self
def tick(self, timeout):
self.frame = self.world.tick()
data = [self._retrieve_data(q, timeout) for q in self._queues]
assert all(x.frame == self.frame for x in data)
return data
def __exit__(self, *args, **kwargs):
2019-07-03 00:56:57 +08:00
self.world.apply_settings(self._settings)
2019-07-01 20:25:11 +08:00
def _retrieve_data(self, sensor_queue, timeout):
while True:
data = sensor_queue.get(timeout=timeout)
if data.frame == self.frame:
return data
def draw_image(surface, image, blend=False):
2019-03-01 06:09:26 +08:00
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
array = array[:, :, :3]
array = array[:, :, ::-1]
image_surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
2019-07-01 20:25:11 +08:00
if blend:
image_surface.set_alpha(100)
2019-03-01 06:09:26 +08:00
surface.blit(image_surface, (0, 0))
def get_font():
fonts = [x for x in pygame.font.get_fonts()]
default_font = 'ubuntumono'
font = default_font if default_font in fonts else fonts[0]
font = pygame.font.match_font(font)
return pygame.font.Font(font, 14)
def should_quit():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
return True
return False
def main():
actor_list = []
pygame.init()
2019-07-01 20:25:11 +08:00
display = pygame.display.set_mode(
(800, 600),
pygame.HWSURFACE | pygame.DOUBLEBUF)
font = get_font()
clock = pygame.time.Clock()
2019-03-01 06:09:26 +08:00
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)
world = client.get_world()
try:
m = world.get_map()
start_pose = random.choice(m.get_spawn_points())
waypoint = m.get_waypoint(start_pose.location)
blueprint_library = world.get_blueprint_library()
vehicle = world.spawn_actor(
random.choice(blueprint_library.filter('vehicle.*')),
start_pose)
actor_list.append(vehicle)
vehicle.set_simulate_physics(False)
2019-07-01 20:25:11 +08:00
camera_rgb = world.spawn_actor(
2019-03-01 06:09:26 +08:00
blueprint_library.find('sensor.camera.rgb'),
carla.Transform(carla.Location(x=-5.5, z=2.8), carla.Rotation(pitch=-15)),
attach_to=vehicle)
2019-07-01 20:25:11 +08:00
actor_list.append(camera_rgb)
2019-03-01 06:09:26 +08:00
2019-07-01 20:25:11 +08:00
camera_semseg = world.spawn_actor(
blueprint_library.find('sensor.camera.semantic_segmentation'),
carla.Transform(carla.Location(x=-5.5, z=2.8), carla.Rotation(pitch=-15)),
attach_to=vehicle)
actor_list.append(camera_semseg)
2019-03-01 06:09:26 +08:00
2019-07-01 20:25:11 +08:00
# Create a synchronous mode context.
2019-07-03 00:56:57 +08:00
with CarlaSyncMode(world, camera_rgb, camera_semseg, fps=30) as sync_mode:
2019-03-01 06:09:26 +08:00
while True:
2019-07-01 20:25:11 +08:00
if should_quit():
return
clock.tick()
# Advance the simulation and wait for the data.
snapshot, image_rgb, image_semseg = sync_mode.tick(timeout=2.0)
# Choose the next waypoint and update the car location.
waypoint = random.choice(waypoint.next(1.5))
vehicle.set_transform(waypoint.transform)
image_semseg.convert(carla.ColorConverter.CityScapesPalette)
2019-07-03 00:56:57 +08:00
fps = round(1.0 / snapshot.timestamp.delta_seconds)
2019-07-01 20:25:11 +08:00
# Draw the display.
draw_image(display, image_rgb)
draw_image(display, image_semseg, blend=True)
display.blit(
font.render('% 5d FPS (real)' % clock.get_fps(), True, (255, 255, 255)),
(8, 10))
display.blit(
font.render('% 5d FPS (simulated)' % fps, True, (255, 255, 255)),
(8, 28))
pygame.display.flip()
2019-03-01 06:09:26 +08:00
finally:
print('destroying actors.')
for actor in actor_list:
actor.destroy()
pygame.quit()
print('done.')
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')