Exemple #1
0
def test_1_npc_appear(carla_Town03_synchronous):
    """Test case where NPC appears before EGO vehicle after awhile.
    """
    client, world, carla_map, traffic_manager = carla_Town03_synchronous
    ego_vehicle = None
    other_vehicles = []
    data_collector = None

    n_burn_frames = 180
    save_frequency = 40
    record_interval = 5
    scene_interval = 30
    try:
        # Mock vehicles
        spawn_points = carla_map.get_spawn_points()
        blueprint = world.get_blueprint_library().find('vehicle.audi.a2')
        spawn_point = spawn_points[251]
        ego_vehicle = world.spawn_actor(blueprint, spawn_point)
        ego_vehicle.set_autopilot(True, traffic_manager.get_port())

        other_vehicle_ids = []
        blueprints = get_all_vehicle_blueprints(world)
        for idx in [164]:
            blueprint = np.random.choice(blueprints)
            spawn_point = spawn_points[idx]
            other_vehicle = world.spawn_actor(blueprint, spawn_point)
            other_vehicle.set_autopilot(True, traffic_manager.get_port())
            other_vehicles.append(other_vehicle)
            other_vehicle_ids.append(other_vehicle.id)

        # Setup data collector
        map_reader = NaiveMapQuerier(world, carla_map, debug=True)
        scene_config = SceneConfig(scene_interval=scene_interval,
                                   record_interval=record_interval)
        data_collector = DataCollector(ego_vehicle,
                                       map_reader,
                                       other_vehicle_ids,
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       debug=True)
        data_collector.start_sensor()
        assert data_collector.sensor_is_listening

        # Run simulation for X steps
        for idx in range(n_burn_frames + record_interval *
                         (save_frequency - 1)):
            frame = world.tick()
            data_collector.capture_step(frame)

    finally:
        if data_collector:
            data_collector.destroy()
        if ego_vehicle:
            ego_vehicle.destroy()
        for other_vehicle in other_vehicles:
            other_vehicle.destroy()
Exemple #2
0
def test_no_npcs(carla_Town03_synchronous):
    client, world, carla_map, traffic_manager = carla_Town03_synchronous
    ego_vehicle = None
    data_collector = None

    n_burn_frames = 60
    save_frequency = 30
    record_interval = 5
    scene_interval = 20
    try:
        # Mock vehicles
        blueprint = world.get_blueprint_library().find('vehicle.audi.a2')
        spawn_point = carla_map.get_spawn_points()[0]
        ego_vehicle = world.spawn_actor(blueprint, spawn_point)
        ego_vehicle.set_autopilot(True, traffic_manager.get_port())

        # Setup data collector
        map_reader = NaiveMapQuerier(world, carla_map, debug=True)
        scene_config = SceneConfig(scene_interval=scene_interval,
                                   record_interval=record_interval)
        data_collector = DataCollector(ego_vehicle,
                                       map_reader, [],
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       debug=True)
        data_collector.start_sensor()
        assert data_collector.sensor_is_listening

        # Run simulation for X steps
        for idx in range(n_burn_frames + record_interval *
                         (save_frequency - 1)):
            frame = world.tick()
            data_collector.capture_step(frame)

    finally:
        if data_collector:
            data_collector.destroy()
        if ego_vehicle:
            ego_vehicle.destroy()
