def __init__(self, name, lidar_setup, flags, log_file_name=None):
        """ Initializes the Lidar inside the simulation with the given
        parameters.

        Args:
            name: The unique name of the operator.
            lidar_setup: A LidarSetup tuple..
            flags: A handle to the global flags instance to retrieve the
                configuration.
            log_file_name: The file to log the required information to.
        """
        super(LidarDriverOperator,
              self).__init__(name, no_watermark_passthrough=True)
        # The operator does not pass watermarks by defaults.
        self._flags = flags
        self._logger = setup_logging(self.name, log_file_name)
        self._lidar_setup = lidar_setup

        _, self._world = get_world(self._flags.carla_host,
                                   self._flags.carla_port,
                                   self._flags.carla_timeout)
        if self._world is None:
            raise ValueError("There was an issue connecting to the simulator.")

        # The hero vehicle actor object we obtain from Carla.
        self._vehicle = None
        # Handle to the Lidar Carla actor.
        self._lidar = None
        self._lock = threading.Lock()
Пример #2
0
def main(argv):
    client, world = get_world(FLAGS.carla_host,
                              FLAGS.carla_port,
                              FLAGS.carla_timeout)

    # Replayer time factor is only available in > 0.9.5.
    client.set_replayer_time_factor(0.1)
    print(client.replay_file(FLAGS.carla_replay_file,
                             FLAGS.carla_replay_start_time,
                             FLAGS.carla_replay_duration,
                             FLAGS.carla_replay_id))
    # Sleep a bit to allow the server to start the replay.
    time.sleep(1)
    vehicle = world.get_actors().find(FLAGS.carla_replay_id)
    if vehicle is None:
        raise ValueError("There was an issue finding the vehicle.")

    # Install the camera.
    camera_blueprint = world.get_blueprint_library().find('sensor.camera.rgb')
    camera_blueprint.set_attribute('image_size_x',
                                   str(FLAGS.carla_camera_image_width))
    camera_blueprint.set_attribute('image_size_y',
                                   str(FLAGS.carla_camera_image_height))

    transform = carla.Transform(carla.Location(2.0, 0.0, 1.4),
                                carla.Rotation(pitch=0, yaw=0, roll=0))

    camera = world.spawn_actor(camera_blueprint,
                               transform,
                               attach_to=vehicle)

    # Register the callback on the camera.
    camera.listen(process_images)

    time.sleep(20)
Пример #3
0
def setup_world():
    # Connect to the Carla simulator.
    client, world = get_world()
    settings = world.get_settings()
    settings.synchronous_mode = True
    world.apply_settings(settings)
    return world
Пример #4
0
    def execute(self):
        # Connect to CARLA and retrieve the world running.
        self._client, self._world = get_world(self._flags.carla_host,
                                              self._flags.carla_port,
                                              self._flags.carla_timeout)
        if self._client is None or self._world is None:
            raise ValueError('There was an issue connecting to the simulator.')

        # Replayer time factor is only available in > 0.9.5.
        # self._client.set_replayer_time_factor(0.1)
        print(
            self._client.replay_file(self._flags.carla_replay_file,
                                     self._flags.carla_replay_start_time,
                                     self._flags.carla_replay_duration,
                                     self._flags.carla_replay_id))
        # Sleep a bit to allow the server to start the replay.
        time.sleep(1)
        self._driving_vehicle = self._world.get_actors().find(
            self._flags.carla_replay_id)
        if self._driving_vehicle is None:
            raise ValueError("There was an issue finding the vehicle.")
        # TODO(ionel): We do not currently have a top message.
        timestamp = Timestamp(coordinates=[sys.maxint])
        vehicle_id_msg = Message(self._driving_vehicle.id, timestamp)
        self.get_output_stream('vehicle_id_stream').send(vehicle_id_msg)
        self.get_output_stream('vehicle_id_stream').send(
            WatermarkMessage(timestamp))
        self._world.on_tick(self.on_world_tick)
        self.spin()
Пример #5
0
def setup_world():
    # Connect to the Carla simulator.
    client, world = get_world()
    settings = world.get_settings()
    settings.synchronous_mode = True
    settings.fixed_delta_seconds = 0.05
    world.apply_settings(settings)
    return world
