-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensors.py
186 lines (151 loc) · 6 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
import glob
import os
import sys
import math
import queue
import numpy as np
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
from carla import ColorConverter as cc
from env.utils import FPS
from env.configer import CARLA_VERSION
camera_list = [
{
'name': 'Lidar',
'type': 'sensor.lidar.ray_cast',
'range' : 40.0,
'channels': 1,
'upper_fov': 0.0,
'lower_fov': 0.0,
'rotation_frequency': float(FPS),
'points_per_second': FPS*360*4,
'pos' : carla.Transform(carla.Location(x=1.7, z=1.12)),
'convertor': None
},
{
'name': 'FrontRGB',
'type': 'sensor.camera.rgb',
'width' : 1500,
'height' : 650,
'fov': 140.0,
'pos' : carla.Transform(carla.Location(x=1.0, z=1.6)),
'convertor': cc.Raw,
# 'camera_exposure_mode': 'manual',
# 'shutter_speed': 3000
'iso': 100.0,
"exposure_mode": "histogram",
"exposure_compensation": 0.0,
"exposure_min_bright": 10.0,
"exposure_max_bright": 12.0,
}
]
class CameraSensor:
def __init__(self, world, vehicle, camera):
self.world = world
self.name = camera['name']
self.type = camera['type']
bp = self.world.get_blueprint_library().find(self.type)
if self.type.startswith('sensor.camera'):
self.width = camera['width']
self.height = camera['height']
self.fov = camera['fov']
bp.set_attribute("image_size_x", str(self.width))
bp.set_attribute("image_size_y", str(self.height)) # set resolution
bp.set_attribute("fov", str(self.fov))
# 'iso': 100.0,
# "exposure_mode": "histogram",
# "exposure_compensation": -1.0,
# "exposure_min_bright": 7.0,
# "exposure_max_bright": 9.0,
if self.type.endswith('rgb') and CARLA_VERSION == '0.9.12':
bp.set_attribute('iso', str(camera['iso']))
bp.set_attribute('exposure_mode', str(camera['exposure_mode']))
bp.set_attribute('exposure_compensation', str(camera['exposure_compensation']))
bp.set_attribute('exposure_min_bright', str(camera['exposure_min_bright']))
bp.set_attribute('exposure_max_bright', str(camera['exposure_max_bright']))
elif self.type.startswith('sensor.lidar'):
bp.set_attribute('range', str(camera['range']))
bp.set_attribute('channels', str(camera['channels']))
bp.set_attribute('upper_fov', str(camera['upper_fov']))
bp.set_attribute('lower_fov', str(camera['lower_fov']))
bp.set_attribute('rotation_frequency', str(camera['rotation_frequency']))
bp.set_attribute('points_per_second', str(camera['points_per_second']))
else:
assert False, 'camera type error'
self.sensor = self.world.spawn_actor(bp, camera['pos'], attach_to=vehicle)
self.convertor = camera['convertor']
self.que = queue.Queue()
self.sensor.listen(self.que.put)
def get_data(self, frame0):
event = self.que.get()
assert (event.frame == frame0) # ensure synchronous
if self.type.startswith('sensor.camera'):
event.convert(self.convertor)
img = np.array(event.raw_data) # BGRA 32-bit pixels
img = img.reshape((self.height, self.width, 4))[:, :, :3] # BGR
return img
elif self.type.startswith('sensor.lidar'):
points = np.frombuffer(event.raw_data, dtype=np.dtype('f4'))
if CARLA_VERSION == '0.9.7':
points = np.reshape(points, (int(points.shape[0] / 3), 3))
elif CARLA_VERSION == '0.9.12':
points = np.reshape(points, (int(points.shape[0] / 4), 4))
else:
raise NotImplementedError
return points, event.transform
def destroy(self):
self.sensor.stop()
self.sensor.destroy()
class CollisionSensor:
def __init__(self, world, vehicle, name):
self.world = world
self.name = name
self.history = 0.0 # the intensity of the collision, 0.0 means pristine
self.other_actor = None
bp = self.world.get_blueprint_library().find("sensor.other.collision")
self.sensor = self.world.spawn_actor(bp, carla.Transform(), attach_to=vehicle)
self.que = queue.Queue()
self.sensor.listen(self.que.put)
def get_data(self, frame0):
"""
Returns:
self.hitory -- the max collision intensity
self.other_actor -- the bp of the collided actor
"""
if not self.que.empty():
event = self.que.get()
impulse = event.normal_impulse
intensity = math.sqrt(impulse.x ** 2 + impulse.y ** 2 + impulse.z ** 2) # calc the intensity
if intensity > self.history:
self.history = intensity
self.other_actor = event.other_actor.type_id
return (self.history, self.other_actor)
def destroy(self):
self.sensor.stop()
self.sensor.destroy()
class LaneInvasionSensor:
def __init__(self, world, vehicle, name):
self.world = world
self.name = name
bp = self.world.get_blueprint_library().find('sensor.other.lane_invasion')
self.sensor = self.world.spawn_actor(bp, carla.Transform(), attach_to=vehicle)
self.que = queue.Queue()
self.sensor.listen(self.que.put)
def get_data(self, frame0):
"""
Returns:
set() -- a set contains all the lane that invaded
"""
if self.que.empty():
return set()
event = self.que.get()
return set(x.type for x in event.crossed_lane_markings)
def destroy(self):
self.sensor.stop()
self.sensor.destroy()