| 875 | |
| 876 | |
| 877 | class CameraManager(object): |
| 878 | def __init__(self, parent_actor, hud, gamma_correction): |
| 879 | self.sensor = None |
| 880 | self.surface = None |
| 881 | self._parent = parent_actor |
| 882 | self.hud = hud |
| 883 | self.recording = False |
| 884 | bound_y = 0.5 + self._parent.bounding_box.extent.y |
| 885 | Attachment = carla.AttachmentType |
| 886 | self._camera_transforms = [ |
| 887 | (carla.Transform(carla.Location(x=-5.5, z=2.5), carla.Rotation(pitch=8.0)), Attachment.SpringArm), |
| 888 | (carla.Transform(carla.Location(x=1.6, z=1.7)), Attachment.Rigid), |
| 889 | (carla.Transform(carla.Location(x=5.5, y=1.5, z=1.5)), Attachment.SpringArm), |
| 890 | (carla.Transform(carla.Location(x=-8.0, z=6.0), carla.Rotation(pitch=6.0)), Attachment.SpringArm), |
| 891 | (carla.Transform(carla.Location(x=-1, y=-bound_y, z=0.5)), Attachment.Rigid)] |
| 892 | self.transform_index = 1 |
| 893 | self.sensors = [ |
| 894 | ['sensor.camera.rgb', cc.Raw, 'Camera RGB', {}], |
| 895 | ['sensor.camera.depth', cc.Raw, 'Camera Depth (Raw)', {}], |
| 896 | ['sensor.camera.depth', cc.Depth, 'Camera Depth (Gray Scale)', {}], |
| 897 | ['sensor.camera.depth', cc.LogarithmicDepth, 'Camera Depth (Logarithmic Gray Scale)', {}], |
| 898 | ['sensor.camera.semantic_segmentation', cc.Raw, 'Camera Semantic Segmentation (Raw)', {}], |
| 899 | ['sensor.camera.semantic_segmentation', cc.CityScapesPalette, |
| 900 | 'Camera Semantic Segmentation (CityScapes Palette)', {}], |
| 901 | ['sensor.lidar.ray_cast', None, 'Lidar (Ray-Cast)', {}], |
| 902 | ['sensor.camera.rgb', cc.Raw, 'Camera RGB Distorted', |
| 903 | {'lens_circle_multiplier': '3.0', |
| 904 | 'lens_circle_falloff': '3.0', |
| 905 | 'chromatic_aberration_intensity': '0.5', |
| 906 | 'chromatic_aberration_offset': '0'}]] |
| 907 | world = self._parent.get_world() |
| 908 | bp_library = world.get_blueprint_library() |
| 909 | for item in self.sensors: |
| 910 | bp = bp_library.find(item[0]) |
| 911 | if item[0].startswith('sensor.camera'): |
| 912 | bp.set_attribute('image_size_x', str(hud.dim[0])) |
| 913 | bp.set_attribute('image_size_y', str(hud.dim[1])) |
| 914 | if bp.has_attribute('gamma'): |
| 915 | bp.set_attribute('gamma', str(gamma_correction)) |
| 916 | for attr_name, attr_value in item[3].items(): |
| 917 | bp.set_attribute(attr_name, attr_value) |
| 918 | elif item[0].startswith('sensor.lidar'): |
| 919 | bp.set_attribute('range', '50') |
| 920 | item.append(bp) |
| 921 | self.index = None |
| 922 | |
| 923 | def toggle_camera(self): |
| 924 | self.transform_index = (self.transform_index + 1) % len(self._camera_transforms) |
| 925 | self.set_sensor(self.index, notify=False, force_respawn=True) |
| 926 | |
| 927 | def set_sensor(self, index, notify=True, force_respawn=False): |
| 928 | index = index % len(self.sensors) |
| 929 | needs_respawn = True if self.index is None else \ |
| 930 | (force_respawn or (self.sensors[index][2] != self.sensors[self.index][2])) |
| 931 | if needs_respawn: |
| 932 | if self.sensor is not None: |
| 933 | self.sensor.destroy() |
| 934 | self.surface = None |