def spawn_sensors(world: carla.World,
                  actor: carla.Actor) -> Dict[str, carla.Sensor]:
    sensors = {}
    blueprints = {
        "rgb":
        world.get_blueprint_library().find('sensor.camera.rgb'),
        "segmentation":
        world.get_blueprint_library().find(
            'sensor.camera.semantic_segmentation'),
        "depth":
        world.get_blueprint_library().find('sensor.camera.depth')
    }

    fov = random.randint(60, 110)
    yaw = random.gauss(0, 45)
    sensor_transform = carla.Transform(carla.Location(0, 0, 3),
                                       carla.Rotation(0, yaw, 0))
    for sensor_name, blueprint in blueprints.items():
        blueprint.set_attribute("image_size_x", str(1920 / 2))
        blueprint.set_attribute("image_size_y", str(1080 / 2))
        blueprint.set_attribute("fov", str(fov))
        blueprint.set_attribute(
            "sensor_tick", str(SENSOR_TICK))  # Get new data every x second
        sensor: carla.Sensor = world.spawn_actor(blueprint,
                                                 sensor_transform,
                                                 attach_to=actor)
        sensors[sensor_name] = sensor
    return sensors
예제 #2
0
def spawn_vehicles(
    world: carla.World,  # pylint: disable=no-member
    num_vehicles: int,
) -> Sequence[carla.Vehicle]:  # pylint: disable=no-member
    """Spawns `vehicles` randomly in spawn points.

  Args:
    world: The world object associated with the simulation.
    num_vehicles: The number of vehicles to spawn.

  Returns:
    The list of vehicles actors.
  """
    # Blueprints library.
    bl = world.get_blueprint_library()
    # List of spawn points.
    spawn_points = world.get_map().get_spawn_points()
    # Output container
    actors = list()
    for _ in range(num_vehicles):
        # Fetch random blueprint.
        vehicle_bp = random.choice(bl.filter("vehicle.*"))
        # Attempt to spawn vehicle in random location.
        actor = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points))
        if actor is not None:
            # Enable autopilot.
            actor.set_autopilot(True)
            # Append actor to the list.
            actors.append(actor)
    logging.debug("Spawned {} other vehicles".format(len(actors)))
    return actors
예제 #3
0
def spawn_hero(
    world: carla.World,  # pylint: disable=no-member
    spawn_point: carla.Transform,  # pylint: disable=no-member
    vehicle_id: Optional[str] = None,
) -> carla.Vehicle:  # pylint: disable=no-member
    """Spawns `hero` in `spawn_point`.

  Args:
    world: The world object associated with the simulation.
    spawn_point: The point to spawn the hero actor.
    vehicle_id: An (optional) valid id for the vehicle object.

  Returns:
    The actor (vehicle) object.
  """
    # Blueprints library.
    bl = world.get_blueprint_library()
    if vehicle_id is not None:
        # Get the specific vehicle from the library.
        hero_bp = bl.find(vehicle_id)
    else:
        # Randomly choose a vehicle from the list.
        hero_bp = random.choice(bl.filter("vehicle.*"))
    # Rename the actor to `hero`.
    hero_bp.set_attribute("role_name", "hero")
    logging.debug("Spawns hero actor at {}".format(
        carla_xyz_to_ndarray(spawn_point.location)))
    hero = world.spawn_actor(hero_bp, spawn_point)
    return hero
예제 #4
0
def get_blueprints(world: carla.World,
                   vehicles_filter: str = None,
                   pedestrians_filter: str = None,
                   safe=True):
    """Returns a list of blueprints for vehicles and pedestrians"""
    if vehicles_filter is None:
        vehicles_filter = 'vehicle.*'

    if pedestrians_filter is None:
        pedestrians_filter = 'walker.pedestrian.*'

    blueprint_lib = world.get_blueprint_library()

    vehicles_blueprints = blueprint_lib.filter(vehicles_filter)
    walkers_blueprints = blueprint_lib.filter(pedestrians_filter)

    if safe:
        vehicles_blueprints = filter(
            lambda b: (int(b.get_attribute('number_of_wheels')) == 4) and
            not (b.id.endswith('isetta') or b.id.endswith('carlacola') or b.id.
                 endswith('cybertruck') or b.id.endswith('t2')),
            vehicles_blueprints)
        vehicles_blueprints = list(vehicles_blueprints)

    return vehicles_blueprints, walkers_blueprints
