Example #1
0
    def Create_actors(self,world, blueprint_library): 
        ego_list = []
        npc_list = []
        obstacle_list = []
        sensor_list = []
        # ego车辆设置---------------------------------------------------------------
        ego_bp = blueprint_library.find(id='vehicle.lincoln.mkz2017')
        # 坐标建立
        ego_transform = Transform(Location(x=160.341522, y=-371.640472, z=0.281942), 
                    Rotation(pitch=0.000000, yaw=0.500910, roll=0.000000))
        # 车辆从蓝图定义以及坐标生成
        ego = world.spawn_actor(ego_bp, ego_transform)
        ego_list.append(ego)
        print('created %s' % ego.type_id)

        # 视角设置------------------------------------------------------------------
        spectator = world.get_spectator()
        # spec_transform = ego.get_transform()
        spec_transform = Transform(Location(x=140.341522, y=-375.140472, z=15.281942), 
                    Rotation(pitch=0.000000, yaw=0.500910, roll=0.000000))
        spec_transform.location += carla.Location(x=60,z=45)
        spec_transform.rotation = carla.Rotation(pitch=-90, yaw=90)
        spectator.set_transform(spec_transform)

        # npc设置--------------------------------------------------------------------
        npc_transform = ego_transform
        for i in range(1):
            npc_transform.location += carla.Location(x=-15,y=-3.5)
            npc_bp = blueprint_library.find(id='vehicle.lincoln.mkz2017')
            npc_bp.set_attribute('color', '229,28,0')
            npc = world.try_spawn_actor(npc_bp, npc_transform)
            if npc is None:
                print('%s npc created failed' % i)
            else:
                npc_list.append(npc)
                print('created %s' % npc.type_id)

        # 障碍物设置------------------------------------------------------------------
        obstacle_transform = ego_transform
        for i in range(28):
            if i == 0:
                obsta_bp = blueprint_library.find(id='vehicle.mercedes-benz.coupe')
                obstacle_transform.location += carla.Location(x=95,y=3.8)
                obstacle = world.try_spawn_actor(obsta_bp, obstacle_transform)
                obstacle_transform.location += carla.Location(x=0,y=-5.3)
                if obstacle is None:
                    print('%s obstacle created failed' % i)
                else:
                    obstacle_list.append(obstacle)
                    print('created %s' % obstacle.type_id)
            else:
                obsta_bp = blueprint_library.find(id='static.prop.streetbarrier')
                obstacle_transform.location += carla.Location(x=-3.5,y=7.4)
                obstacle1 = world.try_spawn_actor(obsta_bp, obstacle_transform)
                obstacle_list.append(obstacle1)
                obstacle_transform.location += carla.Location(y=-7.4)
                obstacle2 = world.try_spawn_actor(obsta_bp, obstacle_transform)
                obstacle_list.append(obstacle2)

        # 传感器设置-------------------------------------------------------------------
        ego_collision = SS.CollisionSensor(ego)
        npc_collision = SS.CollisionSensor(npc)
        # lane_invasion = SS.LaneInvasionSensor(ego)
        sensor_list.append(ego_collision)
        sensor_list.append(npc_collision)
        return ego_list,npc_list,obstacle_list,sensor_list
Example #2
0
File: utils.py Project: ymote/pylot
def get_top_down_transform(transform, top_down_camera_altitude):
    # Calculation relies on the fact that the camera's FOV is 90.
    top_down_location = (transform.location +
                         Location(0, 0, top_down_camera_altitude))
    return Transform(top_down_location, Rotation(-90, 0, 0))
