Example #1
0
def get_present_vehicles(role_name_prefix: str,
                         world: carla.World) -> List[carla.Actor]:
    all_actors: carla.ActorList = world.get_actors().filter('vehicle.*')
    return [
        a for a in all_actors
        if has_prefixed_attribute(a, 'role_name', role_name_prefix)
    ]
Example #2
0
def get_pedestrians_surface(
    world: carla.World,  # pylint: disable=no-member
    pixels_per_meter: int = 5,
    scale: float = 1.0,
    margin: int = 150,
) -> pygame.Surface:
    """Generates a `PyGame` surface of pedestrians.

  Args:
    world: The `CARLA` world.
    pixels_per_meter: The number of pixels rendered per meter.
    scale: The scaling factor of the rendered map.
    margin: The number of pixels used for margin.

  Returns
    The pedestrians rendered as a `PyGame` surface.
  """
    # Fetch CARLA map.
    carla_map = world.get_map()

    # Fetch all the pedestrians.
    pedestrians = [
        actor for actor in world.get_actors()
        if "walker.pedestrian" in actor.type_id
    ]

    # Setups the `PyGame` surface and offsets.
    world_offset, surface = draw_settings(
        carla_map=carla_map,
        pixels_per_meter=pixels_per_meter,
        scale=scale,
        margin=margin,
    )

    # Set background black
    surface.fill(COLORS["BLACK"])

    # Iterate over pedestrians.
    for pedestrian in pedestrians:
        # Draw pedestrian as a rectangular.
        corners = actor_to_corners(pedestrian)
        # Convert to pixel coordinates.
        corners = [
            world_to_pixel(p, scale, world_offset, pixels_per_meter)
            for p in corners
        ]
        pygame.draw.polygon(surface, COLORS["WHITE"], corners)

    return surface
def set_traffic_lights_green(world: carla.World):
    set_world_asynchronous(world)
    for tl in world.get_actors().filter('traffic.*'):
        if isinstance(tl, carla.TrafficLight):
            tl.set_state(carla.TrafficLightState.Green)
            tl.freeze(True)
Example #4
0
def get_traffic_lights_surface(
    world: carla.World,  # pylint: disable=no-member
    pixels_per_meter: int = 5,
    scale: float = 1.0,
    margin: int = 150,
) -> Tuple[pygame.Surface, pygame.Surface, pygame.Surface]:
    """Generates three `PyGame` surfaces of traffic lights (Green, Yellow, Red).

  Args:
    world: The `CARLA` world.
    pixels_per_meter: The number of pixels rendered per meter.
    scale: The scaling factor of the rendered map.
    margin: The number of pixels used for margin.

  Returns
    The traffic lights rendered as `PyGame` surface (Green, Yellow, Red).
  """
    # Fetch CARLA map.
    carla_map = world.get_map()

    # Fetch all the pedestrians.
    traffic_lights = [
        actor for actor in world.get_actors()
        if "traffic.traffic_light" in actor.type_id
    ]

    # Setups the `PyGame` surface and offsets.
    world_offset, green_surface = draw_settings(
        carla_map=carla_map,
        pixels_per_meter=pixels_per_meter,
        scale=scale,
        margin=margin,
    )
    width = green_surface.get_width()
    height = green_surface.get_height()
    yellow_surface = pygame.Surface((width, height))  # pylint: disable=too-many-function-args
    red_surface = pygame.Surface((width, height))  # pylint: disable=too-many-function-args

    # Set background black
    green_surface.fill(COLORS["BLACK"])
    yellow_surface.fill(COLORS["BLACK"])
    red_surface.fill(COLORS["BLACK"])

    # Iterate over pedestrians.
    for traffic_light in traffic_lights:
        # Identify state of the traffic light.
        if traffic_light.state.name == "Green":
            surface = green_surface
        elif traffic_light.state.name == "Yellow":
            surface = yellow_surface
        elif traffic_light.state.name == "Red":
            surface = red_surface
        else:
            continue
        # Convert to pixel coordinates.
        center = world_to_pixel(
            traffic_light.get_transform().location,
            scale,
            world_offset,
            pixels_per_meter,
        )
        pygame.draw.circle(surface, COLORS["WHITE"], center, 10)

    return green_surface, yellow_surface, red_surface