Exemple #3
0
def test_2_data_collectors(carla_Town03_synchronous):
    """Test case where NPCs move away from the EGO vehicle and disappear.
    """
    client, world, carla_map, traffic_manager = carla_Town03_synchronous
    vehicles = []
    data_collectors = []

    n_burn_frames = 40
    save_frequency = 40
    record_interval = 5
    scene_interval = 30
    try:
        # Mock vehicles
        spawn_points = carla_map.get_spawn_points()
        vehicle_ids = []
        blueprints = get_all_vehicle_blueprints(world)
        for idx in [7, 187, 122, 221]:
            blueprint = np.random.choice(blueprints)
            spawn_point = spawn_points[idx]
            vehicle = world.spawn_actor(blueprint, spawn_point)
            vehicle.set_autopilot(True, traffic_manager.get_port())
            vehicles.append(vehicle)
            vehicle_ids.append(vehicle.id)

        # Setup data collector
        map_reader = NaiveMapQuerier(world, carla_map, debug=True)
        scene_config = SceneConfig(scene_interval=scene_interval,
                                   record_interval=record_interval)
        data_collector = DataCollector(vehicles[0],
                                       map_reader,
                                       vehicle_ids[1:],
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       debug=True)
        data_collector.start_sensor()
        data_collectors.append(data_collector)
        data_collector = DataCollector(vehicles[1],
                                       map_reader,
                                       vehicle_ids[:1] + vehicle_ids[2:],
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       debug=True)
        data_collector.start_sensor()
        data_collectors.append(data_collector)

        # Run simulation for X steps
        for idx in range(n_burn_frames + record_interval *
                         (save_frequency - 1)):
            frame = world.tick()
            for data_collector in data_collectors:
                data_collector.capture_step(frame)

    finally:
        for data_collector in data_collectors:
            data_collector.destroy()
        for vehicle in vehicles:
            vehicle.destroy()
    def __setup_actors(self, episode):
        """Setup vehicles and data collectors for an episode.

        Parameters
        ----------
        episode : int

        Returns
        -------
        list of int
            IDs of 4 wheeled vehicles on autopilot.
        list of DataCollector
            Data collectors with set up done and listening to LIDAR.
        """
        vehicle_ids = []
        data_collectors = []
        blueprints = get_all_vehicle_blueprints(self.world)
        spawn_points = self.carla_map.get_spawn_points()
        number_of_spawn_points = len(spawn_points)
        logging.info(f"Using {self.args.n_vehicles} out of "
                     f"{number_of_spawn_points} spawn points")

        if self.args.n_vehicles < number_of_spawn_points:
            np.random.shuffle(spawn_points)
        elif self.args.n_vehicles > number_of_spawn_points:
            msg = "requested %d vehicles, but could only find %d spawn points"
            logging.warning(msg, self.args.n_vehicles, number_of_spawn_points)
            self.args.n_vehicles = number_of_spawn_points

        # Generate vehicles
        batch = []
        for n, transform in enumerate(spawn_points):
            if n >= self.args.n_vehicles:
                break
            blueprint = np.random.choice(blueprints)
            if blueprint.has_attribute('color'):
                color = np.random.choice(
                    blueprint.get_attribute('color').recommended_values)
                blueprint.set_attribute('color', color)
            if blueprint.has_attribute('driver_id'):
                driver_id = np.random.choice(
                    blueprint.get_attribute('driver_id').recommended_values)
                blueprint.set_attribute('driver_id', driver_id)
            blueprint.set_attribute('role_name', 'autopilot')

            # prepare the light state of the cars to spawn
            light_state = vls.NONE
            if self.args.car_lights_on:
                light_state = vls.Position | vls.LowBeam | vls.LowBeam

            # spawn the cars and set their autopilot and light state all together
            batch.append(
                SpawnActor(blueprint, transform).then(
                    SetAutopilot(FutureActor, True,
                                 self.traffic_manager.get_port())).then(
                                     SetVehicleLightState(
                                         FutureActor, light_state)))

        # Wait for vehicles to finish generating
        for response in self.client.apply_batch_sync(batch, True):
            if response.error:
                logging.error(response.error)
            else:
                vehicle_ids.append(response.actor_id)

        # Add data collector to a handful of vehicles
        vehicles = self.world.get_actors(vehicle_ids)

        #>>>#########################################
        # In CARLA 0.9.13 must set this deliberately?
        self.traffic_manager.set_global_distance_to_leading_vehicle(5.0)
        self.traffic_manager.global_percentage_speed_difference(0.0)
        for vehicle in vehicles:
            self.traffic_manager.auto_lane_change(vehicle, True)
            self.traffic_manager.ignore_lights_percentage(vehicle, 0.0)
            self.traffic_manager.ignore_signs_percentage(vehicle, 0.0)
            self.traffic_manager.ignore_vehicles_percentage(vehicle, 0.0)
            self.traffic_manager.ignore_walkers_percentage(vehicle, 0.0)
        #<<<#########################################

        vehicles = dict(zip(vehicle_ids, vehicles))
        vehicles_ids_to_data_collect = vehicle_ids[:self.args.
                                                   n_data_collectors]

        for idx, vehicle_id in enumerate(vehicles_ids_to_data_collect):
            vehicle_ids_to_watch = vehicle_ids[:idx] + vehicle_ids[idx + 1:]
            vehicle = self.world.get_actor(vehicle_id)
            data_collector = DataCollector(
                vehicle,
                self.map_reader,
                vehicle_ids_to_watch,
                scene_builder_cls=TrajectronPlusPlusSceneBuilder,
                scene_config=self.scene_config,
                save_frequency=self.args.save_frequency,
                n_burn_frames=self.args.n_burn_frames,
                episode=episode,
                exclude_samples=self.exclude_filter,
                callback=self.add_scene,
                debug=self.args.debug)
            data_collector.start_sensor()
            data_collectors.append(data_collector)

        logging.info(f"Spawned {len(vehicle_ids)} vehicles")
        logging.info(f"Spawned {len(data_collectors)} data collectors")
        return vehicle_ids, data_collectors
