-
Notifications
You must be signed in to change notification settings - Fork 2
/
sensors.py
196 lines (162 loc) · 7.79 KB
/
sensors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import math
import weakref
from collections import defaultdict
from functools import partial
from typing import List
from config import CAMERA_KEYWORDS, DATASET_FRAMERATE, EVAL_FRAMERATE_SCALE
from game.common import destroy_actor
from util.common import add_carla_module, get_logger
add_carla_module()
logger = get_logger(__name__)
import carla
CAMERA_SHIFT = 0.4
__camera_transforms__ = {
'center': carla.Transform(
carla.Location(x=1.6, z=1.7),
carla.Rotation(yaw=0)),
'left': carla.Transform(
carla.Location(x=1.6, y=-CAMERA_SHIFT, z=1.7),
carla.Rotation(yaw=math.atan2(-CAMERA_SHIFT, 1.6) * 180 / math.pi)),
'right': carla.Transform(
carla.Location(x=1.6, y=+CAMERA_SHIFT, z=1.7),
carla.Rotation(yaw=math.atan2(CAMERA_SHIFT, 1.6) * 180 / math.pi)),
'extra': carla.Transform(
carla.Location(x=1.6, z=1.7),
carla.Rotation(yaw=0)),
}
for key in __camera_transforms__.keys():
assert key in CAMERA_KEYWORDS
for key in CAMERA_KEYWORDS:
assert key in __camera_transforms__
class SensorBase:
def __init__(self, parent_actor):
self.sensor = None
self._parent = parent_actor
self.world = self._parent.get_world()
self.sensor = self.generate_sensor()
def generate_sensor(self):
raise NotImplementedError
def destroy(self):
destroy_actor(self.sensor)
def __del__(self):
self.destroy()
class CollisionSensor(SensorBase):
def __init__(self, parent_actor):
SensorBase.__init__(self, parent_actor)
self.history = defaultdict(int)
# weak_self = weakref.ref(self)
# self.sensor.listen(lambda event: CollisionSensor.on_collision(weak_self, event))
def generate_sensor(self):
bp = self.world.get_blueprint_library().find('sensor.other.collision')
return self.world.spawn_actor(bp, carla.Transform(), attach_to=self._parent)
def has_collided(self, frame_number: int) -> bool:
return self.history[frame_number] > 0
@staticmethod
def on_collision(weak_self, event):
self = weak_self()
if not self:
return
# actor_type = get_actor_display_name(event.other_actor)
impulse = event.normal_impulse
intensity = math.sqrt(impulse.x ** 2 + impulse.y ** 2 + impulse.z ** 2)
self.history[event.frame_number] += intensity
class CameraSensor(SensorBase):
def __init__(self, parent_actor, image_path_func, timing_dict, transform_dict, width, height, camera_keyword: str):
self.width = width
self.height = height
self.camera_keyword = camera_keyword
SensorBase.__init__(self, parent_actor)
self.image_path_func = image_path_func
self.image_frame_number = None
self.image_frame = None
self.timing_dict = timing_dict
self.transform_dict = transform_dict
# weak_self = weakref.ref(self)
# self.sensor.listen(lambda image: CameraSensor.on_listen(weak_self, image))
def generate_sensor(self):
bp = self.world.get_blueprint_library().find('sensor.camera.rgb')
bp.set_attribute('image_size_x', str(self.width))
bp.set_attribute('image_size_y', str(self.height))
bp.set_attribute('sensor_tick', str(1 / (DATASET_FRAMERATE * EVAL_FRAMERATE_SCALE)))
return self.world.spawn_actor(bp, __camera_transforms__[self.camera_keyword], attach_to=self._parent)
# @staticmethod
# def on_listen(weak_self, carla_image: carla.Image):
# self = weak_self()
# if not self:
# return
# frame_number = carla_image.frame_number
# self.timing_dict[frame_number] = get_current_time()
# self.transform_dict[frame_number] = self.sensor.get_transform()
# self.image_frame_number = frame_number
# numpy_image = numpy_from_carla_image(carla_image)
# self.image_frame = numpy_image
# # print(frame_number, self.image_frame.shape, self.image_path_func(frame_number))
# cv2.imwrite(str(self.image_path_func(frame_number)), numpy_image)
class SegmentationSensor(SensorBase):
def __init__(self, parent_actor, image_path_func, width, height, camera_keyword: str):
self.width = width
self.height = height
self.camera_keyword = camera_keyword
SensorBase.__init__(self, parent_actor)
self.image_path_func = image_path_func
self.image_frame_number = None
self.image_frame = None
# weak_self = weakref.ref(self)
# self.sensor.listen(lambda image: SegmentationSensor.on_listen(weak_self, image))
def generate_sensor(self):
bp = self.world.get_blueprint_library().find('sensor.camera.semantic_segmentation')
bp.set_attribute('image_size_x', str(self.width))
bp.set_attribute('image_size_y', str(self.height))
bp.set_attribute('sensor_tick', str(1 / (DATASET_FRAMERATE * EVAL_FRAMERATE_SCALE)))
return self.world.spawn_actor(bp, __camera_transforms__[self.camera_keyword], attach_to=self._parent)
# @staticmethod
# def on_listen(weak_self, carla_image: carla.Image):
# self = weak_self()
# if not self:
# return
# frame_number = carla_image.frame_number
# self.image_frame_number = frame_number
# numpy_image = numpy_from_carla_image(carla_image)
# self.image_frame = numpy_image
# cv2.imwrite(str(self.image_path_func(frame_number)), numpy_image)
def generate_camera_sensor(world, agent, camera_keyword, width, height):
bp = world.get_blueprint_library().find('sensor.camera.rgb')
bp.set_attribute('image_size_x', str(width))
bp.set_attribute('image_size_y', str(height))
bp.set_attribute('sensor_tick', str(1 / (DATASET_FRAMERATE * EVAL_FRAMERATE_SCALE)))
return world.spawn_actor(bp, __camera_transforms__[camera_keyword], attach_to=agent)
def generate_segmentation_sensor(world, agent, camera_keyword, width, height):
bp = world.get_blueprint_library().find('sensor.camera.semantic_segmentation')
bp.set_attribute('image_size_x', str(width))
bp.set_attribute('image_size_y', str(height))
bp.set_attribute('sensor_tick', str(1 / (DATASET_FRAMERATE * EVAL_FRAMERATE_SCALE)))
return world.spawn_actor(bp, __camera_transforms__[camera_keyword], attach_to=agent)
def generate_collision_sensor(world, agent):
bp = world.get_blueprint_library().find('sensor.other.collision')
return world.spawn_actor(bp, carla.Transform(), attach_to=agent)
def set_all_sensors(world, agent, camera_keywords, width, height, use_extra):
camera_sensor_dict = dict()
segmentation_sensor_dict = dict()
if use_extra:
camera_sensor_dict['extra'] = generate_camera_sensor(world, agent, 'extra', 1280, 720)
for keyword in camera_keywords:
camera_sensor_dict[keyword] = generate_camera_sensor(world, agent, keyword, width, height)
segmentation_sensor_dict[keyword] = generate_segmentation_sensor(world, agent, keyword, width, height)
collision_sensor = generate_collision_sensor(world, agent)
return camera_sensor_dict, segmentation_sensor_dict, collision_sensor
class SensorManager:
def __init__(self, world, agent, camera_keywords: List[str], width: int, height: int, use_extra: bool):
self.camera_sensor_dict = dict()
self.segmentation_sensor_dict = dict()
self.collision_sensor = None
self.camera_sensor_dict, self.segmentation_sensor_dict, self.collision_sensor = \
set_all_sensors(world, agent, camera_keywords, width, height, use_extra)
def destroy(self):
for sensor in self.camera_sensor_dict.values():
sensor.destroy()
for sensor in self.segmentation_sensor_dict.values():
sensor.destroy()
if self.collision_sensor is not None:
self.collision_sensor.destroy()
def __del__(self):
self.destroy()