Merge pull request #315 from carla-simulator/visualize_start_pos_example

Showing all the start positions.
This commit is contained in:
Néstor Subirón 2018-04-04 12:57:29 +02:00 committed by GitHub
commit d289125e98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 0 deletions

View File

@ -21,6 +21,7 @@ client_example.py | Basic usage example of the "carla" module.
manual_control.py | A GUI client in which the vehicle can be controlled manually. manual_control.py | A GUI client in which the vehicle can be controlled manually.
point_cloud_example.py | Usage example for converting depth images into a point cloud in world coordinates. point_cloud_example.py | Usage example for converting depth images into a point cloud in world coordinates.
run_benchmark.py | Run the CoRL'17 benchmark with a trivial agent. run_benchmark.py | Run the CoRL'17 benchmark with a trivial agent.
view_start_positions.py | Show all the possible start positions in a map
!!! note !!! note
If you are building CARLA from source, the Python code is inside the If you are building CARLA from source, the Python code is inside the
@ -98,3 +99,4 @@ You can see all the available options in the script's help
The other scripts present in the _"PythonClient"_ folder run in a similar The other scripts present in the _"PythonClient"_ folder run in a similar
fashion. We recommend now launching _"manual_control.py"_ for a GUI interface fashion. We recommend now launching _"manual_control.py"_ for a GUI interface
implemented with PyGame. implemented with PyGame.

View File

@ -0,0 +1,133 @@
#!/usr/bin/env python3
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""Connects with a CARLA simulator and displays the available start positions
for the current map."""
from __future__ import print_function
import argparse
import logging
import time
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from carla.client import make_carla_client
from carla.planner.map import CarlaMap
from carla.settings import CarlaSettings
from carla.tcp import TCPConnectionError
def view_start_positions(args):
# We assume the CARLA server is already waiting for a client to connect at
# host:port. The same way as in the client example.
with make_carla_client(args.host, args.port) as client:
print('CarlaClient connected')
# We load the default settings to the client.
scene = client.load_settings(CarlaSettings())
print("Received the start positions")
# We get the number of player starts, in order to detect the city.
number_of_player_starts = len(scene.player_start_spots)
if number_of_player_starts > 100: # WARNING: unsafe way to check for city, see issue #313
image = mpimg.imread("carla/planner/Town01.png")
carla_map = CarlaMap('Town01', 16.53, 50)
else:
image = mpimg.imread("carla/planner/Town02.png")
carla_map = CarlaMap('Town02', 16.53, 50)
_, ax = plt.subplots(1)
ax.imshow(image)
if args.positions == 'all':
positions_to_plot = range(len(scene.player_start_spots))
else:
positions_to_plot = map(int, args.positions.split(','))
for position in positions_to_plot:
# Check if position is valid
if position >= len(scene.player_start_spots):
raise RuntimeError('Selected position is invalid')
# Convert world to pixel coordinates
pixel = carla_map.convert_to_pixel([scene.player_start_spots[position].location.x,
scene.player_start_spots[position].location.y,
scene.player_start_spots[position].location.z])
circle = Circle((pixel[0], pixel[1]), 12, color='r', label='A point')
ax.add_patch(circle)
if not args.no_labels:
plt.text(pixel[0], pixel[1], str(position), size='x-small')
plt.show()
def main():
argparser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument(
'-v', '--verbose',
action='store_true',
dest='debug',
help='print debug information')
argparser.add_argument(
'--host',
metavar='H',
default='localhost',
help='IP of the host server (default: localhost)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port to listen to (default: 2000)')
argparser.add_argument(
'-pos', '--positions',
metavar='P',
default='all',
help='Indices of the positions that you want to plot on the map. '
'The indices must be separated by commas (default = all positions)')
argparser.add_argument(
'--no-labels',
action='store_true',
help='do not display position indices')
args = argparser.parse_args()
log_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
logging.info('listening to server %s:%s', args.host, args.port)
while True:
try:
view_start_positions(args)
print('Done.')
return
except TCPConnectionError as error:
logging.error(error)
time.sleep(1)
except RuntimeError as error:
logging.error(error)
break
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')