def scenario(carla_synchronous, scenario_params):
    n_burn_frames, save_frequency, record_interval, scene_interval, \
            collector_spawn, npc_spawn, spawn_shifts = scenario_params
    client, world, carla_map, traffic_manager = carla_synchronous
    vehicles = []
    data_collectors = []
    env = Environment(node_type_list=['VEHICLE'],
                      standardization=standardization)
    scenes = []

    def add_scene(scene):
        scenes.append(scene)

    try:
        # Mock vehicles
        spawn_points = carla_map.get_spawn_points()
        blueprints = get_all_vehicle_blueprints(world)
        spawn_indices = collector_spawn + npc_spawn
        vehicle_ids = []
        for k, spawn_idx in enumerate(spawn_indices):
            blueprint = np.random.choice(blueprints)
            spawn_point = spawn_points[spawn_idx]
            spawn_point = shift_spawn_point(carla_map, k, spawn_shifts,
                                            spawn_point)
            # Prevent collision with road.
            spawn_point.location += carla.Location(0, 0, 0.5)
            # carlautil.debug_point(client, spawn_point.location, t=5.0)
            vehicle = world.spawn_actor(blueprint, spawn_point)
            vehicle.set_autopilot(True, traffic_manager.get_port())
            vehicles.append(vehicle)
            vehicle_ids.append(vehicle.id)

        # Setup data collector
        map_reader = NaiveMapQuerier(world, carla_map, debug=True)
        scene_config = SceneConfig(scene_interval=scene_interval,
                                   record_interval=record_interval,
                                   node_type=env.NodeType)
        n_data_collector = len(collector_spawn)
        for collector_idx in range(n_data_collector):
            other_vehicle_ids = vehicle_ids[:collector_idx] + vehicle_ids[
                collector_idx + 1:]
            data_collector = DataCollector(vehicles[collector_idx],
                                           map_reader,
                                           other_vehicle_ids,
                                           scene_builder_cls=TESTSceneBuilder,
                                           scene_config=scene_config,
                                           save_frequency=save_frequency,
                                           n_burn_frames=n_burn_frames,
                                           callback=add_scene,
                                           debug=True)
            data_collector.start_sensor()
            assert data_collector.sensor_is_listening
            data_collectors.append(data_collector)

        # Run simulation for X steps
        for idx in range(n_burn_frames + record_interval *
                         (save_frequency - 1)):
            frame = world.tick()
            for data_collector in data_collectors:
                data_collector.capture_step(frame)

        assert len(scenes) == n_data_collector
        for scene in scenes:
            plot_trajectron_scene('out', scene)

    finally:
        for data_collector in data_collectors:
            data_collector.destroy()
        for vehicle in vehicles:
            vehicle.destroy()
Exemple #6
0
def test_2_data_collectors(carla_Town03_synchronous):
    """Test case where NPCs move away from the EGO vehicle and disappear.
    """
    client, world, carla_map, traffic_manager = carla_Town03_synchronous
    vehicles = []
    data_collectors = []
    env = Environment(node_type_list=['VEHICLE'],
                      standardization=standardization)
    scenes = []

    def add_scene(scene):
        scenes.append(scene)

    n_burn_frames = 40
    save_frequency = 40
    record_interval = 5
    scene_interval = 30
    try:
        # Mock vehicles
        spawn_points = carla_map.get_spawn_points()
        vehicle_ids = []
        blueprints = get_all_vehicle_blueprints(world)
        for idx in [7, 187, 122, 221]:
            blueprint = np.random.choice(blueprints)
            spawn_point = spawn_points[idx]
            vehicle = world.spawn_actor(blueprint, spawn_point)
            vehicle.set_autopilot(True, traffic_manager.get_port())
            vehicles.append(vehicle)
            vehicle_ids.append(vehicle.id)

        # Setup data collector
        map_reader = NaiveMapQuerier(world, carla_map, debug=True)
        scene_config = SceneConfig(scene_interval=scene_interval,
                                   record_interval=record_interval)
        data_collector = DataCollector(vehicles[0],
                                       map_reader,
                                       vehicle_ids[1:],
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       callback=add_scene,
                                       debug=True)
        data_collector.start_sensor()
        data_collectors.append(data_collector)
        data_collector = DataCollector(vehicles[1],
                                       map_reader,
                                       vehicle_ids[:1] + vehicle_ids[2:],
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       callback=add_scene,
                                       debug=True)
        data_collector.start_sensor()
        data_collectors.append(data_collector)

        # Run simulation for X steps
        for idx in range(n_burn_frames + record_interval *
                         (save_frequency - 1)):
            frame = world.tick()
            for data_collector in data_collectors:
                data_collector.capture_step(frame)

        assert len(scenes) == 2
        for scene in scenes:
            fig, ax = plt.subplots(figsize=(15, 15))
            # extent = (scene.x_min, scene.x_max, scene.y_min, scene.y_max)
            ax.imshow(scene.map['VEHICLE'].as_image(), origin='lower')
            spectral = cm.nipy_spectral(np.linspace(0, 1, len(scene.nodes)))
            for idx, node in enumerate(scene.nodes):
                # using values from scene.map['VEHICLE'].homography
                # to scale points
                xy = 3 * node.data.data[:, :2]
                ax.scatter(xy[:, 0], xy[:, 1], color=spectral[idx])
            ax.set_xlabel('x')
            ax.set_ylabel('y')
            ax.set_aspect('equal')
            fig.tight_layout()
            fn = f"{ scene.name.replace('/', '_') }.png"
            fp = os.path.join('out', fn)
            fig.savefig(fp)

    finally:
        for data_collector in data_collectors:
            data_collector.destroy()
        for vehicle in vehicles:
            vehicle.destroy()