Example #3
0
def setup_scenario(world,
                   client,
                   synchronous_master=False,
                   subject_behavior="normal"):

    # @todo cannot import these directly.
    SpawnActor = carla.command.SpawnActor
    SetAutopilot = carla.command.SetAutopilot
    FutureActor = carla.command.FutureActor

    batch = []
    ego_batch = []

    vehicles_list = []

    blueprints = world.get_blueprint_library()
    blueprints = [x for x in blueprints if x.id.endswith("model3")]

    all_waypoints = world.get_map().generate_waypoints(3)

    # waypoint_list = filter_waypoints(road_id, lane_id)
    left_most_lane = filter_waypoints(all_waypoints, 15, -3)
    middle_lane = filter_waypoints(all_waypoints, 15, -5)
    right_lane = filter_waypoints(all_waypoints, 15, -6)

    sub_spawn_point = middle_lane[1].transform
    manual_spawn_point = left_most_lane[1].transform
    ego_spawn_point = right_lane[1].transform

    sub_spawn_point = carla.Transform(
        Location(x=sub_spawn_point.location.x,
                 y=sub_spawn_point.location.y,
                 z=0.5),
        Rotation(yaw=sub_spawn_point.rotation.yaw),
    )
    ego_spawn_point = carla.Transform(
        Location(x=ego_spawn_point.location.x,
                 y=ego_spawn_point.location.y,
                 z=0.5),
        Rotation(yaw=ego_spawn_point.rotation.yaw),
    )
    manual_spawn_point = carla.Transform(
        Location(x=manual_spawn_point.location.x,
                 y=manual_spawn_point.location.y,
                 z=0.5),
        Rotation(yaw=manual_spawn_point.rotation.yaw),
    )

    # --------------
    # Spawn vehicles
    # --------------

    # Manual Vehicle
    blueprint = random.choice(blueprints)
    color = "0,255,0"
    blueprint.set_attribute("color", color)
    blueprint.set_attribute("role_name", "hero")
    batch.append(
        SpawnActor(blueprint,
                   manual_spawn_point).then(SetAutopilot(FutureActor, False)))

    # Subject Vehicle Details
    blueprint = random.choice(blueprints)
    color = "0,0,255"
    blueprint.set_attribute("color", color)
    blueprint.set_attribute("role_name", "autopilot")
    batch.append(
        SpawnActor(blueprint,
                   sub_spawn_point).then(SetAutopilot(FutureActor, True)))

    # Ego Vehicle Details
    color = "255,0,0"
    blueprint.set_attribute("color", color)
    blueprint.set_attribute("role_name", "ego")
    ego_batch.append(
        SpawnActor(blueprint,
                   ego_spawn_point).then(SetAutopilot(FutureActor, True)))

    # Spawn
    ego_vehicle_id = None
    for response in client.apply_batch_sync(ego_batch, synchronous_master):
        if response.error:
            print("Response Error while applying ego batch!")
        else:
            # self.vehicles_list.append(response.actor_id)
            ego_vehicle_id = response.actor_id
    print("Ego vehicle id: ------------------------------", ego_vehicle_id)
    for response in client.apply_batch_sync(batch, synchronous_master):
        if response.error:
            print("Response Error while applying batch!")
        else:
            vehicles_list.append(response.actor_id)

    manual_vehicle = world.get_actors(vehicles_list)[0]
    subject_vehicle = world.get_actors(vehicles_list)[1]

    ego_vehicle = world.get_actors([ego_vehicle_id])[0]

    print("Warm start initiated...")
    warm_start_curr = 0
    update_spectator(world, ego_vehicle)

    while warm_start_curr < 3:
        warm_start_curr += world.get_settings().fixed_delta_seconds
        if synchronous_master:
            world.tick()
        else:
            world.wait_for_tick()

    client.apply_batch_sync([SetAutopilot(ego_vehicle, False)],
                            synchronous_master)
    client.apply_batch_sync([SetAutopilot(subject_vehicle, False)],
                            synchronous_master)

    if subject_behavior == "manual":
        manual_agent = Agent(manual_vehicle)
    else:
        subject_agent = BehaviorAgent(subject_vehicle,
                                      behavior=subject_behavior)
        destination = carla.Location(x=240.50791931152344,
                                     y=45.247249603271484,
                                     z=0.0)
        subject_agent.set_destination(subject_agent.vehicle.get_location(),
                                      destination,
                                      clean=True)

        subject_agent.update_information(world)

    print("Warm start finished...")

    ## Get current lane waypoints
    ego_vehicle_location = ego_vehicle.get_location()
    nearest_waypoint = world.get_map().get_waypoint(ego_vehicle_location,
                                                    project_to_road=True)
    current_lane_waypoints = nearest_waypoint.next_until_lane_end(1)

    if subject_behavior == "manual":
        return (ego_vehicle, manual_vehicle, current_lane_waypoints,
                manual_agent)
    else:
        return (ego_vehicle, subject_vehicle, current_lane_waypoints,
                subject_agent)
Example #4
0
world = client.get_world()

blueprint_library = world.get_blueprint_library()
vehicle_blueprints = blueprint_library.filter('vehicle.mustang.*')
blueprint = random.choice(vehicle_blueprints)

transform = Transform(Location(50.0, 50.0, 2.0))
player_vehicle = world.spawn_actor(blueprint, transform)

vehicle_camera = carla.Camera('MyCameraDepth', type='Depth')
vehicle_camera.attach_to(player_vehicle, Transform(Location(z=60.0)))
vehicle_camera.listen(lambda image: image.save_to_disk())

static_camera = carla.Camera('SecurityCamera', type='SceneFinal')
static_camera.attach_to(world, Transform(Location(x=30.0), Rotation(yaw=90)))
static_camera.listen(lambda image: image.save_to_disk())

while True:
    control = carla.VehicleControl(throttle=0.6, reverse=True)
    player_vehicle.apply_control(control)

    time.sleep(1)

### Spawning error handling ####################################################

actor = world.try_spawn_actor(blueprint, Transform())
if actor is None:
    return

