1"""
2Module to add high-level semantic return types for obstacle and traffic light detection results via named tuples.
3
4The code is compatible with Python 2.7, <3.6 and >=3.6. The later uses the typed version of named tuples.
5"""
6
7
8# Modern Python 3.6+ syntax for better type hinting
9
10from typing import NamedTuple, Optional, Union
11from typing_extensions import Literal
12
13import carla
14
15# Carla code with compatibility
16'''
17import sys
18if sys.version_info < (3, 6):
19 from collections import namedtuple
20 ObstacleDetectionResult = namedtuple('ObstacleDetectionResult', ['obstacle_was_found', 'obstacle', 'distance'])
21 TrafficLightDetectionResult = namedtuple('TrafficLightDetectionResult', ['traffic_light_was_found', 'traffic_light'])
22else:
23 from typing import NamedTuple, Union, TYPE_CHECKING
24 from carla import Actor, TrafficLight
25 """
26 # Python 3.6+, incompatible with Python 2.7 syntax
27 class ObstacleDetectionResult(NamedTuple):
28 obstacle_was_found : bool
29 obstacle : Union[Actor, None]
30 distance : float
31 # distance : Union[float, Literal[-1]] # Python 3.8+ only
32
33 class TrafficLightDetectionResult(NamedTuple):
34 traffic_light_was_found : bool
35 traffic_light : Union[TrafficLight, None]
36 """
37 if TYPE_CHECKING:
38 from typing import Literal
39 ObstacleDetectionResult = NamedTuple('ObstacleDetectionResult', [('obstacle_was_found', bool), ('obstacle', Union[Actor, None]), ('distance', Union[float, Literal[-1]])])
40 else:
41 ObstacleDetectionResult = NamedTuple('ObstacleDetectionResult', [('obstacle_was_found', bool), ('obstacle', Union[Actor, None]), ('distance', float)])
42
43 TrafficLightDetectionResult = NamedTuple('TrafficLightDetectionResult', [('traffic_light_was_found', bool), ('traffic_light', Union[TrafficLight, None])])
44'''
45
46
[docs]
47class TrafficLightDetectionResult(NamedTuple):
48 traffic_light_was_found: bool
49 """"""
50 traffic_light: Optional[carla.TrafficLight]
51 """The found traffic light. If no traffic light was found, the value is None."""
52
[docs]
53 def __bool__(self):
54 """
55 Returns:
56 Value of :py:attr:`traffic_light_was_found`.
57 """
58 return self.traffic_light_was_found
59
60
61# Use proper NamedTuples (Python 3.6+) and not the compatibility version from carla
[docs]
62class ObstacleDetectionResult(NamedTuple):
63 obstacle_was_found: bool
64 """"""
65 obstacle: Optional[carla.Actor]
66 """The found actor that represents the obstacle."""
67 distance: Union[float, Literal[-1]]
68 """The distance to the obstacle. If no obstacle was found, the distance is -1."""
69
[docs]
70 def __bool__(self) -> bool:
71 """
72 Returns:
73 Value of :py:attr:`obstacle_was_found`.
74 """
75 return self.obstacle_was_found
76
77
[docs]
78class CameraBlueprint(NamedTuple):
79 """
80 Represents a camera blueprint to spawn a camera sensor
81 to be used with the :py:class:`CameraManager`.
82 """
83
84 # TODO: Should be turned into a TypedDict instead of a NamedTuple to handle the setting of actual_blueprint better
85
86 blueprint_path: str
87 """Blueprint name for the actor"""
88 color_convert: carla.ColorConverter
89 """
90 Color converter for the camera.
91 If none is needed set to :py:attr:`carla.ColorConverter.Raw`
92 """
93 name: str
94 """Semantic name of the blueprint, e.g. RGB, Segmentation"""
95 actual_blueprint: Optional[carla.ActorBlueprint] = None
96 """The actual blueprint object; filled in later"""