-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_semantic_lidar.py
264 lines (219 loc) · 8.87 KB
/
test_semantic_lidar.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
### Example program to save several sensor data including bounding box
### Sensors: RGB Camera (+BoundingBox), Semantic Lidar
### By Mukhlas Adib
### 2020
### Last tested on CARLA 0.9.10.1
### CARLA Simulator is licensed under the terms of the MIT license
### For a copy, see <https://opensource.org/licenses/MIT>
### For more information about CARLA Simulator, visit https://carla.org/
import glob
import os
import sys
import time
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:
print('carla not found')
pass
import carla
import argparse
import logging
import random
import queue
import numpy as np
from matplotlib import pyplot as plt
import cv2
import carla_vehicle_annotator as cva
### Set to True if you need to save the data in darknet training format, False otherwise
save_darknet = True
###
def retrieve_data(sensor_queue, frame, timeout=1):
while True:
try:
data = sensor_queue.get(True,timeout)
except queue.Empty:
return None
if data.frame == frame:
return data
def main():
argparser = argparse.ArgumentParser(
description=__doc__)
argparser.add_argument(
'--host',
metavar='H',
default='127.0.0.1',
help='IP of the host server (default: 127.0.0.1)')
argparser.add_argument(
'-p', '--port',
metavar='P',
default=2000,
type=int,
help='TCP port to listen to (default: 2000)')
argparser.add_argument(
'-n', '--number-of-vehicles',
metavar='N',
default=100,
type=int,
help='number of vehicles (default: 10)')
argparser.add_argument(
'-tm_p', '--tm_port',
metavar='P',
default=8000,
type=int,
help='port to communicate with TM (default: 8000)')
args = argparser.parse_args()
vehicles_list = []
nonvehicles_list = []
client = carla.Client(args.host, args.port)
client.set_timeout(10.0)
try:
traffic_manager = client.get_trafficmanager(args.tm_port)
traffic_manager.set_global_distance_to_leading_vehicle(2.0)
world = client.get_world()
print('\nRUNNING in synchronous mode\n')
settings = world.get_settings()
traffic_manager.set_synchronous_mode(True)
if not settings.synchronous_mode:
synchronous_master = True
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
else:
synchronous_master = False
blueprints = world.get_blueprint_library().filter('vehicle.*')
spawn_points = world.get_map().get_spawn_points()
number_of_spawn_points = len(spawn_points)
if args.number_of_vehicles < number_of_spawn_points:
random.shuffle(spawn_points)
elif args.number_of_vehicles > number_of_spawn_points:
msg = 'Requested %d vehicles, but could only find %d spawn points'
logging.warning(msg, args.number_of_vehicles, number_of_spawn_points)
args.number_of_vehicles = number_of_spawn_points
SpawnActor = carla.command.SpawnActor
SetAutopilot = carla.command.SetAutopilot
FutureActor = carla.command.FutureActor
# --------------
# Spawn vehicles
# --------------
batch = []
for n, transform in enumerate(spawn_points):
if n >= args.number_of_vehicles:
break
blueprint = random.choice(blueprints)
if blueprint.has_attribute('color'):
color = random.choice(blueprint.get_attribute('color').recommended_values)
blueprint.set_attribute('color', color)
if blueprint.has_attribute('driver_id'):
driver_id = random.choice(blueprint.get_attribute('driver_id').recommended_values)
blueprint.set_attribute('driver_id', driver_id)
blueprint.set_attribute('role_name', 'autopilot')
batch.append(SpawnActor(blueprint, transform).then(SetAutopilot(FutureActor, True)))
spawn_points.pop(0)
for response in client.apply_batch_sync(batch, synchronous_master):
if response.error:
logging.error(response.error)
else:
vehicles_list.append(response.actor_id)
print('Created %d npc vehicles \n' % len(vehicles_list))
# -----------------------------
# Spawn ego vehicle and sensors
# -----------------------------
q_list = []
idx = 0
tick_queue = queue.Queue()
world.on_tick(tick_queue.put)
q_list.append(tick_queue)
tick_idx = idx
idx = idx+1
# Spawn ego vehicle
ego_bp = random.choice(blueprints)
ego_transform = random.choice(spawn_points)
ego_vehicle = world.spawn_actor(ego_bp, ego_transform)
vehicles_list.append(ego_vehicle)
ego_vehicle.set_autopilot(True)
print('Ego-vehicle ready')
# Spawn RGB camera
cam_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
cam_bp = world.get_blueprint_library().find('sensor.camera.rgb')
cam_bp.set_attribute('sensor_tick', '1.0')
cam = world.spawn_actor(cam_bp, cam_transform, attach_to=ego_vehicle)
nonvehicles_list.append(cam)
cam_queue = queue.Queue()
cam.listen(cam_queue.put)
q_list.append(cam_queue)
cam_idx = idx
idx = idx+1
print('RGB camera ready')
# Spawn LIDAR sensor
lidar_bp = world.get_blueprint_library().find('sensor.lidar.ray_cast_semantic')
lidar_bp.set_attribute('sensor_tick', '1.0')
lidar_bp.set_attribute('channels', '64')
lidar_bp.set_attribute('points_per_second', '1120000')
lidar_bp.set_attribute('upper_fov', '40')
lidar_bp.set_attribute('lower_fov', '-40')
lidar_bp.set_attribute('range', '100')
lidar_bp.set_attribute('rotation_frequency', '20')
lidar_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
lidar = world.spawn_actor(lidar_bp, lidar_transform, attach_to=ego_vehicle)
nonvehicles_list.append(lidar)
lidar_queue = queue.Queue()
lidar.listen(lidar_queue.put)
q_list.append(lidar_queue)
lidar_idx = idx
idx = idx+1
print('LIDAR ready')
# Begin the loop
time_sim = 0
while True:
# Extract the available data
nowFrame = world.tick()
# Check whether it's time for sensor to capture data
if time_sim >= 1:
data = [retrieve_data(q,nowFrame) for q in q_list]
assert all(x.frame == nowFrame for x in data if x is not None)
# Skip if any sensor data is not available
if None in data:
continue
vehicles_raw = world.get_actors().filter('vehicle.*')
snap = data[tick_idx]
rgb_img = data[cam_idx]
lidar_img = data[lidar_idx]
# Attach additional information to the snapshot
vehicles = cva.snap_processing(vehicles_raw, snap)
# Calculating visible bounding boxes
filtered_out,_ = cva.auto_annotate_lidar(vehicles, cam, lidar_img, show_img = rgb_img, json_path = 'vehicle_class_json_file.txt')
# Save the results
cva.save_output(rgb_img, filtered_out['bbox'], filtered_out['class'], save_patched=True, out_format='json')
# Save the results to darknet format
if save_darknet: cva.save2darknet(filtered_out['bbox'], filtered_out['class'], rgb_img)
time_sim = 0
time_sim = time_sim + settings.fixed_delta_seconds
finally:
try:
if save_darknet: cva.save2darknet(None, None, None, save_train = True)
except:
print('No darknet formatted data directory found')
try:
cam.stop()
lidar.stop()
except:
print('Sensors has not been initiated')
settings = world.get_settings()
settings.synchronous_mode = False
settings.fixed_delta_seconds = None
world.apply_settings(settings)
print('\ndestroying %d vehicles' % len(vehicles_list))
client.apply_batch([carla.command.DestroyActor(x) for x in vehicles_list])
print('destroying %d nonvehicles' % len(nonvehicles_list))
client.apply_batch([carla.command.DestroyActor(x) for x in nonvehicles_list])
time.sleep(0.5)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\ndone.')