try:
Example #5
0
    def restart(self):
        # Keep same camera config if the camera manager exists.
        cam_index = self.camera_manager.index if self.camera_manager is not None else 0
        cam_pos_index = self.camera_manager.transform_index if self.camera_manager is not None else 0
        # Get a random blueprint.
        # ******I have made this random.choice just have one choice --> vehicle.bmw.grandtourer
        blueprint = random.choice(self.world.get_blueprint_library().filter(
            self._actor_filter))
        # print("blue print")
        # print(self.world.get_blueprint_library().filter(self._actor_filter))
        blueprint.set_attribute('role_name', 'hero')
        if blueprint.has_attribute('color'):
            color = random.choice(
                blueprint.get_attribute('color').recommended_values)
            # print("color")
            # print(blueprint.get_attribute('color').recommended_values)
            # ****** I have set the blueprint being white as '255,255,255'
            blueprint.set_attribute('color', '255,255,255')
        batch = []
        blueprint2 = random.choice(self.world.get_blueprint_library().filter(
            self._actor_filter))
        blueprint2.set_attribute('role_name', 'hero2')
        if blueprint2.has_attribute('color'):
            blueprint2.set_attribute('color', '0,0,0')
        # batch.append(SpawnActor(blueprint, transform))

        # Spawn the player.

        #     ******************* change positions ****************************
        while self.player is None:
            spawn_points = self.map.get_spawn_points()

            print("length of sapwn_point is %d" % len(spawn_points) +
                  ",just choose one")  # 257 just choose one
            # Transform(Location(x=299.399994, y=129.750000, z=1.320625), Rotation(pitch=0.000000, yaw=179.999756, roll=0.000000))
            spawn_point = spawn_points[2]
            spawn_point1 = Transform(
                Location(x=250, y=129.750000, z=1.320625),
                Rotation(pitch=0.000000, yaw=179.999756, roll=0.000000))
            # print("spawn_point:")
            # print(spawn_point)
            # ********************************* end ***************************
            self.player = self.world.try_spawn_actor(blueprint, spawn_point1)
            # print(self.player.set_velocity(carla.Vector3D(x=-5.0, y=0.000000, z=0.000000)))
            spawn_point2 = Transform(
                Location(x=300, y=129.750000, z=1.320625),
                Rotation(pitch=0.000000, yaw=179.999756, roll=0.000000))

            # self.vehicle2 = self.world.spawn_actor(blueprint2, spawn_point2).set_autopilot(enabled=True)
            # print(self.vehicle2.get_velocity())
            self.vehicle2 = self.world.spawn_actor(blueprint2, spawn_point2)
            self.vehicle2.set_velocity(
                carla.Vector3D(x=-50.0, y=0.000000, z=0.000000))
            # spawn_point3 = Transform(Location(x=280, y=129.750000, z=1.320625),
            #                          Rotation(pitch=0.000000, yaw=179.999756, roll=0.000000))
            # batch.append(self.world.spawn_actor(blueprint2, spawn_point3))
            # batch[0].set_velocity(carla.Vector3D(x=-2.0, y=0.000000, z=0.000000))

        # Set up the sensors.
        self.collision_sensor = CollisionSensor(self.player, self.hud)
        self.lane_invasion_sensor = LaneInvasionSensor(self.player, self.hud)
        self.gnss_sensor = GnssSensor(self.player)
        self.camera_manager = CameraManager(self.player, self.hud)
        self.camera_manager.transform_index = cam_pos_index
        self.camera_manager.set_sensor(cam_index, notify=False)
        actor_type = get_actor_display_name(self.player)
        self.hud.notification(actor_type)
    def Create_actors(self,world, blueprint_library): 
        ego_list = []
        npc_list = []
        obstacle_list = []
        sensor_list = []
        # ego车辆设置---------------------------------------------------------------
        ego_bp = blueprint_library.find(id='vehicle.lincoln.mkz2017')
        # 坐标建立
        ego_transform = Transform(Location(x=9, y=-110.350967, z=0.1), 
                    Rotation(pitch=0, yaw=-90, roll=-0.000000))
        # 车辆从蓝图定义以及坐标生成
        ego = world.spawn_actor(ego_bp, ego_transform)
        ego_list.append(ego)
        print('created %s' % ego.type_id)

        # 视角设置------------------------------------------------------------------
        spectator = world.get_spectator()
        # spec_transform = ego.get_transform()
        spec_transform = Transform(Location(x=9, y=-115.350967, z=0), 
                    Rotation(pitch=0, yaw=180, roll=-0.000000))
        spec_transform.location += carla.Location(x=-5,z=60)
        spec_transform.rotation = carla.Rotation(pitch=-90,yaw=1.9,roll=-0.000000)
        spectator.set_transform(spec_transform)

        # npc设置--------------------------------------------------------------------
        npc_transform = Transform(Location(x=9, y=-110.350967, z=0.1), 
                    Rotation(pitch=0, yaw=-90, roll=-0.000000))
        for i in range(1):
            npc_transform.location += carla.Location(x=-18,y=-20.4)
            npc_transform.rotation = carla.Rotation(pitch=0,yaw=0, roll=-0.000000)
            npc_bp = blueprint_library.find(id='vehicle.lincoln.mkz2017')
            # print(npc_bp.get_attribute('color').recommended_values)
            npc_bp.set_attribute('color', '229,28,0')
            npc = world.try_spawn_actor(npc_bp, npc_transform)
            if npc is None:
                print('%s npc created failed' % i)
            else:
                npc_list.append(npc)
                print('created %s' % npc.type_id)

        # 障碍物设置------------------------------------------------------------------
        obsta_bp = blueprint_library.find(id='static.prop.streetbarrier')
        # 障碍物1
        obstacle_transform1 = Transform(Location(x=9, y=-110.350967,z=0), 
                    Rotation(pitch=0, yaw=-90, roll=-0.000000))
        obstacle_transform1.location += carla.Location(x=50,y=-21.5,z=3)
        obstacle_transform1.rotation = carla.Rotation(pitch=0, yaw=0, roll=0.000000)
        for i in range(30):
            obstacle1 = world.try_spawn_actor(obsta_bp, obstacle_transform1)
            obstacle_transform1.location += carla.Location(x=-2.5,y=-0.05,z=-0.1)
            obstacle_list.append(obstacle1)
        # 障碍物2  
        obstacle_transform2 = Transform(Location(x=9, y=-110.350967,z=0), 
                    Rotation(pitch=0, yaw=-90, roll=-0.000000))
        obstacle_transform2.location += carla.Location(x=-2.8,y=-18.5)
        obstacle_transform2.rotation = carla.Rotation(pitch=0, yaw=0, roll=0.000000)
        for i in range(7):
            obstacle2 = world.try_spawn_actor(obsta_bp, obstacle_transform2)
            obstacle_transform2.location += carla.Location(x=-2.5)
            obstacle_list.append(obstacle2)
        # 障碍物3
        obstacle_transform3 = Transform(Location(x=9, y=-110.350967,z=0), 
                    Rotation(pitch=0, yaw=-90, roll=-0.000000))
        obstacle_transform3.location += carla.Location(x=-1.65,y=-17.5)
        obstacle_transform3.rotation = carla.Rotation(pitch=0, yaw=90, roll=0.000000)
        for i in range(10):
            obstacle3 = world.try_spawn_actor(obsta_bp, obstacle_transform3)
            obstacle_transform3.location += carla.Location(x=0.006,y=2.5)
            obstacle_list.append(obstacle3)
        # 障碍物4
        obstacle_transform4 = Transform(Location(x=9, y=-110.350967,z=0), 
                    Rotation(pitch=0, yaw=-90, roll=-0.000000))
        obstacle_transform4.location += carla.Location(x=2.45,y=-15)
        obstacle_transform4.rotation = carla.Rotation(pitch=0, yaw=90, roll=0.000000)
        for i in range(10):
            obstacle4 = world.try_spawn_actor(obsta_bp, obstacle_transform4)
            obstacle_transform4.location += carla.Location(y=2.5)
            obstacle_list.append(obstacle4)

        # 传感器设置-------------------------------------------------------------------
        ego_collision = SS.CollisionSensor(ego)
        npc_collision = SS.CollisionSensor(npc)
        ego_invasion = SS.LaneInvasionSensor(ego)
        npc_invasion = SS.LaneInvasionSensor(npc)
        sensor_list.extend([[ego_collision,ego_invasion],[npc_collision,npc_invasion]])

        return ego_list,npc_list,obstacle_list,sensor_list