Пример #6
0
def log_bounding_boxes(carla_image, depth_frame, segmented_frame,
                       traffic_lights, tl_color, speed_signs, stop_signs,
                       weather, town):
    game_time = int(carla_image.timestamp * 1000)
    print("Processing game time {} in {} with weather {}".format(
        game_time, town, weather))
    frame = bgra_to_bgr(to_bgra_array(carla_image))
    # Copy the frame to ensure its on the heap.
    frame = copy.deepcopy(frame)
    transform = to_pylot_transform(carla_image.transform)
    _, world = get_world()
    town_name = world.get_map().name

    speed_limit_det_objs = []
    if speed_signs:
        speed_limit_det_objs = pylot.simulation.utils.get_speed_limit_det_objs(
            speed_signs, transform, transform, depth_frame, FLAGS.frame_width,
            FLAGS.frame_height, FLAGS.camera_fov, segmented_frame)

    traffic_stop_det_objs = []
    if stop_signs:
        traffic_stop_det_objs = pylot.simulation.utils.get_traffic_stop_det_objs(
            stop_signs, transform, depth_frame, FLAGS.frame_width,
            FLAGS.frame_height, FLAGS.camera_fov)

    traffic_light_det_objs = []
    if traffic_lights:
        traffic_light_det_objs = get_traffic_light_objs(
            traffic_lights, transform, depth_frame, segmented_frame,
            FLAGS.frame_width, FLAGS.frame_height, tl_color, town_name)

    det_objs = (speed_limit_det_objs + traffic_stop_det_objs +
                traffic_light_det_objs)

    if FLAGS.visualize_bboxes:
        visualize_ground_bboxes('bboxes', game_time, frame, det_objs)

    # Log the frame.
    rgb_frame = bgr_to_rgb(frame)
    file_name = '{}signs-{}_{}_{}.png'.format(FLAGS.data_path, game_time,
                                              weather, town)
    rgb_img = Image.fromarray(np.uint8(rgb_frame))
    rgb_img.save(file_name)

    if FLAGS.log_bbox_images:
        annotate_image_with_bboxes(game_time, frame, det_objs)
        rgb_frame = bgr_to_rgb(frame)
        file_name = '{}annotated-signs-{}_{}_{}.png'.format(
            FLAGS.data_path, game_time, weather, town)
        rgb_img = Image.fromarray(np.uint8(rgb_frame))
        rgb_img.save(file_name)

    # Log the bounding boxes.
    bboxes = [det_obj.get_bbox_label() for det_obj in det_objs]
    file_name = '{}bboxes-{}_{}_{}.json'.format(FLAGS.data_path, game_time,
                                                weather, town)
    with open(file_name, 'w') as outfile:
        json.dump(bboxes, outfile)
Пример #7
0
    def on_vehicle_id(self, msg):
        """ This function receives the identifier for the vehicle, retrieves
        the handler for the vehicle from the simulation and attaches the
        camera to it.

        Args:
            msg: The identifier for the vehicle to attach the camera to.
        """
        vehicle_id = msg.data
        self._logger.info(
            "The CameraDriverOperator received the vehicle id: {}".format(
                vehicle_id))

        # Connect to the world. We connect here instead of in the constructor
        # to ensure we're connected to the latest world.
        _, world = get_world(self._flags.carla_host, self._flags.carla_port,
                             self._flags.carla_timeout)
        if world is None:
            raise ValueError("There was an issue connecting to the simulator.")
        if self._flags.carla_synchronous_mode:
            set_synchronous_mode(world, self._flags.carla_fps)

        num_tries = 0
        while self._vehicle is None and num_tries < 30:
            self._vehicle = world.get_actors().find(vehicle_id)
            self._logger.info(
                "Could not find vehicle. Try {}".format(num_tries))
            time.sleep(1)
            num_tries += 1
        if self._vehicle is None:
            raise ValueError("There was an issue finding the vehicle.")

        # Install the camera.
        camera_blueprint = world.get_blueprint_library().find(
            self._camera_setup.camera_type)

        camera_blueprint.set_attribute('image_size_x',
                                       str(self._camera_setup.width))
        camera_blueprint.set_attribute('image_size_y',
                                       str(self._camera_setup.height))
        camera_blueprint.set_attribute('fov', str(self._camera_setup.fov))

        transform = to_carla_transform(self._camera_setup.get_transform())

        self._logger.info("Spawning a camera: {}".format(self._camera_setup))

        self._camera = world.spawn_actor(camera_blueprint,
                                         transform,
                                         attach_to=self._vehicle)

        # Register the callback on the camera.
        self._camera.listen(self.process_images)