Exemple #7
0
def test_1_npc_appear(carla_Town03_synchronous):
    """Test case where NPC appears before EGO vehicle after awhile.
    """
    client, world, carla_map, traffic_manager = carla_Town03_synchronous
    ego_vehicle = None
    other_vehicles = []
    data_collector = None
    env = Environment(node_type_list=['VEHICLE'],
                      standardization=standardization)
    scenes = []

    def add_scene(scene):
        scenes.append(scene)

    n_burn_frames = 160
    save_frequency = 40
    record_interval = 5
    scene_interval = 30
    try:
        # Mock vehicles
        spawn_points = carla_map.get_spawn_points()
        blueprint = world.get_blueprint_library().find('vehicle.audi.a2')
        spawn_point = spawn_points[251]
        ego_vehicle = world.spawn_actor(blueprint, spawn_point)
        ego_vehicle.set_autopilot(True, traffic_manager.get_port())

        other_vehicle_ids = []
        blueprints = get_all_vehicle_blueprints(world)
        for idx in [164]:
            blueprint = np.random.choice(blueprints)
            spawn_point = spawn_points[idx]
            other_vehicle = world.spawn_actor(blueprint, spawn_point)
            other_vehicle.set_autopilot(True, traffic_manager.get_port())
            other_vehicles.append(other_vehicle)
            other_vehicle_ids.append(other_vehicle.id)

        # Setup data collector
        map_reader = NaiveMapQuerier(world, carla_map, debug=True)
        scene_config = SceneConfig(scene_interval=scene_interval,
                                   record_interval=record_interval)
        data_collector = DataCollector(ego_vehicle,
                                       map_reader,
                                       other_vehicle_ids,
                                       scene_builder_cls=TESTSceneBuilder,
                                       scene_config=scene_config,
                                       save_frequency=save_frequency,
                                       n_burn_frames=n_burn_frames,
                                       callback=add_scene,
                                       debug=True)
        data_collector.start_sensor()
        assert data_collector.sensor_is_listening

        # Run simulation for X steps
        for idx in range(n_burn_frames + record_interval *
                         (save_frequency - 1)):
            frame = world.tick()
            data_collector.capture_step(frame)

        assert len(scenes) == 1
        scene = scenes[0]
        fig, ax = plt.subplots(figsize=(15, 15))
        # extent = (scene.x_min, scene.x_max, scene.y_min, scene.y_max)
        ax.imshow(scene.map['VEHICLE'].as_image(), origin='lower')
        spectral = cm.nipy_spectral(np.linspace(0, 1, len(scene.nodes)))
        for idx, node in enumerate(scene.nodes):
            # using values from scene.map['VEHICLE'].homography
            # to scale points
            xy = 3 * node.data.data[:, :2]
            ax.scatter(xy[:, 0], xy[:, 1], color=spectral[idx])
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_aspect('equal')
        fig.tight_layout()
        fn = f"{ scene.name.replace('/', '_') }.png"
        fp = os.path.join('out', fn)
        fig.savefig(fp)

    finally:
        if data_collector:
            data_collector.destroy()
        if ego_vehicle:
            ego_vehicle.destroy()
        for other_vehicle in other_vehicles:
            other_vehicle.destroy()