Example #7
0
            sensor_names = []
            sensor_type = str('sensor.camera.rgb')
            sensor_id = str(sensor_spec.pop("id"))

            sensor_name = sensor_type + "/" + sensor_id
            if sensor_name in sensor_names:
                raise NameError
            sensor_names.append(sensor_name)

            spawn_point = sensor_spec.pop("spawn_point")
            point = Transform(
                Location(x=spawn_point.pop("x"),
                         y=-spawn_point.pop("y"),
                         z=spawn_point.pop("z")),
                Rotation(pitch=-spawn_point.pop("pitch", 0.0),
                         yaw=-spawn_point.pop("yaw", 0.0),
                         roll=spawn_point.pop("roll", 0.0)))
            # camera_bp.set_attribute('sensor_id',str(sensor_id))

            camera_rgb = world.spawn_actor(camera_bp, point)
            #camera_seg = world.spawn_actor(semseg_bp,point)
            camera_rgb.sensor_name = sensor_id
            #camera_seg.sensor_name = sensor_id + "_seg"
            actor_list.append(camera_rgb)
            #actor_list.append(camera_seg)
            rgb_list.append(camera_rgb)
            print('spawned: ', camera_rgb.sensor_name)
        except RuntimeError as e:
            raise RuntimeError(
                "Setting up global sensors failed: {}".format(e))
    # Spawn finished!