예제 #5
0
def random_blueprint(world: carla.World,
                     actor_filter='vehicle.*',
                     role_name='agent') -> carla.ActorBlueprint:
    """Retrieves a random blueprint.
        :param world: a carla.World instance.
        :param actor_filter: a string used to filter (select) blueprints. Default: 'vehicle.*'
        :param role_name: blueprint's role_name, Default: 'agent'.
        :return: a carla.ActorBlueprint instance.
    """
    blueprints = world.get_blueprint_library().filter(actor_filter)
    blueprint: carla.ActorBlueprint = random.choice(blueprints)
    blueprint.set_attribute('role_name', role_name)

    if blueprint.has_attribute('color'):
        color = random.choice(
            blueprint.get_attribute('color').recommended_values)
        blueprint.set_attribute('color', color)

    if blueprint.has_attribute('driver_id'):
        driver_id = random.choice(
            blueprint.get_attribute('driver_id').recommended_values)
        blueprint.set_attribute('driver_id', driver_id)

    if blueprint.has_attribute('is_invincible'):
        blueprint.set_attribute('is_invincible', 'true')

    # set the max speed
    if blueprint.has_attribute('speed'):
        float(blueprint.get_attribute('speed').recommended_values[1])
        float(blueprint.get_attribute('speed').recommended_values[2])
    else:
        print("No recommended values for 'speed' attribute")

    return blueprint
예제 #6
0
    def __init__(self, carla_world: carla.World, ego_vehicle: carla.Vehicle):
        # Params for images
        self.img_width = 1280 # in [px]
        self.img_height = 720 # in [px]
        self.fov_horizontal = 120 # in [deg]
        self.display_width = 640 # in [px]
        self.display_height = 360 # in [px]

        # Params for static camera calibration
        self.translation_x = 0.4 # in [m] from center of car
        self.translation_z = 1.5 # in [m] from bottom of car
        self.translation_y = 0.0 # in [m] from center of car
        self.pitch = 0.0 # in [deg]
        self.roll = 0.0 # in [deg]
        self.yaw = 0.0 # in [deg]

        # Images that are stored on disk per frame
        self.rgb_img: np.array = np.zeros((self.img_height, self.img_width, 3), dtype=np.uint8)
        self.depth_img: np.array = np.zeros((self.img_height, self.img_width, 3), dtype=np.uint8)
        self.semseg_img: np.array = np.zeros((self.img_height, self.img_width, 3), dtype=np.uint8)

        # Images that are displayed in pygame for visualization (note spectator img is just needed for visu)
        self.display_spectator_img: np.array = np.zeros((self.display_height, self.display_width, 3), dtype=np.uint8)
        self.display_rgb_img: np.array = np.zeros((self.display_height, self.display_width, 3), dtype=np.uint8)
        self.display_depth_img: np.array = np.zeros((self.display_height, self.display_width, 3), dtype=np.uint8)
        self.display_semseg_img: np.array = np.zeros((self.display_height, self.display_width, 3), dtype=np.uint8)

        # Create sensors
        blueprint_library: carla.BlueprintLibrary = carla_world.get_blueprint_library()
        cam_transform = carla.Transform(
            carla.Location(x=self.translation_x, z=self.translation_z, y=self.translation_y),
            carla.Rotation(pitch=self.pitch, yaw=self.yaw, roll=self.roll)
        )

        # Attach dummy rgb sensor to follow the ego vehicle (only for visulization)
        spectator_bp = blueprint_library.find('sensor.camera.rgb')
        self._add_attr_to_blueprint(spectator_bp)
        spectator_transform = carla.Transform(carla.Location(x=-6, z=3))
        self.spectator: carla.Actor = carla_world.spawn_actor(spectator_bp, spectator_transform, attach_to=ego_vehicle)
        self.spectator.listen(self._read_spectator_img)

        # Attach rgb cam sensors to ego vehicle
        cam_rgb_bp = blueprint_library.find('sensor.camera.rgb')
        self._add_attr_to_blueprint(cam_rgb_bp)
        self.cam_rgb: carla.Actor = carla_world.spawn_actor(cam_rgb_bp, cam_transform, attach_to=ego_vehicle)
        self.cam_rgb.listen(self._read_rgb_img)

        # Attach depth sensor to ego vehicle
        cam_depth_bp = blueprint_library.find("sensor.camera.depth")
        self._add_attr_to_blueprint(cam_depth_bp)
        self.cam_depth: carla.Actor = carla_world.spawn_actor(cam_depth_bp, cam_transform, attach_to=ego_vehicle)
        self.cam_depth.listen(self._read_depth_img)

        # Attach semseg sensor to ego vehicle
        cam_semseg_bp = blueprint_library.find("sensor.camera.semantic_segmentation")
        self._add_attr_to_blueprint(cam_semseg_bp)
        self.cam_semseg: carla.Actor = carla_world.spawn_actor(cam_semseg_bp, cam_transform, attach_to=ego_vehicle)
        self.cam_semseg.listen(self._read_semseg_img)
    def __init__(self, world: carla.World, carla_vehicle: carla.Vehicle):
        self.has_collided = False

        def on_collision(e):
            self.has_collided = True

        blueprint_library = world.get_blueprint_library()
        blueprint = blueprint_library.find('sensor.other.collision')
        self._collision_sensor = world.spawn_actor(
            blueprint, carla_vehicle.get_transform(), attach_to=carla_vehicle)
        self._collision_sensor.listen(on_collision)