Пример #8
0
    def __init__(self, name, flags, log_file_name=None, csv_file_name=None):
        super(CarlaReplayOperator, self).__init__(name)
        self._flags = flags
        self._logger = setup_logging(self.name, log_file_name)
        self._csv_logger = setup_csv_logging(self.name + '-csv', csv_file_name)

        # Connect to CARLA and retrieve the world running.
        self._client, self._world = get_world(self._flags.carla_host,
                                              self._flags.carla_port,
                                              self._flags.carla_timeout)
        if self._client is None or self._world is None:
            raise ValueError('There was an issue connecting to the simulator.')

        # Lock to ensure that the callbacks do not execute simultaneously.
        self._lock = threading.Lock()
Пример #9
0
def setup_world(host, port, delta):
    """ Connects to the simulator at the given host:port and sets the mode to
    synchronous.

    Args:
        host: The host where the simulator is running.
        port: The port to connect to at the given host.
        delta: The delta at which to run the simulation.

    Returns:
        The instance of `world` that was returned from the simulator.
    """
    _, world = get_world(host, port)
    if world is None:
        print("Could not connect to the simulator at {}:{}".format(host, port),
              file=sys.stderr)
        sys.exit(1)

    # Turn on synchronous mode.
    set_synchronous_mode(world, True, delta)

    return world
Пример #10
0
    def __init__(self,
                 name,
                 output_stream_name,
                 bgr_camera_setup,
                 flags,
                 log_file_name=None,
                 csv_file_name=None):
        """ Initializes the operator.

        Args:
            bgr_camera_setup: A simulation.utils.CameraSetup object
        """
        super(PerfectDetectorOp, self).__init__(name)
        self._logger = setup_logging(self.name, log_file_name)
        self._csv_logger = setup_csv_logging(self.name + '-csv', csv_file_name)
        self._flags = flags
        self._output_stream_name = output_stream_name
        _, world = get_world(self._flags.carla_host,
                             self._flags.carla_port,
                             self._flags.carla_timeout)
        if world is None:
            raise ValueError("There was an issue connecting to the simulator.")
        self._town_name = world.get_map().name
        # Queues of incoming data.
        self._bgr_imgs = deque()
        self._can_bus_msgs = deque()
        self._depth_imgs = deque()
        self._pedestrians = deque()
        self._segmented_imgs = deque()
        self._traffic_lights = deque()
        self._vehicles = deque()
        self._speed_limit_signs = deque()
        self._stop_signs = deque()
        self._bgr_intrinsic = bgr_camera_setup.get_intrinsic()
        self._bgr_transform = bgr_camera_setup.get_unreal_transform()
        self._bgr_img_size = (bgr_camera_setup.width, bgr_camera_setup.height)
        self._lock = threading.Lock()
        self._frame_cnt = 0
Пример #11
0
 def execute(self):
     """ Retrieve the world instance from the simulator. """
     _, world = get_world(self._flags.carla_host, self._flags.carla_port,
                          self._flags.carla_timeout)
     self._world_map = world.get_map()
     self.spin()
Пример #12
0
    def __init__(self,
                 name,
                 auto_pilot,
                 flags,
                 log_file_name=None,
                 csv_file_name=None):
        """ Initializes the CarlaOperator with the given name.

        Args:
            name: The unique name of the operator.
            auto_pilot: True to enable auto_pilot for ego vehicle.
            flags: A handle to the global flags instance to retrieve the
                configuration.
            log_file_name: The file to log the required information to.
            csv_file_name: The file to log info to in csv format.
        """
        super(CarlaOperator, self).__init__(name)
        self._flags = flags
        self._logger = setup_logging(self.name, log_file_name)
        self._csv_logger = setup_csv_logging(self.name + '-csv', csv_file_name)
        self._auto_pilot = auto_pilot
        # Connect to CARLA and retrieve the world running.
        self._client, self._world = get_world(self._flags.carla_host,
                                              self._flags.carla_port,
                                              self._flags.carla_timeout)
        if self._client is None or self._world is None:
            raise ValueError('There was an issue connecting to the simulator.')

        if self._flags.carla_version == '0.9.6':
            self._world = self._client.load_world(
                'Town{:02d}'.format(self._flags.carla_town))
        else:
            # TODO (Sukrit) :: ERDOS provides no way to retrieve handles to the
            # class objects to do garbage collection. Hence, objects from
            # previous runs of the simulation may persist. We need to clean
            # them up right now. In future, move this logic to a seperate
            # destroy function.
            reset_world(self._world)

        # Set the weather.
        weather, name = get_weathers()[self._flags.carla_weather - 1]
        self._logger.info('Setting the weather to {}'.format(name))
        self._world.set_weather(weather)
        # Turn on the synchronous mode so we can control the simulation.
        if self._flags.carla_synchronous_mode:
            set_synchronous_mode(self._world, self._flags.carla_fps)

        # Spawn the required number of vehicles.
        self._vehicles = self._spawn_vehicles(self._flags.carla_num_vehicles)

        # Spawn the vehicle that the pipeline has to drive and send it to
        # the downstream operators.
        self._driving_vehicle = self._spawn_driving_vehicle()

        if self._flags.carla_version == '0.9.6':
            # Pedestrians are do not move in versions older than 0.9.6.
            (self._pedestrians, ped_control_ids) = self._spawn_pedestrians(
                self._flags.carla_num_pedestrians)

        # Tick once to ensure that the actors are spawned before the data-flow
        # starts.
        self._tick_at = time.time()
        self._tick_simulator()

        # Start pedestrians
        if self._flags.carla_version == '0.9.6':
            self._start_pedestrians(ped_control_ids)