Example #8
0
    def restart(self):
        # Keep same camera config if the camera manager exists.
        cam_index = self.camera_manager.index if self.camera_manager is not None else 0
        cam_pos_index = self.camera_manager.transform_index if self.camera_manager is not None else 0
        # Get a random blueprint.
        # ******I have made this random.choice just have one choice --> vehicle.bmw.grandtourer
        blueprint = random.choice(self.world.get_blueprint_library().filter(
            self._actor_filter))
        # print("blue print")
        # print(self.world.get_blueprint_library().filter(self._actor_filter))
        blueprint.set_attribute('role_name', 'hero')
        if blueprint.has_attribute('color'):
            color = random.choice(
                blueprint.get_attribute('color').recommended_values)
            # print("color")
            # print(blueprint.get_attribute('color').recommended_values)
            # ****** I have set the blueprint being white as '255,255,255'
            blueprint.set_attribute('color', '255,255,255')
        batch = []
        blueprint2 = random.choice(self.world.get_blueprint_library().filter(
            self._actor_filter))
        blueprint2.set_attribute('role_name', 'hero2')
        if blueprint2.has_attribute('color'):
            blueprint2.set_attribute('color', '0,0,0')
        # batch.append(SpawnActor(blueprint, transform))

        # Spawn the player.

        #     ******************* change positions ****************************************
        while self.player1 is None:
            # spawn_points = self.map.get_spawn_points()
            # print("length of sapwn_point is %d" % len(spawn_points) + ",just choose one")  # 257 just choose one
            x_rand = random.randint(18000, 23000)
            x_rand_v2 = random.randint(x_rand + 1000, 25000)
            x_rand = x_rand / 100.0
            x_rand_v2 = x_rand_v2 / 100.0
            y_rand = random.randint(12855, 13455)
            y_rand = y_rand / 100.0
            x_speed_randon_v2 = random.randint(100, 3000)
            if x_speed_randon_v2 - 1000 > 0:
                x_speed_player = x_speed_randon_v2 - 100

            else:
                x_speed_player = 0
            x_speed_player = x_speed_player / 100

            x_speed_randon_v2 = x_speed_randon_v2 / 100
            spawn_point1 = Transform(
                Location(x=x_rand, y=y_rand, z=1.320625),
                Rotation(pitch=0.000000, yaw=179.999756, roll=0.000000))
            # ********************************* end *****************************************
            self.player1 = self.world.try_spawn_actor(blueprint, spawn_point1)
            spawn_point2 = Transform(
                Location(x=x_rand_v2, y=y_rand, z=1.320625),
                Rotation(pitch=0.000000, yaw=179.999756, roll=0.000000))
            if self.vehicle2 is None:
                # print("*****************")
                self.vehicle2 = self.world.try_spawn_actor(
                    blueprint2, spawn_point2)
                self.vehicle2.set_velocity(
                    carla.Vector3D(x=-x_speed_randon_v2, y=0.00000,
                                   z=0.000000))
                self.player1.set_velocity(
                    carla.Vector3D(x=-x_speed_player, y=0.00000, z=0.00000))

        # Set up the sensors.
        self.collision_sensor = CollisionSensor(self.player1, self.hud)
        self.lane_invasion_sensor = LaneInvasionSensor(self.player1, self.hud)
        self.gnss_sensor = GnssSensor(self.player1)
        self.camera_manager = CameraManager(self.player1, self.hud)
        self.camera_manager.transform_index = cam_pos_index
        self.camera_manager.set_sensor(cam_index, notify=False)
        actor_type = get_actor_display_name(self.player1)
        self.hud.notification(actor_type)
