Skip to content

Camera Manager

CameraManager ¤

__init__(self, parent_actor, hud, gamma_correction) special ¤

Source code in carla_client/util/camera_manager.py
def __init__(self, parent_actor, hud, gamma_correction):
    self.sensor = None
    self.surface = None
    self._parent = parent_actor
    self.hud = hud
    self.recording = False
    self.logger = logging.getLogger("Camera Manager")
    bound_y = 0.5 + self._parent.bounding_box.extent.y
    Attachment = carla.AttachmentType
    self._camera_transforms = [
        (
            carla.Transform(
                carla.Location(x=-5.5, z=2.5), carla.Rotation(pitch=8.0)
            ),
            Attachment.SpringArm,
        ),
        (carla.Transform(carla.Location(x=1.6, z=1.7)), Attachment.Rigid),
        (
            carla.Transform(carla.Location(x=5.5, y=1.5, z=1.5)),
            Attachment.SpringArm,
        ),
        (
            carla.Transform(
                carla.Location(x=-8.0, z=6.0), carla.Rotation(pitch=6.0)
            ),
            Attachment.SpringArm,
        ),
        (
            carla.Transform(carla.Location(x=-1, y=-bound_y, z=0.5)),
            Attachment.Rigid,
        ),
    ]
    self.transform_index = 1
    self.sensors = [
        ["sensor.camera.rgb", cc.Raw, "Camera RGB", {}],
        ["sensor.camera.depth", cc.Raw, "Camera Depth (Raw)", {}],
        ["sensor.camera.depth", cc.Depth, "Camera Depth (Gray Scale)", {}],
        [
            "sensor.camera.depth",
            cc.LogarithmicDepth,
            "Camera Depth (Logarithmic Gray Scale)",
            {},
        ],
        [
            "sensor.camera.semantic_segmentation",
            cc.Raw,
            "Camera Semantic Segmentation (Raw)",
            {},
        ],
        [
            "sensor.camera.semantic_segmentation",
            cc.CityScapesPalette,
            "Camera Semantic Segmentation (CityScapes Palette)",
            {},
        ],
        ["sensor.lidar.ray_cast", None, "Lidar (Ray-Cast)", {}],
        ["sensor.camera.dvs", cc.Raw, "Dynamic Vision Sensor", {}],
        [
            "sensor.camera.rgb",
            cc.Raw,
            "Camera RGB Distorted",
            {
                "lens_circle_multiplier": "3.0",
                "lens_circle_falloff": "3.0",
                "chromatic_aberration_intensity": "0.5",
                "chromatic_aberration_offset": "0",
            },
        ],
    ]
    world = self._parent.get_world()
    bp_library = world.get_blueprint_library()
    for item in self.sensors:
        bp = bp_library.find(item[0])
        if item[0].startswith("sensor.camera"):
            bp.set_attribute("image_size_x", str(hud.dim[0]))
            bp.set_attribute("image_size_y", str(hud.dim[1]))
            if bp.has_attribute("gamma"):
                bp.set_attribute("gamma", str(gamma_correction))
            for attr_name, attr_value in item[3].items():
                bp.set_attribute(attr_name, attr_value)
        elif item[0].startswith("sensor.lidar"):
            bp.set_attribute("range", "50")
        item.append(bp)
    self.index = None

next_sensor(self) ¤

Source code in carla_client/util/camera_manager.py
def next_sensor(self):
    self.set_sensor(self.index + 1)

render(self, display) ¤

Source code in carla_client/util/camera_manager.py
def render(self, display):
    if self.surface is not None:
        display.blit(self.surface, (0, 0))

set_sensor(self, index, notify=True, force_respawn=False) ¤

Source code in carla_client/util/camera_manager.py
def set_sensor(self, index, notify=True, force_respawn=False):
    index = index % len(self.sensors)
    needs_respawn = (
        True
        if self.index is None
        else (
            force_respawn or (self.sensors[index][2] != self.sensors[self.index][2])
        )
    )
    if needs_respawn:
        if self.sensor is not None:
            self.sensor.destroy()
            self.surface = None
        self.sensor = self._parent.get_world().spawn_actor(
            self.sensors[index][-1],
            self._camera_transforms[self.transform_index][0],
            attach_to=self._parent,
            attachment_type=self._camera_transforms[self.transform_index][1],
        )
        # We need to pass the lambda a weak reference to self to avoid
        # circular reference.
        weak_self = weakref.ref(self)
        self.sensor.listen(
            lambda image: CameraManager._parse_image(weak_self, image)
        )
    if notify:
        self.hud.notification(self.sensors[index][2])
    self.index = index

toggle_camera(self) ¤

Source code in carla_client/util/camera_manager.py
def toggle_camera(self):
    self.transform_index = (self.transform_index + 1) % len(self._camera_transforms)
    self.set_sensor(self.index, notify=False, force_respawn=True)

toggle_recording(self) ¤

Source code in carla_client/util/camera_manager.py
def toggle_recording(self):
    self.recording = not self.recording
    self.hud.notification("Recording %s" % ("On" if self.recording else "Off"))