Пример #13
0
    def on_vehicle_id(self, msg):
        """ This function receives the identifier for the vehicle, retrieves
        the handler for the vehicle from the simulation and attaches the
        camera to it.

        Args:
            msg: The identifier for the vehicle to attach the camera to.
        """
        vehicle_id = msg.data
        self._logger.info(
            "The LidarDriverOperator received the vehicle id: {}".format(
                vehicle_id))

        # Connect to the world. We connect here instead of in the constructor
        # to ensure we're connected to the latest world.
        _, world = get_world(self._flags.carla_host,
                             self._flags.carla_port,
                             self._flags.carla_timeout)
        if world is None:
            raise ValueError("There was an issue connecting to the simulator.")
        if self._flags.carla_synchronous_mode:
            set_synchronous_mode(world, self._flags.carla_fps)

        num_tries = 0
        while self._vehicle is None and num_tries < 30:
            self._vehicle = world.get_actors().find(vehicle_id)
            self._logger.info("Could not find vehicle. Try {}".format(num_tries))
            time.sleep(1)
            num_tries += 1
        if self._vehicle is None:
            raise ValueError("There was an issue finding the vehicle.")

        # Install the Lidar.
        lidar_blueprint = world.get_blueprint_library().find(
            self._lidar_setup.lidar_type)

        lidar_blueprint.set_attribute('channels',
                                      str(self._lidar_setup.channels))
        lidar_blueprint.set_attribute('range',
                                      str(self._lidar_setup.range))
        lidar_blueprint.set_attribute('points_per_second',
                                      str(self._lidar_setup.points_per_second))
        lidar_blueprint.set_attribute(
            'rotation_frequency',
            str(self._lidar_setup.rotation_frequency))
        lidar_blueprint.set_attribute('upper_fov',
                                      str(self._lidar_setup.upper_fov))
        lidar_blueprint.set_attribute('lower_fov',
                                      str(self._lidar_setup.lower_fov))
        # XXX(ionel): Set sensor tick.
        # lidar_blueprint.set_attribute('sensor_tick')

        transform = to_carla_transform(self._lidar_setup.get_transform())

        self._logger.info("Spawning a lidar: {}".format(self._lidar_setup))

        self._lidar = world.spawn_actor(lidar_blueprint,
                                        transform,
                                        attach_to=self._vehicle)
        # Register the callback on the Lidar.
        self._lidar.listen(self.process_point_clouds)
Пример #14
0
    camera_blueprint.set_attribute('image_size_y', '600')
    camera = world.spawn_actor(camera_blueprint, transform)
    # Register callback to be invoked when a new frame is received.
    camera.listen(callback)
    return camera


def add_vehicle(world, transform):
    # Location of the vehicle in world coordinates.
    v_blueprint = world.get_blueprint_library().find('vehicle.audi.a2')
    vehicle = world.spawn_actor(v_blueprint, transform)
    return vehicle


# Connect to the Carla simulator.
client, world = get_world()
settings = world.get_settings()
settings.synchronous_mode = True
world.apply_settings(settings)

print("Adding sensors")
target_vehicle = add_vehicle(world, target_vehicle_transform)
lidar = add_lidar(world, sensor_transform, on_lidar_msg)
depth_camera = add_depth_camera(world, sensor_transform, on_depth_msg)
camera = add_camera(world, sensor_transform, on_camera_msg)

# Move the spectactor view to the camera position.
world.get_spectator().set_transform(sensor_transform)

try:
    # Tick the simulator once to get 1 data reading.