8.1 KiB
Python Cookbook
This section contains a list of recipes that complement the tutorial and are used to illustrate the use of Python API methods.
Each recipe has a list of python API classes, which is divided into those in which the recipe is centered, and those that need to be used.
There are more recipes to come!
Actor Spectator Recipe
This recipe spawns an actor and the spectator camera at the actor's location.
Focused on:
carla.World
carla.Actor
Used:
carla.WorldSnapshot
carla.ActorSnapshot
# ...
world = client.get_world()
spectator = world.get_spectator()
vehicle_bp = random.choice(world.get_blueprint_library().filter('vehicle.bmw.*'))
transform = random.choice(world.get_map().get_spawn_points())
vehicle = world.try_spawn_actor(vehicle_bp, transform)
# Wait for world to get the vehicle actor
world.tick()
world_snapshot = world.wait_for_tick()
actor_snapshot = world_snapshot.find(vehicle.id)
# Set spectator at given transform (vehicle transform)
spectator.set_transform(actor_snapshot.get_transform())
# ...
Attach Sensors Recipe
This recipe attaches different camera / sensors to a vehicle with different attachments.
Focused on:
carla.Sensor
carla.AttachmentType
Used:
carla.World
# ...
camera = world.spawn_actor(rgb_camera_bp, transform, attach_to=vehicle, attachment_type=Attachment.SpringArm)
# Default attachment: Attachment.Rigid
gnss_sensor = world.spawn_actor(sensor_gnss_bp, transform, attach_to=vehicle)
collision_sensor = world.spawn_actor(sensor_collision_bp, transform, attach_to=vehicle)
lane_invasion_sensor = world.spawn_actor(sensor_lane_invasion_bp, transform, attach_to=vehicle)
# ...
Actor Attribute Recipe
This recipe changes attributes of different type of blueprint actors.
Focused on:
carla.ActorAttribute
carla.ActorBlueprint
Used:
carla.World
carla.BlueprintLibrary
# ...
walker_bp = world.get_blueprint_library().filter('walker.pedestrian.0002')
walker_bp.set_attribute('is_invincible', True)
# ...
# Changes attribute randomly by the recommended value
vehicle_bp = wolrd.get_blueprint_library().filter('vehicle.bmw.*')
color = random.choice(vehicle_bp.get_attribute('color').recommended_values)
vehicle_bp.set_attribute('color', color)
# ...
camera_bp = world.get_blueprint_library().filter('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', 600)
camera_bp.set_attribute('image_size_y', 600)
# ...
Converted Image Recipe
This recipe applies a color conversion to the image taken by a camera sensor, so it is converted to a semantic segmentation image.
Focused on:
carla.ColorConverter
carla.Sensor
# ...
camera_bp = world.get_blueprint_library().filter('sensor.camera.semantic_segmentation')
# ...
cc = carla.ColorConverter.CityScapesPalette
camera.listen(lambda image: image.save_to_disk('output/%06d.png' % image.frame, cc))
# ...
Lanes Recipe
This recipe shows the current traffic rules affecting the vehicle. Shows the current lane type and if a lane change can be done in the actual lane or the surrounding ones.
Focused on:
carla.LaneMarking
carla.LaneMarkingType
carla.LaneChange
carla.LaneType
Used:
carla.Waypoint
carla.World
# ...
waypoint = world.get_map().get_waypoint(vehicle.get_location(),project_to_road=True, lane_type=(carla.LaneType.Driving | carla.LaneType.Shoulder | carla.LaneType.Sidewalk))
print("Current lane type: " + str(waypoint.lane_type))
# Check current lane change allowed
print("Current Lane change: " + str(waypoint.lane_change))
# Left and Right lane markings
print("L lane marking type: " + str(waypoint.left_lane_marking.type))
print("L lane marking change: " + str(waypoint.left_lane_marking.lane_change))
print("R lane marking type: " + str(waypoint.right_lane_marking.type))
print("R lane marking change: " + str(waypoint.right_lane_marking.lane_change))
# ...
Debug Bounding Box Recipe
This recipe shows how to draw traffic light actor bounding boxes from a world snapshot.
Focused on:
carla.DebugHelper
carla.BoundingBox
Used:
carla.ActorSnapshot
carla.Actor
carla.Vector3D
carla.Color
# ....
debug = world.debug
world_snapshot = world.get_snapshot()
for actor_snapshot in world_snapshot:
actual_actor = world.get_actor(actor_snapshot.id)
if actual_actor.type_id == 'traffic.traffic_light':
debug.draw_box(carla.BoundingBox(actor_snapshot.get_transform().location,carla.Vector3D(0.5,0.5,2)),actor_snapshot.get_transform().rotation, 0.05, carla.Color(255,0,0,0),0)
# ...
Debug Vehicle Trail Recipe
This recipe is a modification of
lane_explorer.py
example.
It draws the path of an actor through the world, printing information at each waypoint.
Focused on:
carla.DebugHelper
carla.Waypoint
carla.Actor
Used:
carla.ActorSnapshot
carla.Vector3D
carla.LaneType
carla.Color
carla.Map
# ...
current_w = map.get_waypoint(vehicle.get_location())
while True:
next_w = map.get_waypoint(vehicle.get_location(), lane_type=carla.LaneType.Driving | carla.LaneType.Shoulder | carla.LaneType.Sidewalk )
# Check if the vehicle is moving
if next_w.id != current_w.id:
vector = vehicle.get_velocity()
# Check if the vehicle is on a sidewalk
if current_w.lane_type == carla.LaneType.Sidewalk:
draw_waypoint_union(debug, current_w, next_w, cyan if current_w.is_junction else red, 60)
else:
draw_waypoint_union(debug, current_w, next_w, cyan if current_w.is_junction else green, 60)
debug.draw_string(current_w.transform.location, str('%15.0f km/h' % (3.6 * math.sqrt(vector.x**2 + vector.y**2 + vector.z**2))), False, orange, 60)
draw_transform(debug, current_w.transform, white, 60)
# Update the current waypoint and sleep for some time
current_w = next_w
time.sleep(args.tick_time)
# ...
The image below shows how a vehicle loses control and drives on a sidewalk. The trail shows the path it was following and the speed at each waypoint.
Traffic lights Recipe
This recipe changes from red to green the traffic light that affects the vehicle. This is done by detecting if the vehicle actor is at a traffic light.
Focused on:
carla.TrafficLight
carla.TrafficLightState
Used:
carla.Vehicle
# ...
if vehicle_actor.is_at_traffic_light():
traffic_light = vehicle_actor.get_traffic_light()
if traffic_light.get_state() == carla.TrafficLightState.Red:
# world.hud.notification("Traffic light changed! Good to go!")
traffic_light.set_state(carla.TrafficLightState.Green)
# ...