def main(weather_num):
    img_size = np.asarray([960,540],dtype=np.int)
    actor_list = []
    rgb_list = []
    pygame.init()

    display = pygame.display.set_mode((VIEW_WIDTH, VIEW_HEIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF)
    font = get_font()
    clock = pygame.time.Clock()

    client = carla.Client('localhost', 2000,1)
    client.set_timeout(60.0)
    # cam_subset=1

    world = client.get_world()
    # weather = carla.WeatherParameters(
    #     cloudiness=0.0,
    #     precipitation=0.0,
    #     sun_altitude_angle=50.0)

    
    #world.set_weather(all_weather[weather_num])
    world.set_weather(getattr(carla.WeatherParameters, "ClearNoon"))
    
    
    # 加入其他车辆
    #Spawn_the_vehicles(world,client,car_num[0])
    ###########相机参数
    # cam_type='cam1'
    
    # with open("/media/wuminghu/hard/hard/carla/cam.txt") as csv_file:
    #     reader = csv.reader(csv_file, delimiter=' ')
    #     for line, row in enumerate(reader):
    #         if row[0] in cam_type:
    #             w_x,w_y,w_yaw,cam_H,cam_pitch=[float(i) for i in row[1:]]
    #             break
    ###########相机参数
    # w_x,w_y,w_yaw,cam_H,cam_pitch=-116,100,100,4,90

    sensors_definition_file = '/home/ubuntu/xwp/CenterNet/carla_ros/dataset.json'
    if not os.path.exists(sensors_definition_file):
        raise RuntimeError(
            "Could not read sensor-definition from {}".format(sensors_definition_file))

    with open(sensors_definition_file) as handle:
        json_actors = json.loads(handle.read())
    
    global_sensors = []
    for actor in json_actors["objects"]:
        global_sensors.append(actor)
    
    camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')
    sizex = str(960)
    sizey = str(540)
    fovcfg = str(90)
    camera_bp.set_attribute('image_size_x',sizex )
    camera_bp.set_attribute('image_size_y', sizey)
    camera_bp.set_attribute('fov', fovcfg)
    semseg_bp = world.get_blueprint_library().find('sensor.camera.semantic_segmentation')
    semseg_bp.set_attribute('image_size_x', sizex)
    semseg_bp.set_attribute('image_size_y', sizey)
    semseg_bp.set_attribute('fov', fovcfg)

    for sensor_spec in global_sensors:
        try:
            sensor_names = []
            sensor_type = str('sensor.camera.rgb')
            sensor_id = str(sensor_spec.pop("id"))

            sensor_name = sensor_type + "/" + sensor_id
            if sensor_name in sensor_names:
                raise NameError
            sensor_names.append(sensor_name)

            spawn_point = sensor_spec.pop("spawn_point")
            point = Transform(Location(x=spawn_point.pop("x"), y=-spawn_point.pop("y"), z=spawn_point.pop("z")),
                 Rotation(pitch=-spawn_point.pop("pitch", 0.0), yaw=-spawn_point.pop("yaw", 0.0), roll=spawn_point.pop("roll", 0.0)))
            # camera_bp.set_attribute('sensor_id',str(sensor_id))

            camera_rgb = world.spawn_actor(camera_bp,point)
            #camera_seg = world.spawn_actor(semseg_bp,point)
            camera_rgb.sensor_name = sensor_id
            #camera_seg.sensor_name = sensor_id + "_seg"  
            actor_list.append(camera_rgb)
            #actor_list.append(camera_seg)
            rgb_list .append(camera_rgb)
            print('spawned: ',camera_rgb.sensor_name)
        except RuntimeError as e:
            raise RuntimeError("Setting up global sensors failed: {}".format(e))

    out_path="/home/ubuntu/xwp/datasets/multi_view_dataset/new"
    if not os.path.exists(out_path):
        os.makedirs(out_path)

    vehicles_actor = Spawn_the_vehicles(world,client,car_num[0])
    ######################################################file
    with CarlaSyncMode(world, *actor_list, fps=20) as sync_mode:
        count=0
        k=0
        while count<10000:
        # while count<100:
            count+=1
            if should_quit():
                return
            clock.tick()
            # Advance the simulation and wait for the data.
            #snapshot, image_rgb,image_semseg = sync_mode.tick(timeout=2.0)
            blobs = sync_mode.tick(timeout=2.0)
            if count%10!=0:
                continue
            k+=1
            print(k)

            # img.append(image_rgb)
            #image=image_rgb
            # image1=image_semseg.convert(ColorConverter.CityScapesPalette)
            # import pdb; pdb.set_trace()
            all_images = blobs[1:]
            images = all_images
            #images = all_images[::2]
            #semseg_images = all_images[1::2]
            snapshot = blobs[0]
            fps = round(1.0 / snapshot.timestamp.delta_seconds)

            
            world_snapshot = world.get_snapshot()

            actual_actor=[world.get_actor(actor_snapshot.id) for actor_snapshot in world_snapshot]
            got_vehicles=[actor for actor in actual_actor if actor.type_id.find('vehicle')!=-1]

            vehicles_list = []
            for camera_rgb in rgb_list:
                vehicles=[vehicle for vehicle in got_vehicles if vehicle.get_transform().location.distance(camera_rgb.get_transform().location)<65]
                vehicles_list.append(vehicles)
                cam_path = out_path + "/{}".format(camera_rgb.sensor_name)
                if not os.path.exists(cam_path):
                    os.makedirs(cam_path)
                    os.makedirs(cam_path+"/label_2")
                    os.makedirs(cam_path+"/image_2")
                    os.makedirs(cam_path+"/calib")
            # debug = world.debug
            # for vehicle in vehicles:
            #     debug.draw_box(carla.BoundingBox(vehicle.get_transform().location+vehicle.bounding_box.location, vehicle.bounding_box.extent), vehicle.get_transform().rotation, 0.05, carla.Color(255,0,0,0), life_time=0.05)
            # import pdb; pdb.set_trace()
            all_vehicles_list = []
            
            for i,(camera_rgb, vehicles,image) in enumerate(zip(rgb_list,vehicles_list,images)):
                v=[] #filtered_vehicles

                # Convert semseg to cv.image
                # semseg = np.frombuffer(osemseg.raw_data, dtype=np.dtype("uint8"))
                # semseg = np.reshape(semseg, (osemseg.height, osemseg.width, 4))
                # semseg = semseg[:, :, :3]
                # semseg = semseg[:, :, ::-1]
                # BGR
                # Done

                label_files=open("{}/{}/label_2/{:0>6d}.txt".format(out_path,camera_rgb.sensor_name,k), "w")
                car_2d_bbox=[]
                for car in vehicles:
                    extent = car.bounding_box.extent
                    ###############location
                    car_location =car.bounding_box.location
                    # car_location1=car.get_transform().location
                    cords = np.zeros((1, 4))
                    cords[0, :]=np.array([car_location.x,car_location.y,car_location.z, 1])
                    cords_x_y_z = ClientSideBoundingBoxes._vehicle_to_sensor(cords, car, camera_rgb)[:3, :]
                    cords_y_minus_z_x = np.concatenate([cords_x_y_z[1, :], -cords_x_y_z[2, :], cords_x_y_z[0, :]])
                    ###############location
                    bbox = np.transpose(np.dot(camera_coordinate(camera_rgb), cords_y_minus_z_x))
                    camera_bbox = (np.concatenate([bbox[:, 0] / bbox[:, 2], bbox[:, 1] / bbox[:, 2], bbox[:, 2]], axis=1))
                    # pdb.set_trace()
                    camera_bbox_e = camera_bbox[0]
                    # if camera_bbox_e[:,0]<0 or camera_bbox_e[:,1]<0 or camera_bbox_e[:,2]<0:
                    #     continue
                    if camera_bbox_e[:,2]<0:
                        continue
                    
                    #bboxe = camera_bbox
                    xmin,ymin,xmax,ymax=0,0,0,0
                    bboxe = ClientSideBoundingBoxes.get_bounding_boxes([car], camera_rgb)
                    if len(bboxe)==0:
                        continue
                    bboxe=bboxe[0]
                    t_points = [(int(bboxe[i, 0]), int(bboxe[i, 1])) for i in range(8)]
                    width_x=[int(bboxe[i, 0]) for i in range(8)]
                    high_y=[int(bboxe[i, 1]) for i in range(8)]
                    xmin,ymin,xmax,ymax=min(width_x),min(high_y),max(width_x),max(high_y)
                    x_cen=(xmin+xmax)/2
                    y_cen=(ymin+ymax)/2
                    if x_cen<0 or y_cen<0 or x_cen>VIEW_WIDTH or y_cen>VIEW_HEIGHT:
                        continue
                    car_type, truncated, occluded, alpha= 'Car', 0, 0,0
                    dh, dw,dl=extent.z*2,extent.y*2,extent.x*2
                    cords_y_minus_z_x=np.array(cords_y_minus_z_x)
                    ly, lz,lx=cords_y_minus_z_x[0][0],cords_y_minus_z_x[1][0],cords_y_minus_z_x[2][0]
                    lz=lz+dh
                    ry=(car.get_transform().rotation.yaw-camera_rgb.get_transform().rotation.yaw+90)*np.pi/180
                    check_box=False
                    ofs = 3
                    for one_box in car_2d_bbox:
                        xmi,ymi,xma,yma=one_box
                        if xmin>xmi-ofs and ymin>ymi-ofs and xmax<xma+ofs and ymax<yma+ofs:
                            check_box=True
                    if check_box or np.sqrt(ly**2+lx**2)<3:
                        continue

                    bbox_crop = tuple(max(0, b) for b in [xmin,ymin,xmax,ymax])
                    bbox_crop = (min(img_size[0], bbox_crop[0]),
                                min(img_size[1], bbox_crop[1]),
                                min(img_size[0], bbox_crop[2]),
                                min(img_size[1], bbox_crop[3]))
                    # Use segment image to determine whether the vehicle is occluded.
                    # See https://carla.readthedocs.io/en/0.9.11/ref_sensors/#semantic-segmentation-camera
                    # print('seg: ',semseg[int((ymin+ymax)/2),int((xmin+xmax)/2),0])
                    # print('x: ',int((ymin+ymax)/2),'y: ',int((xmin+xmax)/2))
                    # if semseg[int((ymin+ymax)/2),int((xmin+xmax)/2),0] != 10:
                    #     continue

                    car_2d_bbox.append(bbox_crop)
                    
                    v.append(car)
                    if car not in all_vehicles_list:
                        all_vehicles_list.append(car)
                    if ry>np.pi:
                        ry-=np.pi
                    if ry<-np.pi:
                        ry+=np.pi
                    # pdb.set_trace()
                    txt="{} {} {} {} {} {} {} {} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {} {}\n".format(car_type, truncated, occluded, alpha, xmin, ymin, xmax, ymax, dh, dw,
                        dl,ly, lz, lx,  ry,car.id)
                    label_files.write(txt)
                label_files.close()
                #print(cam_type,cam_subset,k,len(v))

                #bounding_boxes = ClientSideBoundingBoxes.get_bounding_boxes(v, camera_rgb) 
                ######################################################file
                # pygame.image.save(display,'%s/image_1/%06d.png' % (out_path,k))       
                image.save_to_disk('{}/{}/image_2/{:0>6d}.png'.format (out_path,camera_rgb.sensor_name,k))        
            
            global_file_path = out_path + '/global_label_2'
            if not os.path.exists(global_file_path):
                os.makedirs(global_file_path)
            global_vehicle_file = open("{}/{:0>6d}.txt".format(global_file_path,k), "w")
            for vehicle in all_vehicles_list:
                extent = vehicle.bounding_box.extent
                car_type, truncated, occluded, alpha,xmin,ymin,xmax,ymax= 'Car', 0, 0,0,0,0,0,0
                dh, dw,dl=extent.z*2,extent.y*2,extent.x*2
                location  = vehicle.get_transform().location
                ly,lz,lx = location.x,location.y,location.z
                ry = vehicle.get_transform().rotation.yaw
                
                txt="{} {} {} {} {} {} {} {} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {} {}\n".format(car_type, truncated, occluded, alpha, xmin, ymin, xmax, ymax, dh, dw,
                            dl,ly, lz, lx,  ry,vehicle.id)
                global_vehicle_file.write(txt)
            global_vehicle_file.close()


    for camera_rgb in rgb_list:
        calib_files=open("{}/{}/calib/{:0>6d}.txt".format(out_path,camera_rgb.sensor_name,0), "w")
        K=camera_coordinate(camera_rgb)

        txt="P2: {} {} {} {} {} {} {} {} {}\n".format(  K[0][0],K[0][1],K[0][2],
                                                    K[1][0],K[1][1],K[1][2],
                                                    K[2][0],K[2][1],K[2][2])
        calib_files.write(txt)
        calib_files.close()


    print('destroying actors.')
    for actor in actor_list:
        actor.destroy()
    
    for vehicle in vehicles_actor:
        vehicle.destory()

    pygame.quit()

    return
Example #10
0
def main(args):
    """ The main function that orchestrates the setup of the world, connection
    to the scenario and the subsequent logging of the frames for computation
    of the mIoU.

    Args:
        args: The argparse.Namespace instance that is retrieved from parsing
        the arguments.
    """

    # Setup the world.
    world = setup_world(args.host, args.port, args.delta)

    # Retrieve the ego-vehicle from the simulation.
    ego_vehicle = None
    while ego_vehicle is None:
        print("Waiting for the scenario to be ready ...")
        sleep(1)
        ego_vehicle = retrieve_actor(world, 'vehicle.*', 'hero')
        world.tick()

    # Transform of the cameras.
    camera_transform = Transform(location=Location(1.0, 0.0, 1.8),
                                 rotation=Rotation(0, 0, 0))

    # Connect the RGB camera to the vehicle.
    rgb_camera = spawn_camera('sensor.camera.rgb', camera_transform,
                              ego_vehicle, *args.res.split('x'))

    # Connect the Semantic segmentation camera to the vehicle.
    semantic_camera = spawn_camera('sensor.camera.semantic_segmentation',
                                   camera_transform, ego_vehicle,
                                   *args.res.split('x'))

    # Connect the depth camera to the vehicle.
    depth_camera = spawn_camera('sensor.camera.depth', camera_transform,
                                ego_vehicle, *args.res.split('x'))

    # Open the CSV file for writing.
    csv_file = open(args.output, 'w')
    csv_writer = csv.writer(csv_file)

    # Create the cleanup function.
    global CLEANUP_FUNCTION
    CLEANUP_FUNCTION = functools.partial(
        cleanup_function,
        world=world,
        cameras=[rgb_camera, semantic_camera, depth_camera],
        csv_file=csv_file)

    # Create a PyGame surface for debugging purposes.
    width, height = map(int, args.res.split('x'))
    surface = None
    if args.visualize:
        surface = pygame.display.set_mode((width, height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

    # Register a callback function with the camera.
    rgb_camera.listen(process_rgb_images)
    semantic_camera.listen(process_semantic_images)

    depth_camera_setup = DepthCameraSetup(
        "depth_camera", width, height,
        pylot.utils.Transform.from_simulator_transform(camera_transform))
    depth_camera.listen(
        functools.partial(process_depth_images,
                          depth_camera_setup=depth_camera_setup,
                          ego_vehicle=ego_vehicle,
                          speed=args.speed,
                          csv=csv_writer,
                          surface=surface,
                          visualize=args.visualize))

    try:
        # To keep the thread alive so that the images can be processed.
        while True:
            pass
    except KeyboardInterrupt:
        CLEANUP_FUNCTION()
        sys.exit(0)
Example #11
0
 def restart(self):
     # Keep same camera config if the camera manager exists.
     cam_index = self.camera_manager.index if self.camera_manager is not None else 0
     cam_pos_index = self.camera_manager.transform_index if self.camera_manager is not None else 0
     # Get a random blueprint.
     # blueprint = random.choice(self.world.get_blueprint_library().filter(self._actor_filter))
     blueprint = random.choice(self.world.get_blueprint_library().filter('toyota'))
     blueprint.set_attribute('role_name', self.actor_role_name)
     if blueprint.has_attribute('color'):
         # color = random.choice(blueprint.get_attribute('color').recommended_values)
         # color = "255,255,255"
         color = self.color
         blueprint.set_attribute('color', color)
     # Spawn the player.
     if self.player is not None:
         print("this is the playert not none")
         spawn_point = self.player.get_transform()
         spawn_point.location.z += 2.0
         spawn_point.rotation.roll = 0.0
         spawn_point.rotation.pitch = 0.0
         self.destroy()
         self.player = self.world.try_spawn_actor(blueprint, spawn_point)
     while self.player is None:
         # def ego_spawn_point(points):
         #     for pt in points:
         #         if pt.location.x < 69 and pt.location.x > 68 and pt.location.y > 6 and pt.location.y < 7:
         #             return pt
         # def hero_spawn_point(points):
         #     for pt in points:
         #         if pt.location.x < 69 and pt.location.x > 68 and pt.location.y > 7.5 and pt.location.y < 10:
         #             return pt
         # spawn_points = self.map.get_spawn_points()
         # spawn_point = random.choice(spawn_points) if spawn_points else carla.Transform()
         # if self.actor_role_name == "hero":
         #     spawn_point = hero_spawn_point(spawn_points)
         # elif self.actor_role_name == "ego_vehicle":
         #     spawn_point = ego_spawn_point(spawn_points)
         # print(" ========================================= ")
         # for i in range(10):
         #     print(" these are all the available spawn points ", spawn_points[i])
         # spawn_point = spawn_points[179]
         spawn_point = Transform(Location(x=self.init_positionx, y=self.init_positiony, z=2.5), Rotation(pitch=1, yaw=180, roll=1))
         self.player = self.world.try_spawn_actor(blueprint, spawn_point)
     # Set up the sensors.
     self.collision_sensor = CollisionSensor(self.player, self.hud)
     self.lane_invasion_sensor = LaneInvasionSensor(self.player, self.hud)
     self.gnss_sensor = GnssSensor(self.player)
     self.camera_manager = CameraManager(self.player, self.hud)
     self.camera_manager.transform_index = cam_pos_index
     self.camera_manager.set_sensor(cam_index, notify=False)
     actor_type = get_actor_display_name(self.player)
     self.hud.notification(actor_type)