| 668 | |
| 669 | |
| 670 | class CameraManager(object): |
| 671 | def __init__(self, parent_actor, hud): |
| 672 | self.sensor = None |
| 673 | self.surface = None |
| 674 | self._parent = parent_actor |
| 675 | self.hud = hud |
| 676 | self.recording = False |
| 677 | self._camera_transforms = [ |
| 678 | carla.Transform(carla.Location(x=-5.5, z=2.8), carla.Rotation(pitch=-15)), |
| 679 | carla.Transform(carla.Location(x=1.6, z=1.7))] |
| 680 | self.transform_index = 1 |
| 681 | self.sensors = [ |
| 682 | ['sensor.camera.rgb', cc.Raw, 'Camera RGB'], |
| 683 | ['sensor.camera.depth', cc.Raw, 'Camera Depth (Raw)'], |
| 684 | ['sensor.camera.depth', cc.Depth, 'Camera Depth (Gray Scale)'], |
| 685 | ['sensor.camera.depth', cc.LogarithmicDepth, 'Camera Depth (Logarithmic Gray Scale)'], |
| 686 | ['sensor.camera.semantic_segmentation', cc.Raw, 'Camera Semantic Segmentation (Raw)'], |
| 687 | ['sensor.camera.semantic_segmentation', cc.CityScapesPalette, |
| 688 | 'Camera Semantic Segmentation (CityScapes Palette)'], |
| 689 | ['sensor.lidar.ray_cast', None, 'Lidar (Ray-Cast)']] |
| 690 | world = self._parent.get_world() |
| 691 | bp_library = world.get_blueprint_library() |
| 692 | for item in self.sensors: |
| 693 | bp = bp_library.find(item[0]) |
| 694 | if item[0].startswith('sensor.camera'): |
| 695 | bp.set_attribute('image_size_x', str(hud.dim[0])) |
| 696 | bp.set_attribute('image_size_y', str(hud.dim[1])) |
| 697 | elif item[0].startswith('sensor.lidar'): |
| 698 | bp.set_attribute('range', '50') |
| 699 | item.append(bp) |
| 700 | self.index = None |
| 701 | |
| 702 | def toggle_camera(self): |
| 703 | self.transform_index = (self.transform_index + 1) % len(self._camera_transforms) |
| 704 | self.sensor.set_transform(self._camera_transforms[self.transform_index]) |
| 705 | |
| 706 | def set_sensor(self, index, notify=True): |
| 707 | index = index % len(self.sensors) |
| 708 | needs_respawn = True if self.index is None \ |
| 709 | else self.sensors[index][0] != self.sensors[self.index][0] |
| 710 | if needs_respawn: |
| 711 | if self.sensor is not None: |
| 712 | self.sensor.destroy() |
| 713 | self.surface = None |
| 714 | self.sensor = self._parent.get_world().spawn_actor( |
| 715 | self.sensors[index][-1], |
| 716 | self._camera_transforms[self.transform_index], |
| 717 | attach_to=self._parent) |
| 718 | # We need to pass the lambda a weak reference to self to avoid |
| 719 | # circular reference. |
| 720 | weak_self = weakref.ref(self) |
| 721 | self.sensor.listen(lambda image: CameraManager._parse_image(weak_self, image)) |
| 722 | if notify: |
| 723 | self.hud.notification(self.sensors[index][2]) |
| 724 | self.index = index |
| 725 | |
| 726 | def next_sensor(self): |
| 727 | self.set_sensor(self.index + 1) |