def run(self): settings = CarlaSettings() settings.add_camera(Camera('DefaultCamera')) camera2 = Camera('Camera2') camera2.set(PostProcessing='Depth', CameraFOV=120) camera2.set_image_size(1924, 1028) settings.add_camera(camera2) self.run_carla_client(settings, 3, 100)
def get_default_carla_settings(args): settings = CarlaSettings( SynchronousMode=args.synchronous, SendNonPlayerAgentsInfo=False, NumberOfVehicles=20, NumberOfPedestrians=40, WeatherId=1) settings.add_sensor(Camera('Camera1')) return str(settings)
def build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depends on the selected Town. """ # We check the town, based on that we define the town related parameters # The size of the vector is related to the number of tasks, inside each # task there is also multiple poses ( start end, positions ) if self._city_name == 'Town01': poses_tasks = [[[7, 3]], [[138, 17]], [[140, 134]], [[140, 134]]] vehicles_tasks = [0, 0, 0, 20] pedestrians_tasks = [0, 0, 0, 50] else: poses_tasks = [[[4, 2]], [[37, 76]], [[19, 66]], [[19, 66]]] vehicles_tasks = [0, 0, 0, 15] pedestrians_tasks = [0, 0, 0, 50] # We set the camera # This single RGB camera is used on every experiment camera = Camera('CameraRGB') camera.set(FOV=100) camera.set_image_size(800, 600) camera.set_position(2.0, 0.0, 1.4) camera.set_rotation(-15.0, 0, 0) # Based on the parameters, creates a vector with experiment objects. experiments_vector = [] for weather in self.weathers: for iteration in range(len(poses_tasks)): poses = poses_tasks[iteration] vehicles = vehicles_tasks[iteration] pedestrians = pedestrians_tasks[iteration] conditions = CarlaSettings() conditions.set( SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather ) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set( Conditions=conditions, Poses=poses, Task=iteration, Repetitions=1 ) experiments_vector.append(experiment) return experiments_vector
def run(self): settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=60, NumberOfPedestrians=90) settings.add_camera(Camera('DefaultCamera')) self.run_carla_client(settings, 3, 100)
def build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depend on the selected Town. """ # We set the camera # This single RGB camera is used on every experiment camera = Camera('CameraRGB') camera.set(FOV=100) camera.set_image_size(800, 600) camera.set_position(2.0, 0.0, 1.4) camera.set_rotation(-15.0, 0, 0) if self._city_name == 'Town01': poses_tasks = self._poses_town01() vehicles_tasks = [0, 0, 0, 20] pedestrians_tasks = [0, 0, 0, 50] else: poses_tasks = self._poses_town02() vehicles_tasks = [0, 0, 0, 15] pedestrians_tasks = [0, 0, 0, 50] experiments_vector = [] for weather in self.weathers: for iteration in range(len(poses_tasks)): poses = poses_tasks[iteration] vehicles = vehicles_tasks[iteration] pedestrians = pedestrians_tasks[iteration] conditions = CarlaSettings() conditions.set( SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather ) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set( Conditions=conditions, Poses=poses, Task=iteration, Repetitions=1 ) experiments_vector.append(experiment) return experiments_vector
def _reset(self): self.num_steps = 0 self.total_reward = 0 self.prev_measurement = None self.prev_image = None self.episode_id = datetime.today().strftime("%Y-%m-%d_%H-%M-%S_%f") self.measurements_file = None # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() self.scenario = random.choice(self.config["scenarios"]) assert self.scenario["city"] == self.city, (self.scenario, self.city) self.weather = random.choice(self.scenario["weather_distribution"]) settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=self.scenario["num_vehicles"], NumberOfPedestrians=self.scenario["num_pedestrians"], WeatherId=self.weather) settings.randomize_seeds() if self.config["use_depth_camera"]: camera1 = Camera("CameraDepth", PostProcessing="Depth") camera1.set_image_size(self.config["render_x_res"], self.config["render_y_res"]) camera1.set_position(30, 0, 130) settings.add_sensor(camera1) camera2 = Camera("CameraRGB") camera2.set_image_size(self.config["render_x_res"], self.config["render_y_res"]) camera2.set_position(30, 0, 130) settings.add_sensor(camera2) # Setup start and end positions scene = self.client.load_settings(settings) positions = scene.player_start_spots self.start_pos = positions[self.scenario["start_pos_id"]] self.end_pos = positions[self.scenario["end_pos_id"]] self.start_coord = [ self.start_pos.location.x // 100, self.start_pos.location.y // 100 ] self.end_coord = [ self.end_pos.location.x // 100, self.end_pos.location.y // 100 ] print("Start pos {} ({}), end {} ({})".format( self.scenario["start_pos_id"], self.start_coord, self.scenario["end_pos_id"], self.end_coord)) # Notify the server that we want to start the episode at the # player_start index. This function blocks until the server is ready # to start the episode. print("Starting new episode...") self.client.start_episode(self.scenario["start_pos_id"]) image, py_measurements = self._read_observation() self.prev_measurement = py_measurements return self.encode_obs(self.preprocess_image(image), py_measurements)
def run_carla_client(args): with make_connection(CarlaClient, args.host, args.port, timeout=15) as client: logging.info('CarlaClient connected') filename = '_images/episode_{:0>3d}/image_{:0>5d}.png' frames_per_episode = 300 episode = 0 while True: episode += 1 settings = CarlaSettings() settings.set(SendNonPlayerAgentsInfo=True,SynchronousMode=args.synchronous) settings.randomize_seeds() camera = Camera('DefaultCamera') camera.set_image_size(300, 200) # Do not change this, hard-coded in test. settings.add_camera(camera) logging.debug('sending CarlaSettings:\n%s', settings) logging.info('new episode requested') scene = client.request_new_episode(settings) number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) logging.info( 'start episode at %d/%d player start (%d frames)', player_start, number_of_player_starts, frames_per_episode) client.start_episode(player_start) use_ai_control = (random.random() < 0.5) reverse = (random.random() < 0.2) for frame in range(0, frames_per_episode): logging.debug('reading measurements...') measurements, images = client.read_measurements() logging.debug('received data of %d agents', len(measurements.non_player_agents)) assert len(images) == 1 assert (images[0].width, images[0].height) == (camera.ImageSizeX, camera.ImageSizeY) if args.images_to_disk: images[0].save_to_disk(filename.format(episode, frame)) logging.debug('sending control...') control = measurements.player_measurements.ai_control if not use_ai_control: control.steer = random.uniform(-1.0, 1.0) control.throttle = 0.3 control.hand_brake = False control.reverse = reverse client.send_control( steer=control.steer, throttle=control.throttle, brake=control.brake, hand_brake=control.hand_brake, reverse=control.reverse)
def build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depend on the selected Town. """ # We set the camera # This single RGB camera is used on every experiment camera = Camera('rgb') camera.set(FOV=100) camera.set_image_size(800, 600) camera.set_position(2.0, 0.0, 1.4) camera.set_rotation(-15.0, 0, 0) poses_tasks = self._poses() vehicles_tasks = [0, 20, 100] pedestrians_tasks = [0, 50, 250] task_names = ['empty', 'normal', 'cluttered'] experiments_vector = [] for weather in self.weathers: for iteration in range(len(poses_tasks)): poses = poses_tasks[iteration] vehicles = vehicles_tasks[iteration] pedestrians = pedestrians_tasks[iteration] conditions = CarlaSettings() conditions.set(SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather) conditions.set(DisableTwoWheeledVehicles=True) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set(Conditions=conditions, Poses=poses, Task=iteration, TaskName=task_names[iteration], Repetitions=1) experiments_vector.append(experiment) return experiments_vector
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need. """ settings = CarlaSettings() # There is no need for non-agent info requests if there are no pedestrians # or vehicles. get_non_player_agents_info = False if (NUM_PEDESTRIANS > 0 or NUM_VEHICLES > 0): get_non_player_agents_info = True # Base level settings settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=get_non_player_agents_info, NumberOfVehicles=NUM_VEHICLES, NumberOfPedestrians=NUM_PEDESTRIANS, SeedVehicles=SEED_VEHICLES, SeedPedestrians=SEED_PEDESTRIANS, WeatherId=SIMWEATHER, QualityLevel=args.quality_level) return settings
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need.""" settings = CarlaSettings() settings.set( SynchronousMode=False, SendNonPlayerAgentsInfo=True, NumberOfVehicles=15, NumberOfPedestrians=30, WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel=args.quality_level) settings.randomize_seeds() camera0 = sensor.Camera('CameraRGB') camera0.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT) camera0.set_position(2.0, 0.0, 1.4) camera0.set_rotation(0.0, 0.0, 0.0) settings.add_sensor(camera0) camera2 = sensor.Camera('CameraSemSeg', PostProcessing='SemanticSegmentation') camera2.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT) camera2.set_position(2.0, 0.0, 1.4) camera2.set_rotation(0.0, 0.0, 0.0) settings.add_sensor(camera2) return settings
def run(self): settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=60, NumberOfPedestrians=90) settings.add_sensor(Camera('DefaultCamera')) self.run_carla_client(settings, 3, 100)
def view_start_positions(args): # We assume the CARLA server is already waiting for a client to connect at # host:port. The same way as in the client example. with make_carla_client(args.host, args.port) as client: print('CarlaClient connected') # We load the default settings to the client. scene = client.load_settings(CarlaSettings()) print("Received the start positions") # We get the number of player starts, in order to detect the city. number_of_player_starts = len(scene.player_start_spots) if number_of_player_starts > 100: # WARNING: unsafe way to check for city, see issue #313 image = mpimg.imread("carla/planner/Town01.png") carla_map = CarlaMap('Town01', 16.53, 50) else: image = mpimg.imread("carla/planner/Town02.png") carla_map = CarlaMap('Town02', 16.53, 50) _, ax = plt.subplots(1) ax.imshow(image) if args.positions == 'all': positions_to_plot = range(len(scene.player_start_spots)) else: positions_to_plot = map(int, args.positions.split(',')) for position in positions_to_plot: # Check if position is valid if position >= len(scene.player_start_spots): raise RuntimeError('Selected position is invalid') # Convert world to pixel coordinates pixel = carla_map.convert_to_pixel([ scene.player_start_spots[position].location.x, scene.player_start_spots[position].location.y, scene.player_start_spots[position].location.z ]) circle = Circle((pixel[0], pixel[1]), 12, color='r', label='A point') ax.add_patch(circle) if not args.no_labels: plt.text(pixel[0], pixel[1], str(position), size='x-small') plt.show()
def run(self): settings = CarlaSettings() settings.add_sensor(Camera('DefaultCamera')) camera2 = Camera('Camera2') camera2.set(PostProcessing='Depth', CameraFOV=120) camera2.set_image_size(1924, 1028) settings.add_sensor(camera2) self.run_carla_client(settings, 3, 100)
def build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depends on the selected Town. """ # We check the town, based on that we define the town related parameters # The size of the vector is related to the number of tasks, inside each # task there is also multiple poses ( start end, positions ) if self._city_name == 'Town01': poses_tasks = [[[7, 3]], [[138, 17]], [[140, 134]], [[140, 134]]] vehicles_tasks = [0, 0, 0, 20] pedestrians_tasks = [0, 0, 0, 50] else: poses_tasks = [[[4, 2]], [[37, 76]], [[19, 66]], [[19, 66]]] vehicles_tasks = [0, 0, 0, 15] pedestrians_tasks = [0, 0, 0, 50] # We set the camera # This single RGB camera is used on every experiment camera = Camera('CameraRGB') camera.set(FOV=100) camera.set_image_size(800, 600) camera.set_position(2.0, 0.0, 1.4) camera.set_rotation(-15.0, 0, 0) # Based on the parameters, creates a vector with experiment objects. experiments_vector = [] for weather in self.weathers: for iteration in range(len(poses_tasks)): poses = poses_tasks[iteration] vehicles = vehicles_tasks[iteration] pedestrians = pedestrians_tasks[iteration] conditions = CarlaSettings() conditions.set( SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather ) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set( Conditions=conditions, Poses=poses, Task=iteration, Repetitions=1 ) experiments_vector.append(experiment) return experiments_vector
def default_settings(): settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=0, NumberOfPedestrians=0, WeatherId=1, # random.choice([1, 3, 7, 8, 14]), PlayerVehicle='/Game/Blueprints/Vehicles/Mustang/Mustang.Mustang_C', QualityLevel='Epic') settings.randomize_seeds() camera_RGB = Camera('CameraRGB') camera_RGB.set_image_size(256, 256) camera_RGB.set_position(1, 0, 2.50) settings.add_sensor(camera_RGB) camera_seg = Camera('CameraSegmentation', PostProcessing='SemanticSegmentation') camera_seg.set_image_size(256, 256) camera_seg.set_position(1, 0, 2.50) settings.add_sensor(camera_seg) return settings
def reset(self): """ :return: """ settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=20, NumberOfPedestrians=40, WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel='Epic') # CAMERA camera0 = Camera('CameraRGB', PostProcessing='SceneFinal') # Set image resolution in pixels. camera0.set_image_size(800, 600) # Set its position relative to the car in meters. camera0.set_position(0.30, 0, 1.30) settings.add_sensor(camera0) # Let's add another camera producing ground-truth depth. # camera1 = Camera('CameraDepth', PostProcessing='Depth') # camera1.set_image_size(800, 600) # camera1.set_position(0.30, 0, 1.30) # settings.add_sensor(camera1) # LIDAR # lidar = Lidar('Lidar32') # lidar.set_position(0, 0, 2.50) # lidar.set_rotation(0, 0, 0) # lidar.set( # Channels=32, # Range=50, # PointsPerSecond=100000, # RotationFrequency=10, # UpperFovLimit=10, # LowerFovLimit=-30) # settings.add_sensor(lidar) scene = self.carla_client.load_settings(settings) self.pre_image = None self.cur_image = None self.pre_measurements = None self.cur_measurements = None # define a random starting point of the agent for the appropriate trainning number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) # player_start = 140 self.carla_client.start_episode(player_start) print('Starting new episode at %r, %d...' % (scene.map_name, player_start)) # TODO: read and return status after reset return
def _build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depend on the selected Town. """ # We set the camera # This single RGB camera is used on every experiment camera = Camera('CameraRGB') camera.set(FOV=100) camera.set_image_size(800, 600) camera.set_position(200, 0, 140) camera.set_rotation(-15.0, 0, 0) weathers = [1, 3, 6, 8, 4, 14] if self._city_name == 'Town01': poses_tasks = self._poses_town01() vehicles_tasks = [0, 0, 0, 20] pedestrians_tasks = [0, 0, 0, 50] else: poses_tasks = self._poses_town02() vehicles_tasks = [0, 0, 0, 15] pedestrians_tasks = [0, 0, 0, 50] experiments_vector = [] for weather in weathers: for iteration in range(len(poses_tasks)): poses = poses_tasks[iteration] vehicles = vehicles_tasks[iteration] pedestrians = pedestrians_tasks[iteration] conditions = CarlaSettings() conditions.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather, SeedVehicles=123456789, SeedPedestrians=123456789) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set(Conditions=conditions, Poses=poses, Id=iteration, Repetitions=1) experiments_vector.append(experiment) return experiments_vector
def __init__(self, name, flags, camera_setups=[], lidar_stream_names=[], log_file_name=None, csv_file_name=None): 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.message_num = 0 if self._flags.carla_high_quality: quality = 'Epic' else: quality = 'Low' self.settings = CarlaSettings() self.settings.set( SynchronousMode=self._flags.carla_synchronous_mode, SendNonPlayerAgentsInfo=True, NumberOfVehicles=self._flags.carla_num_vehicles, NumberOfPedestrians=self._flags.carla_num_pedestrians, WeatherId=self._flags.carla_weather, QualityLevel=quality) self.settings.randomize_seeds() self.lidar_streams = [] for (camera_stream_name, camera_type, image_size, pos) in camera_setups: self.__add_camera(name=camera_stream_name, postprocessing=camera_type, image_size=image_size, position=pos) for lidar_stream_name in lidar_stream_names: self.__add_lidar(name=lidar_stream_name) self.agent_id_map = {} self.pedestrian_count = 0
def build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depend on the selected Town. """ # We set the camera # This single RGB camera is used on every experiment camera = Camera('CameraRGB') camera.set(FOV=100) camera.set_image_size(800, 600) camera.set_position(2.0, 0.0, 1.4) camera.set_rotation(-15.0, 0, 0) if self._city_name == 'Town01': poses_tasks = self._poses_town01() vehicles_tasks = [25] pedestrians_tasks = [25] else: poses_tasks = self._poses_town02() vehicles_tasks = [0, 25] pedestrians_tasks = [0, 25] experiments_vector = [] #weathers = [1,3,6,8] for weather in self.weathers: #prinst(weather , vehicles_tasks) for scenario in range(len(vehicles_tasks)): for iteration in range(len(poses_tasks)): #print(f"interation : {iteration} , scenario:{scenario}") poses = poses_tasks[iteration] #print("poses re",poses) vehicles = vehicles_tasks[scenario] #print("Vehicles: ",vehicles) pedestrians = pedestrians_tasks[scenario] #print("pedestrians: ",pedestrians) conditions = CarlaSettings() conditions.set(SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set(Conditions=conditions, Poses=poses, Task=iteration, Repetitions=1) experiments_vector.append(experiment) return experiments_vector
def build_experiments(self): """ Creates the whole set of experiment objects, The experiments created depend on the selected Town. """ # We set the camera # This single RGB camera is used on every experiment camera = Camera('rgb') camera.set(FOV=90) camera.set_image_size(300, 300) camera.set_position(0.2, 0.0, 0.85) camera.set_rotation(-3.0, 0, 0) poses_tasks = self._poses() vehicles_tasks = [0, 0, 0, 15] pedestrians_tasks = [0, 0, 0, 50] experiments_vector = [] for weather in self.weathers: for iteration in range(len(poses_tasks)): poses = poses_tasks[iteration] vehicles = vehicles_tasks[iteration] pedestrians = pedestrians_tasks[iteration] conditions = CarlaSettings() conditions.set( SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicles, NumberOfPedestrians=pedestrians, WeatherId=weather, QualityLevel='Epic' ) # Add all the cameras that were set for this experiments conditions.add_sensor(camera) experiment = Experiment() experiment.set( Conditions=conditions, Poses=poses, Task=iteration, Repetitions=1 ) experiments_vector.append(experiment) return experiments_vector
def generate_settings(args, sync_mode=True): settings = CarlaSettings() settings.set(SynchronousMode=sync_mode, SendNonPlayerAgentsInfo=False, NumberOfVehicles=0, NumberOfPedestrians=0, WeatherId=args.weather, QualityLevel=args.quality_level) settings.randomize_seeds() return settings
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need.""" settings = CarlaSettings() settings.set(SynchronousMode=False, SendNonPlayerAgentsInfo=False, NumberOfVehicles=0, NumberOfPedestrians=0, WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel=args.quality_level) settings.randomize_seeds() return settings
def _make_carla_client(self, host, port): while True: try: self.logger.info( "Trying to make client on port {}".format(port)) self._client = CarlaClient(host, port, timeout=100) self._client.connect() self._client.load_settings(CarlaSettings(QualityLevel='Low')) self._client.start_episode(0) self.logger.info( "Successfully made client on port {}".format(port)) break except TCPConnectionError as error: self.logger.debug('Got TCPConnectionError..sleeping for 1') self.logger.error(error) time.sleep(1)
def gen_settings(args): settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=False, NumberOfVehicles=0, NumberOfPedestrians=0, WeatherId=args.weather, QualityLevel=args.quality_level) settings.randomize_seeds() camera_pos_x = 2 camera_pos_y = 0 camera_pos_z = 1 camera = Camera("MainCamera") camera.set_image_size(800, 600) camera.set_position(camera_pos_x, camera_pos_y, camera_pos_z) settings.add_sensor(camera) return settings
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need.""" settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=0, NumberOfPedestrians=0, SeedVehicles='00000', WeatherId=1, QualityLevel=args.quality_level) settings.randomize_seeds() return settings
def _make_carla_client(self, host, port): for _ in range( 20 ): # Try to connect for up to 20 times (usually connects first attempt if everything is correct) try: self.logger.info( "Trying to make client on port {}".format(port)) self._client = CarlaClient(host, port, timeout=100) self._client.connect() self._client.load_settings(CarlaSettings(QualityLevel='Low')) self._client.start_episode(0) self.logger.info( "Successfully made client on port {}".format(port)) break except TCPConnectionError as error: self.logger.debug('Got TCPConnectionError..sleeping for 1') self.logger.error(error) time.sleep(1)
def make_carla_settings(args): ''' Creates a CarlaSettings object. It configures the sensors, pedestrians, vehicles, weather, etc. ''' settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=args.vehicles, NumberOfPedestrians=args.pedestrians, WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel=args.quality_level) settings.randomize_seeds() # The default camera captures RGB images of the scene. camera0 = Camera('CameraRGB') # Set image resolution in pixels. camera0.set_image_size(800, 600) # Set its position relative to the car in meters. camera0.set_position(0.30, 0, 1.30) settings.add_sensor(camera0) return settings
def get_carla_settings(settings_file=None): if settings_file is None: # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=False, NumberOfVehicles=0, NumberOfPedestrians=0, # 8-14 are sunset; we want easy first WeatherId=random.choice(range(0, 11)), QualityLevel='Epic' ) settings.randomize_seeds() # Now we want to add a couple of cameras to the player vehicle. # We will collect the images produced by these cameras every # frame. # The default camera captures RGB images of the scene. camera0 = Camera('CameraRGB') # Set image resolution in pixels. camera0.set_image_size(carla_config.render_width, carla_config.render_height) # Set its position relative to the car in meters. camera0.set_position(0.30, 0, 1.30) settings.add_sensor(camera0) else: # Alternatively, we can load these settings from a file. with open(settings_file, 'r') as fp: settings = fp.read() return settings
def make_carla_settings(): """Make a CarlaSettings object with the settings we need.""" settings = CarlaSettings() settings.set( SynchronousMode=False, SendNonPlayerAgentsInfo=True, NumberOfVehicles=15, NumberOfPedestrians=30, WeatherId=random.choice([1, 3, 7, 8, 14])) settings.randomize_seeds() # camera0 = sensor.Camera('CameraRGB') # camera0.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT) # camera0.set_position(200, 0, 140) # camera0.set_rotation(0.0, 0.0, 0.0) # settings.add_sensor(camera0) # camera1 = sensor.Camera('CameraDepth', PostProcessing='Depth') # camera1.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT) # camera1.set_position(200, 0, 140) # camera1.set_rotation(0.0, 0.0, 0.0) # settings.add_sensor(camera1) # camera2 = sensor.Camera('CameraSemSeg', PostProcessing='SemanticSegmentation') # camera2.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT) # camera2.set_position(200, 0, 140) # camera2.set_rotation(0.0, 0.0, 0.0) # settings.add_sensor(camera2) lidar0 = sensor.Lidar('Lidar32') lidar0.set_position(0, 0, 250) lidar0.set_rotation(0, 0, 0) lidar0.set( Channels = 32, Range = 5000, PointsPerSecond = 640000, RotationFrequency = 10, UpperFovLimit = 10, LowerFovLimit = -30, ShowDebugPoints = False) settings.add_sensor(lidar0) return settings
def make_carla_settings(args, vehicle_tot): # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() settings.set(SynchronousMode=False, SendNonPlayerAgentsInfo=True, NumberOfVehicles=vehicle_tot, NumberOfPedestrians=0, WeatherId=1, QualityLevel=args.quality_level) settings.randomize_seeds() camera0 = sensor.Camera('CameraRGB') camera0.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT) px, py, pz = 2.0, 0.0, 1.4 # Default (Front) rx, ry, rz = 0.0, 0.0, 0.0 if args.camera == "Driver": px = -0.3 py = -0.5 pz = 1.1 elif args.camera == "Hood": px = 1.0 py = 0.0 pz = 1.4 elif args.camera == "Top": px = -5.5 py = 0.0 pz = 3.0 rx = -10.0 camera0.set_position(px, py, pz) camera0.set_rotation(rx, ry, rz) settings.add_sensor(camera0) return settings
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need. """ settings = CarlaSettings() #settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=False, NumberOfVehicles=2, NumberOfPedestrians=10, WeatherId=1) #settings.randomize_seeds() camera1 = Camera('CameraDepth', PostProcessing='Depth', FOV=fov) camera1.set_image_size(*image_size) camera1.set_position(*camera_local_pos) camera1.set_rotation(*camera_local_rotation) settings.add_sensor(camera1) camera2 = Camera('CameraRGB', PostProcessing='SceneFinal', FOV=fov) camera2.set_image_size(*image_size) camera2.set_position(*camera_local_pos) camera2.set_rotation(*camera_local_rotation) settings.add_sensor(camera2) return settings, camera2
def setup_client_and_server(self, reconnect_client_only=False): # open the server if not reconnect_client_only: self.server = self._open_server() self.server_pid = self.server.pid # To kill incase child process gets lost print("setup server, out:", self.server_pid) while True: try: self.game = CarlaClient(self.host, self.port, timeout=99999999) self.game.connect( connection_attempts=100) # It's taking a very long time for the server process to spawn, so the client needs to wait or try sufficient no. of times lol self.game.load_settings(CarlaSettings()) self.game.start_episode(0) self.is_game_setup = self.server and self.game print("setup client") return except TCPConnectionError as error: print(error) time.sleep(1)
def reset(self): """ Resets the state of the environment and returns an initial observation. :return: observation (object): the initial observation of the space. """ # load Carla settings settings = CarlaSettings() settings = self._load_settings(settings) scene = self.carla_client.load_settings(settings) # define a random starting point of the agent for the appropriate training number_of_player_starts = len(scene.player_start_spots) player_start = self.rng.randint(0, max(0, number_of_player_starts - 1)) self.carla_client.start_episode(player_start) print(f'Starting new episode at {scene.map_name}, {player_start}...') # read and return status after reset self.cur_measurements, self.cur_image = self._read_data() state = self._state( self.cur_image, self.cur_image ) #possibly revise the state to be some operation of several images. meas = self.cur_measurements return state, meas
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need.""" settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=5, NumberOfPedestrians=5, WeatherId=1, QualityLevel='Low', DisableTwoWheeledVehicles=True, SeedVehicles=123456789, SeedPedestrians=123456789) camera0 = sensor.Camera('CameraRGB') camera0.set_image_size(WINDOW_WIDTH, WINDOW_HEIGHT) camera0.set_position(2.0, 0.0, 1.4) camera0.set_rotation(0.0, 0.0, 0.0) settings.add_sensor(camera0) return settings
def view_start_positions(args): # We assume the CARLA server is already waiting for a client to connect at # host:port. The same way as in the client example. with make_carla_client(args.host, args.port) as client: print('CarlaClient connected') # We load the default settings to the client. scene = client.load_settings(CarlaSettings()) print("Received the start positions") try: image = mpimg.imread('carla/planner/%s.png' % scene.map_name) carla_map = CarlaMap(scene.map_name, 0.1653, 50) except IOError as exception: logging.error(exception) logging.error('Cannot find map "%s"', scene.map_name) sys.exit(1) fig, ax = plt.subplots(1) plt.ion() ax.imshow(image) if args.positions == 'all': positions_to_plot = range(len(scene.player_start_spots)) else: positions_to_plot = map(int, args.positions.split(',')) while True: measurements, sensor_data = client.read_data() current_x = measurements.player_measurements.transform.location.x current_y = measurements.player_measurements.transform.location.y pixel = carla_map.convert_to_pixel([current_x, current_y, 0]) circle = Circle((pixel[0], pixel[1]), 12, color='k', label='car') ax.add_patch(circle) plt.pause(0.05)
def start_game(args): pygame.init() with make_carla_client(args.host, args.port) as client: settings = CarlaSettings() settings.set( SynchronousMode=False, SendNonPlayerAgentsInfo=True, NumberOfVehicles=3, NumberOfPedestrians=40, WeatherId=1, QualityLevel="Low") settings.randomize_seeds() scene = client.load_settings(settings) screen, img, dim, scale = setup_game_window(scene) town_map = CarlaMap(scene.map_name, 0.1653, 50) client.start_episode(7) while running: manage_events(pygame.event.get()) screen.blit(img, (0, 0)) play_frame(client, town_map, screen, dim, scale) pygame.display.flip()
def run_carla_client(args): skip_frames = 100 # 100 # at 10 fps number_of_episodes = args.n_episode frames_per_episode = args.n_frame # n_weather = 14 # weathers starts from 1 # n_player_start_spots = 152 # Town01 n_player_start_spots = 83 # Town02 weathers = number_of_episodes * [2] # CloudyNoon start_spots = list(range(n_player_start_spots)) random.shuffle(start_spots) assert number_of_episodes < n_player_start_spots start_spots = start_spots[:number_of_episodes] # weathers = list(range(number_of_episodes)) # # random.shuffle(weathers) # weathers = [w % 14 + 1 for w in weathers] # https://carla.readthedocs.io/en/latest/carla_settings/ # number_of_episodes = n_weather*n_player_start_spots # frames_per_episode = args.n_frame # weathers, start_spots = np.meshgrid(list(range(1, n_weather+1)), list(range(n_player_start_spots))) # weathers = weathers.flatten() # start_spots = start_spots.flatten() if not os.path.isdir(args.out_dir): os.makedirs(args.out_dir) np.savetxt(join(args.out_dir, 'weathers.txt'), weathers, fmt='%d') np.savetxt(join(args.out_dir, 'start-spots.txt'), start_spots, fmt='%d') # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the `make_carla_client` # context manager, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. The # context manager makes sure the connection is always cleaned up on exit. with make_carla_client(args.host, args.port) as client: print('CarlaClient connected') for episode in range(number_of_episodes): # Start a new episode. if args.settings_filepath is None: # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=20, NumberOfPedestrians=40, WeatherId=weathers[episode], # WeatherId=random.randrange(14) + 1, # WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel=args.quality_level) settings.randomize_seeds() # Now we want to add a couple of cameras to the player vehicle. # We will collect the images produced by these cameras every # frame. # The default camera captures RGB images of the scene. camera0 = Camera('RGB') # Set image resolution in pixels. camera0.set(FOV=args.cam_fov) camera0.set_image_size(args.x_res, args.y_res) # Set its position relative to the car in meters. camera0.set_position(*args.cam_offset) camera0.set_rotation(*args.cam_rotation) settings.add_sensor(camera0) # Let's add another camera producing ground-truth depth. camera1 = Camera('Depth', PostProcessing='Depth') camera1.set(FOV=args.cam_fov) camera1.set_image_size(args.x_res, args.y_res) camera1.set_position(*args.cam_offset) camera1.set_rotation(*args.cam_rotation) settings.add_sensor(camera1) camera2 = Camera('SegRaw', PostProcessing='SemanticSegmentation') camera2.set(FOV=args.cam_fov) camera2.set_image_size(args.x_res, args.y_res) camera2.set_position(*args.cam_offset) camera2.set_rotation(*args.cam_rotation) settings.add_sensor(camera2) if args.lidar: lidar = Lidar('Lidar32') lidar.set_position(0, 0, 2.50) lidar.set_rotation(0, 0, 0) lidar.set(Channels=32, Range=50, PointsPerSecond=100000, RotationFrequency=10, UpperFovLimit=10, LowerFovLimit=-30) settings.add_sensor(lidar) else: # Alternatively, we can load these settings from a file. with open(args.settings_filepath, 'r') as fp: settings = fp.read() # Now we load these settings into the server. The server replies # with a scene description containing the available start spots for # the player. Here we can provide a CarlaSettings object or a # CarlaSettings.ini file as string. scene = client.load_settings(settings) # Choose one player start at random. # number_of_player_starts = len(scene.player_start_spots) # player_start = random.randrange(number_of_player_starts) player_start = start_spots[episode] # Notify the server that we want to start the episode at the # player_start index. This function blocks until the server is ready # to start the episode. print('Starting new episode...') client.start_episode(player_start) frame = 0 save_frame_idx = 0 # extra_frames = 0 # last_control = None # last_control_changed = 0 # while frame < skip_frames + frames_per_episode + extra_frames: # Iterate every frame in the episode. for frame in range(skip_frames + frames_per_episode): # Read the data produced by the server this frame. measurements, sensor_data = client.read_data() # Print some of the measurements. print_measurements(measurements) # Save the images to disk if requested. if args.save_images_to_disk and frame >= skip_frames \ and (frame - skip_frames) % args.save_every_n_frames == 0: # if last_control_changed < frame - args.save_every_n_frames: # extra_frames += args.save_every_n_frames # last_control_changed += args.save_every_n_frames # else: save_frame_idx += 1 for name, measurement in sensor_data.items(): filename = args.out_filename_format.format( episode + 1, name, save_frame_idx) measurement.save_to_disk(filename) # We can access the encoded data of a given image as numpy # array using its "data" property. For instance, to get the # depth value (normalized) at pixel X, Y # # depth_array = sensor_data['CameraDepth'].data # value_at_pixel = depth_array[Y, X] # # Now we have to send the instructions to control the vehicle. # If we are in synchronous mode the server will pause the # simulation until we send this control. if not args.autopilot: client.send_control(steer=random.uniform(-1.0, 1.0), throttle=0.5, brake=0.0, hand_brake=False, reverse=False) else: # Together with the measurements, the server has sent the # control that the in-game autopilot would do this frame. We # can enable autopilot by sending back this control to the # server. We can modify it if wanted, here for instance we # will add some noise to the steer. control = measurements.player_measurements.autopilot_control # if last_control: # for v1, v2 in zip(control.values(), last_control.values()): # if v1 != v2: # last_control_changed = frame # break control.steer += random.uniform(-0.1, 0.1) client.send_control(control)
def run_carla_client(args): # Here we will run 3 episodes with 300 frames each. number_of_episodes = 3 frames_per_episode = 300 # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the `make_carla_client` # context manager, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. The # context manager makes sure the connection is always cleaned up on exit. with make_carla_client(args.host, args.port) as client: print('CarlaClient connected') for episode in range(0, number_of_episodes): # Start a new episode. if args.settings_filepath is None: # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=20, NumberOfPedestrians=40, WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel=args.quality_level) settings.randomize_seeds() # Now we want to add a couple of cameras to the player vehicle. # We will collect the images produced by these cameras every # frame. # The default camera captures RGB images of the scene. camera0 = Camera('CameraRGB') # Set image resolution in pixels. camera0.set_image_size(800, 600) # Set its position relative to the car in meters. camera0.set_position(0.30, 0, 1.30) settings.add_sensor(camera0) # Let's add another camera producing ground-truth depth. camera1 = Camera('CameraDepth', PostProcessing='Depth') camera1.set_image_size(800, 600) camera1.set_position(0.30, 0, 1.30) settings.add_sensor(camera1) if args.lidar: lidar = Lidar('Lidar32') lidar.set_position(0, 0, 2.50) lidar.set_rotation(0, 0, 0) lidar.set( Channels=32, Range=50, PointsPerSecond=100000, RotationFrequency=10, UpperFovLimit=10, LowerFovLimit=-30) settings.add_sensor(lidar) else: # Alternatively, we can load these settings from a file. with open(args.settings_filepath, 'r') as fp: settings = fp.read() # Now we load these settings into the server. The server replies # with a scene description containing the available start spots for # the player. Here we can provide a CarlaSettings object or a # CarlaSettings.ini file as string. scene = client.load_settings(settings) # Choose one player start at random. number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) # Notify the server that we want to start the episode at the # player_start index. This function blocks until the server is ready # to start the episode. print('Starting new episode at %r...' % scene.map_name) client.start_episode(player_start) # Iterate every frame in the episode. for frame in range(0, frames_per_episode): # Read the data produced by the server this frame. measurements, sensor_data = client.read_data() # Print some of the measurements. print_measurements(measurements) # Save the images to disk if requested. if args.save_images_to_disk: for name, measurement in sensor_data.items(): filename = args.out_filename_format.format(episode, name, frame) measurement.save_to_disk(filename) # We can access the encoded data of a given image as numpy # array using its "data" property. For instance, to get the # depth value (normalized) at pixel X, Y # # depth_array = sensor_data['CameraDepth'].data # value_at_pixel = depth_array[Y, X] # # Now we have to send the instructions to control the vehicle. # If we are in synchronous mode the server will pause the # simulation until we send this control. if not args.autopilot: client.send_control( steer=random.uniform(-1.0, 1.0), throttle=0.5, brake=0.0, hand_brake=False, reverse=False) else: # Together with the measurements, the server has sent the # control that the in-game autopilot would do this frame. We # can enable autopilot by sending back this control to the # server. We can modify it if wanted, here for instance we # will add some noise to the steer. control = measurements.player_measurements.autopilot_control control.steer += random.uniform(-0.1, 0.1) client.send_control(control)
def run_carla_client(host, port, far): # Here we will run a single episode with 300 frames. number_of_frames = 3000 frame_step = 100 # Save one image every 100 frames output_folder = '_out' image_size = [800, 600] camera_local_pos = [0.3, 0.0, 1.3] # [X, Y, Z] camera_local_rotation = [0, 0, 0] # [pitch(Y), yaw(Z), roll(X)] fov = 70 # Connect with the server with make_carla_client(host, port) as client: print('CarlaClient connected') # Here we load the settings. settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=False, NumberOfVehicles=20, NumberOfPedestrians=40, WeatherId=random.choice([1, 3, 7, 8, 14])) settings.randomize_seeds() camera1 = Camera('CameraDepth', PostProcessing='Depth', FOV=fov) camera1.set_image_size(*image_size) camera1.set_position(*camera_local_pos) camera1.set_rotation(*camera_local_rotation) settings.add_sensor(camera1) camera2 = Camera('CameraRGB', PostProcessing='SceneFinal', FOV=fov) camera2.set_image_size(*image_size) camera2.set_position(*camera_local_pos) camera2.set_rotation(*camera_local_rotation) settings.add_sensor(camera2) client.load_settings(settings) # Start at location index id '0' client.start_episode(0) # Compute the camera transform matrix camera_to_car_transform = camera2.get_unreal_transform() # Iterate every frame in the episode except for the first one. for frame in range(1, number_of_frames): # Read the data produced by the server this frame. measurements, sensor_data = client.read_data() # Save one image every 'frame_step' frames if not frame % frame_step: # Start transformations time mesure. timer = StopWatch() # RGB image [[[r,g,b],..[r,g,b]],..[[r,g,b],..[r,g,b]]] image_RGB = to_rgb_array(sensor_data['CameraRGB']) # 2d to (camera) local 3d # We use the image_RGB to colorize each 3D point, this is optional. # "max_depth" is used to keep only the points that are near to the # camera, meaning 1.0 the farest points (sky) point_cloud = depth_to_local_point_cloud( sensor_data['CameraDepth'], image_RGB, max_depth=far ) # (Camera) local 3d to world 3d. # Get the transform from the player protobuf transformation. world_transform = Transform( measurements.player_measurements.transform ) # Compute the final transformation matrix. car_to_world_transform = world_transform * camera_to_car_transform # Car to World transformation given the 3D points and the # transformation matrix. point_cloud.apply_transform(car_to_world_transform) # End transformations time mesure. timer.stop() # Save PLY to disk # This generates the PLY string with the 3D points and the RGB colors # for each row of the file. point_cloud.save_to_disk(os.path.join( output_folder, '{:0>5}.ply'.format(frame)) ) print_message(timer.milliseconds(), len(point_cloud), frame) client.send_control( measurements.player_measurements.autopilot_control )
def run_carla_client(args): # Here we will run 3 episodes with 300 frames each. number_of_episodes = 5 cut_per_episode = 40 frames_per_cut = 200 # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the `make_carla_client` # context manager, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. The # context manager makes sure the connection is always cleaned up on exit. with make_carla_client(args.host, args.port) as client: print('CarlaClient connected') for episode in range(0, number_of_episodes): print("input any key to continue...") start = input() # each episode dir store a set of traindata. if dir not existed, then make it pathdir = '/home/kadn/dataTrain/episode_{:0>3}'.format(episode) mkdir(pathdir) # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=20, NumberOfPedestrians=40, # WeatherId=random.choice([1, 3, 7, 8, 14]), WeatherId = 1, QualityLevel=args.quality_level) settings.randomize_seeds() # Now we want to add a couple of cameras to the player vehicle. # We will collect the images produced by these cameras every # frame. # The default camera captures RGB images of the scene. camera0 = Camera('CameraRGB') # Set image resolution in pixels. camera0.set_image_size(88, 200) # Set its position relative to the car in meters. camera0.set_position(0.30, 0, 1.30) settings.add_sensor(camera0) # Let's add another camera producing ground-truth depth. camera1 = Camera('CameraDepth', PostProcessing='Depth') camera1.set_image_size(200, 88) camera1.set_position(0.30, 0, 1.30) settings.add_sensor(camera1) camera2 = Camera('CameraSemSeg', PostProcessing='SemanticSegmentation') camera2.set_image_size(88, 200) camera2.set_position(2.0, 0.0, 1.4) camera2.set_rotation(0.0, 0.0, 0.0) settings.add_sensor(camera2) # if args.lidar: lidar = Lidar('Lidar32') lidar.set_position(0, 0, 2.50) lidar.set_rotation(0, 0, 0) lidar.set( Channels=0, Range=30, PointsPerSecond=200000, RotationFrequency=10, UpperFovLimit=0, LowerFovLimit=0) settings.add_sensor(lidar) # else: # # # Alternatively, we can load these settings from a file. # with open(args.settings_filepath, 'r') as fp: # settings = fp.read() # Now we load these settings into the server. The server replies # with a scene description containing the available start spots for # the player. Here we can provide a CarlaSettings object or a # CarlaSettings.ini file as string. scene = client.load_settings(settings) # Choose one player start at random. # number_of_player_starts = len(scene.player_start_spots) # player_start = random.randint(0, max(0, number_of_player_starts - 1)) player_start = 1 # Notify the server that we want to start the episode at the # player_start index. This function blocks until the server is ready # to start the episode. print('Starting new episode at %r...' % scene.map_name) # Start a new episode. client.start_episode(player_start) for cut_per_episode in range(0,cut_per_episode): num = fileCounter(pathdir) filename = "data_{:0>6}.h5".format(num) filepath = pathdir + '/' + filename f = h5py.File(filepath, "w") rgb_file = f.create_dataset("rgb", (200, 88, 200), np.uint8) seg_file = f.create_dataset("seg", (200, 88, 200), np.uint8) lidar_file = f.create_dataset('lidar',(200,200,200),np.uint8) startendpoint = f.create_dataset('startend',(1,2),np.float32) targets_file = f.create_dataset("targets", (200, 28), np.float32) index_file = 0 # Iterate every frame in the episode. for frame in range(0, frames_per_cut): # Read the data produced by the server this frame. measurements, sensor_data = client.read_data() # get data and store sensors, targets_data = record_train_data(measurements,sensor_data) rgb_file[:,:,index_file] = sensors['rgb'] seg_file[:,:,index_file] = sensors['seg'] lidar_file[:,:,index_file] = sensors['lidar'] targets_file[index_file,:] = targets_data index_file += 1 # We can access the encoded data of a given image as numpy # array using its "data" property. For instance, to get the # depth value (normalized) at pixel X, Y # # depth_array = sensor_data['CameraDepth'].data # value_at_pixel = depth_array[Y, X] # # Now we have to send the instructions to control the vehicle. # If we are in synchronous mode the server will pause the # simulation until we send this control. if args.autopilot: client.send_control( steer=0, throttle=0.8, brake=0.0, hand_brake=False, reverse=False) else: # Together with the measurements, the server has sent the # control that the in-game autopilot would do this frame. We # can enable autopilot by sending back this control to the # server. We can modify it if wanted, here for instance we # will add some noise to the steer. control = measurements.player_measurements.autopilot_control control.steer += random.uniform(-0.05, 0.05) client.send_control(control)
def run(self): settings = CarlaSettings() settings.add_camera(Camera('DefaultCamera')) self.run_carla_client(settings, 1, 2000, use_ai_control=True)
def run(self): settings = CarlaSettings() settings.add_camera(Camera('DefaultCamera')) self.run_carla_client(settings, 5, 200)
def run(self): settings = CarlaSettings(SynchronousMode=True) settings.add_camera(Camera('DefaultCamera')) self.run_carla_client(settings, 3, 200)
def run_carla_clients(args): filename = '_images_repeatability/server{:d}/{:0>6d}.png' with make_carla_client(args.host1, args.port1) as client1: logging.info('1st client connected') with make_carla_client(args.host2, args.port2) as client2: logging.info('2nd client connected') settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=50, NumberOfPedestrians=50, WeatherId=random.choice([1, 3, 7, 8, 14])) settings.randomize_seeds() if args.images_to_disk: camera = Camera('DefaultCamera') camera.set_image_size(800, 600) settings.add_sensor(camera) scene1 = client1.load_settings(settings) scene2 = client2.load_settings(settings) number_of_player_starts = len(scene1.player_start_spots) assert number_of_player_starts == len(scene2.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) logging.info( 'start episode at %d/%d player start (run forever, press ctrl+c to cancel)', player_start, number_of_player_starts) client1.start_episode(player_start) client2.start_episode(player_start) frame = 0 while True: frame += 1 meas1, sensor_data1 = client1.read_data() meas2, sensor_data2 = client2.read_data() player1 = meas1.player_measurements player2 = meas2.player_measurements images1 = [x for x in sensor_data1.values() if isinstance(x, Image)] images2 = [x for x in sensor_data2.values() if isinstance(x, Image)] control1 = player1.autopilot_control control2 = player2.autopilot_control try: assert len(images1) == len(images2) assert len(meas1.non_player_agents) == len(meas2.non_player_agents) assert player1.transform.location.x == player2.transform.location.x assert player1.transform.location.y == player2.transform.location.y assert player1.transform.location.z == player2.transform.location.z assert control1.steer == control2.steer assert control1.throttle == control2.throttle assert control1.brake == control2.brake assert control1.hand_brake == control2.hand_brake assert control1.reverse == control2.reverse except AssertionError: logging.exception('assertion failed') if args.images_to_disk: assert len(images1) == 1 images1[0].save_to_disk(filename.format(1, frame)) images2[0].save_to_disk(filename.format(2, frame)) client1.send_control(control1) client2.send_control(control2)
def make_carla_settings(args): """Make a CarlaSettings object with the settings we need.""" settings = CarlaSettings() settings.set( SynchronousMode=True, SendNonPlayerAgentsInfo=False, NumberOfVehicles=30, NumberOfPedestrians=0, WeatherId=1, QualityLevel='Epic') settings.randomize_seeds() camera0 = sensor.Camera('CameraRGB') camera0.set_image_size(CAM_WINDOW_WIDTH, CAM_WINDOW_HEIGHT) camera0.set_position(2.0, 0.0, 1.4) camera0.set_rotation(-15.0, 0.0, 0.0) settings.add_sensor(camera0) camera1 = sensor.Camera('CameraDepth', PostProcessing='Depth') camera1.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT) camera1.set_position(2.0, 0.0, 1.4) camera1.set_rotation(0.0, 0.0, 0.0) settings.add_sensor(camera1) camera2 = sensor.Camera('CameraSemSeg', PostProcessing='SemanticSegmentation') camera2.set_image_size(MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT) camera2.set_position(2.0, 0.0, 1.4) camera2.set_rotation(0.0, 0.0, 0.0) settings.add_sensor(camera2) lidar = sensor.Lidar('Lidar32') lidar.set_position(0, 0, 2.5) lidar.set_rotation(0, 0, 0) lidar.set( Channels=0, Range=50, PointsPerSecond=100000, RotationFrequency=10, UpperFovLimit=0, LowerFovLimit=0) settings.add_sensor(lidar) return settings
def run_carla_client(host, port, autopilot_on, save_images_to_disk, image_filename_format): # Here we will run 3 episodes with 300 frames each. number_of_episodes = 3 frames_per_episode = 300 # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the `make_carla_client` # context manager, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. The # context manager makes sure the connection is always cleaned up on exit. with make_carla_client(host, port) as client: print('CarlaClient connected') for episode in range(0, number_of_episodes): # Start a new episode. # Create a CarlaSettings object. This object is a handy wrapper # around the CarlaSettings.ini file. Here we set the configuration # we want for the new episode. settings = CarlaSettings() settings.set( SynchronousMode=True, NumberOfVehicles=30, NumberOfPedestrians=50, WeatherId=random.choice([1, 3, 7, 8, 14])) settings.randomize_seeds() # Now we want to add a couple of cameras to the player vehicle. We # will collect the images produced by these cameras every frame. # The default camera captures RGB images of the scene. camera0 = Camera('CameraRGB') # Set image resolution in pixels. camera0.set_image_size(800, 600) # Set its position relative to the car in centimeters. camera0.set_position(30, 0, 130) settings.add_camera(camera0) # Let's add another camera producing ground-truth depth. camera1 = Camera('CameraDepth', PostProcessing='Depth') camera1.set_image_size(800, 600) camera1.set_position(30, 0, 130) settings.add_camera(camera1) print('Requesting new episode...') # Now we request a new episode with these settings. The server # replies with a scene description containing the available start # spots for the player. Here instead of a CarlaSettings object we # could also provide a CarlaSettings.ini file as string. scene = client.request_new_episode(settings) # Choose one player start at random. number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) # Notify the server that we want to start the episode at # `player_start`. This function blocks until the server is ready to # start the episode. client.start_episode(player_start) # Iterate every frame in the episode. for frame in range(0, frames_per_episode): # Read the measurements and images produced by the server this # frame. measurements, images = client.read_measurements() # Print some of the measurements we received. print_player_measurements(measurements.player_measurements) # Save the images to disk if requested. if save_images_to_disk: for n, image in enumerate(images): image.save_to_disk(image_filename_format.format(episode, n, frame)) # Now we have to send the instructions to control the vehicle. # If we are in synchronous mode the server will pause the # simulation until we send this control. if not autopilot_on: client.send_control( steer=random.uniform(-1.0, 1.0), throttle=0.3, brake=False, hand_brake=False, reverse=False) else: # Together with the measurements, the server has sent the # control that the in-game AI would do this frame. We can # enable autopilot by sending back this control to the # server. Here we will also add some noise to the steer. control = measurements.player_measurements.ai_control control.steer += random.uniform(-0.1, 0.1) client.send_control(control) print('Done.') return True