| 477 | |
| 478 | |
| 479 | class CameraManager(object): |
| 480 | def __init__(self, parent_actor, hud): |
| 481 | self.sensor = None |
| 482 | self.surface = None |
| 483 | self._parent = parent_actor |
| 484 | self.hud = hud |
| 485 | self.recording = False |
| 486 | self._camera_transforms = [ |
| 487 | carla.Transform(carla.Location(x=-5.5, z=2.8), carla.Rotation(pitch=-15)), |
| 488 | carla.Transform(carla.Location(x=1.6, z=1.7))] |
| 489 | self.transform_index = 1 |
| 490 | self.sensors = [ |
| 491 | ['sensor.camera.rgb', cc.Raw, 'Camera RGB'], |
| 492 | ['sensor.camera.depth', cc.Raw, 'Camera Depth (Raw)'], |
| 493 | ['sensor.camera.depth', cc.Depth, 'Camera Depth (Gray Scale)'], |
| 494 | ['sensor.camera.depth', cc.LogarithmicDepth, 'Camera Depth (Logarithmic Gray Scale)'], |
| 495 | ['sensor.camera.semantic_segmentation', cc.Raw, 'Camera Semantic Segmentation (Raw)'], |
| 496 | ['sensor.camera.semantic_segmentation', cc.CityScapesPalette, |
| 497 | 'Camera Semantic Segmentation (CityScapes Palette)'], |
| 498 | ['sensor.lidar.ray_cast', None, 'Lidar (Ray-Cast)']] |
| 499 | world = self._parent.get_world() |
| 500 | bp_library = world.get_blueprint_library() |
| 501 | for item in self.sensors: |
| 502 | bp = bp_library.find(item[0]) |
| 503 | if item[0].startswith('sensor.camera'): |
| 504 | bp.set_attribute('image_size_x', str(hud.dim[0])) |
| 505 | bp.set_attribute('image_size_y', str(hud.dim[1])) |
| 506 | elif item[0].startswith('sensor.lidar'): |
| 507 | bp.set_attribute('range', '50') |
| 508 | item.append(bp) |
| 509 | self.index = None |
| 510 | |
| 511 | def toggle_camera(self): |
| 512 | self.transform_index = (self.transform_index + 1) % len(self._camera_transforms) |
| 513 | self.sensor.set_transform(self._camera_transforms[self.transform_index]) |
| 514 | |
| 515 | def set_sensor(self, index, notify=True): |
| 516 | index = index % len(self.sensors) |
| 517 | needs_respawn = True if self.index is None \ |
| 518 | else self.sensors[index][0] != self.sensors[self.index][0] |
| 519 | if needs_respawn: |
| 520 | if self.sensor is not None: |
| 521 | self.sensor.destroy() |
| 522 | self.surface = None |
| 523 | self.sensor = self._parent.get_world().spawn_actor( |
| 524 | self.sensors[index][-1], |
| 525 | self._camera_transforms[self.transform_index], |
| 526 | attach_to=self._parent) |
| 527 | # We need to pass the lambda a weak reference to self to avoid |
| 528 | # circular reference. |
| 529 | weak_self = weakref.ref(self) |
| 530 | self.sensor.listen(lambda image: CameraManager._parse_image(weak_self, image)) |
| 531 | if notify: |
| 532 | self.hud.notification(self.sensors[index][2]) |
| 533 | self.index = index |
| 534 | |
| 535 | def next_sensor(self): |
| 536 | self.set_sensor(self.index + 1) |