예제 #8
0
def prepare_ego_vehicle(world: carla.World) -> carla.Actor:
    car_blueprint = world.get_blueprint_library().find("vehicle.audi.a2")

    # This will allow external scripts like manual_control.py or no_rendering_mode.py
    # from the official CARLA examples to take control over the ego agent
    car_blueprint.set_attribute("role_name", "hero")

    # spawn points doesnt matter - scenario sets up position in reset
    ego_vehicle = world.spawn_actor(
        car_blueprint,
        carla.Transform(carla.Location(0, 0, 500), carla.Rotation()))

    assert ego_vehicle is not None, "Ego vehicle could not be spawned"

    # Setup any car sensors you like, collect observations and then use them as input to your model
    return ego_vehicle
예제 #9
0
def spawn_pedestrians(
    world: carla.World,  # pylint: disable=no-member
    num_pedestrians: int,
    speeds: Sequence[float] = (1.0, 1.5, 2.0),
) -> Sequence[carla.Vehicle]:  # pylint: disable=no-member
    """Spawns `pedestrians` in random locations.

  Args:
    world: The world object associated with the simulation.
    num_pedestrians: The number of pedestrians to spawn.
    speeds: The valid set of speeds for the pedestrians.

  Returns:
    The list of pedestrians actors.
  """
    # Blueprints library.
    bl = world.get_blueprint_library()
    # Output container
    actors = list()
    for n in range(num_pedestrians):
        # Fetch random blueprint.
        pedestrian_bp = random.choice(bl.filter("walker.pedestrian.*"))
        # Make pedestrian invicible.
        pedestrian_bp.set_attribute("is_invincible", "true")
        while len(actors) != n:
            # Get random location.
            spawn_point = carla.Transform()  # pylint: disable=no-member
            spawn_point.location = world.get_random_location_from_navigation()
            if spawn_point.location is None:
                continue
            # Attempt to spawn vehicle in random location.
            actor = world.try_spawn_actor(pedestrian_bp, spawn_point)
            player_control = carla.WalkerControl()
            player_control.speed = 3
            pedestrian_heading = 90
            player_rotation = carla.Rotation(0, pedestrian_heading, 0)
            player_control.direction = player_rotation.get_forward_vector()
            if actor is not None:
                actor.apply_control(player_control)
                actors.append(actor)
    logging.debug("Spawned {} pedestrians".format(len(actors)))
    return actors
예제 #10
0
 def __init__(self,
              client: carla.Client,
              world: carla.World,
              safe_mode=True):
     self.client = client
     self.world = world
     self.spawn_points = self.world.get_map().get_spawn_points()
     self.blueprints = self.world.get_blueprint_library().filter(
         "vehicle.*")
     self.blueprintsWalkers = world.get_blueprint_library().filter(
         "walker.pedestrian.*")
     self.vehicles_list: List[int] = []
     self.walkers_list = []
     self.all_id = []
     self.all_actors = []
     self.safe_mode = safe_mode
     self._bad_colors = [
         "255,255,255", "183,187,162", "237,237,237", "134,134,134",
         "243,243,243", "127,130,135", "109,109,109", "181,181,181",
         "140,140,140", "181,178,124", "171,255,0", "251,241,176",
         "158,149,129", "233,216,168", "233,216,168", "108,109,126",
         "193,193,193", "227,227,227", "151,150,125", "206,206,206",
         "255,222,218", "211,211,211", "191,191,191"
     ] if safe_mode else []
예제 #11
0
def get_blueprint(world: carla.World, actor_id: str) -> carla.ActorBlueprint:
    return world.get_blueprint_library().find(actor_id)