8
8
9
9
# Generates 2D and 3D bounding boxes for a simulation and can save them as JSON
10
10
# Instructions:
11
- # Press "r" to start recording images and bounding boxes
12
- # Press "3" to visualize bounding boxes in 3D
13
- # Press "2" to vizualize bounding boxes in 2D
11
+
12
+ """
13
+ Welcome to CARLA bounding boxes.
14
+ R : toggle recording images and bounding boxes
15
+ 3 : visualize bounding boxes in 3D
16
+ 2 : vizualize bounding boxes in 2D
17
+ ESC : quit
18
+ """
14
19
15
20
import carla
16
- import math
17
21
import json
18
22
import random
19
- import time
20
23
import queue
21
24
import pygame
25
+ import argparse
22
26
import numpy as np
23
27
from math import radians
24
28
@@ -253,6 +257,34 @@ def vehicle_light_state_to_dict(vehicle: carla.Vehicle):
253
257
254
258
def main ():
255
259
260
+ argparser = argparse .ArgumentParser (
261
+ description = 'CARLA bounding boxes' )
262
+ argparser .add_argument (
263
+ '--host' ,
264
+ metavar = 'H' ,
265
+ default = '127.0.0.1' ,
266
+ help = 'IP of the host server (default: 127.0.0.1)' )
267
+ argparser .add_argument (
268
+ '-p' , '--port' ,
269
+ metavar = 'P' ,
270
+ default = 2000 ,
271
+ type = int ,
272
+ help = 'TCP port to listen to (default: 2000)' )
273
+ argparser .add_argument (
274
+ '-d' , '--distance' ,
275
+ metavar = 'D' ,
276
+ default = 50 ,
277
+ type = int ,
278
+ help = 'Actor distance threshold' )
279
+ argparser .add_argument (
280
+ '--res' ,
281
+ metavar = 'WIDTHxHEIGHT' ,
282
+ default = '1280x720' ,
283
+ help = 'window resolution (default: 1280x720)' )
284
+ args = argparser .parse_args ()
285
+
286
+ args .width , args .height = [int (x ) for x in args .res .split ('x' )]
287
+
256
288
pygame .init ()
257
289
258
290
# State variables
@@ -263,13 +295,13 @@ def main():
263
295
clock = pygame .time .Clock ()
264
296
pygame .display .set_caption ("Bounding Box Visualization" )
265
297
display = pygame .display .set_mode (
266
- (1280 , 720 ),
298
+ (args . width , args . height ),
267
299
pygame .HWSURFACE | pygame .DOUBLEBUF )
268
300
display .fill ((0 ,0 ,0 ))
269
301
pygame .display .flip ()
270
302
271
303
# Connect to the CARLA server and get the world object
272
- client = carla .Client ('localhost' , 2000 )
304
+ client = carla .Client (args . host , args . port )
273
305
world = client .get_world ()
274
306
275
307
# Set up the simulator in synchronous mode
@@ -293,15 +325,15 @@ def main():
293
325
294
326
# spawn RGB camera
295
327
camera_bp = bp_lib .find ('sensor.camera.rgb' )
296
- camera_bp .set_attribute ('image_size_x' , '1280' )
297
- camera_bp .set_attribute ('image_size_y' , '720' )
328
+ camera_bp .set_attribute ('image_size_x' , str ( args . width ) )
329
+ camera_bp .set_attribute ('image_size_y' , str ( args . height ) )
298
330
camera_init_trans = carla .Transform (carla .Location (z = 2 ))
299
331
camera = world .spawn_actor (camera_bp , camera_init_trans , attach_to = ego_vehicle )
300
332
301
333
# spawn instance segmentation camera
302
334
inst_camera_bp = bp_lib .find ('sensor.camera.instance_segmentation' )
303
- inst_camera_bp .set_attribute ('image_size_x' , '1280' )
304
- inst_camera_bp .set_attribute ('image_size_y' , '720' )
335
+ inst_camera_bp .set_attribute ('image_size_x' , str ( args . width ) )
336
+ inst_camera_bp .set_attribute ('image_size_y' , str ( args . height ) )
305
337
camera_init_trans = carla .Transform (carla .Location (z = 2 ))
306
338
inst_camera = world .spawn_actor (inst_camera_bp , camera_init_trans , attach_to = ego_vehicle )
307
339
@@ -356,6 +388,9 @@ def main():
356
388
inst_seg_image = inst_queue .get ()
357
389
inst_seg = np .reshape (np .copy (inst_seg_image .raw_data ), (inst_seg_image .height , inst_seg_image .width , 4 ))
358
390
391
+ # Decode instance segmentation image
392
+ semantic_labels , actor_ids = decode_instance_segmentation (inst_seg )
393
+
359
394
# Empty list to collect bounding boxes for this frame
360
395
frame_bboxes = []
361
396
@@ -368,8 +403,8 @@ def main():
368
403
npc_bbox = npc .bounding_box
369
404
dist = npc .get_transform ().location .distance (ego_vehicle .get_transform ().location )
370
405
371
- # Filter for the vehicles within 100m
372
- if dist < 50 :
406
+ # Filter for the vehicles within 50m
407
+ if dist < args . distance :
373
408
374
409
# Limit to vehicles in front of the camera
375
410
forward_vec = camera .get_transform ().get_forward_vector ()
@@ -378,7 +413,6 @@ def main():
378
413
if forward_vec .dot (inter_vehicle_vec ) > 0 :
379
414
380
415
# Generate 2D and 2D bounding boxes for each actor
381
- semantic_labels , actor_ids = decode_instance_segmentation (inst_seg )
382
416
npc_bbox_2d = bbox_2d_for_actor (npc , actor_ids , semantic_labels )
383
417
npc_bbox_3d = bbox_3d_for_actor (npc , ego_vehicle , camera_bp , camera )
384
418
@@ -443,7 +477,8 @@ def main():
443
477
444
478
if __name__ == '__main__' :
445
479
print ('Bounding boxes script instructions:' )
446
- print ('Press "r" to start recording images as PNG and bounding boxes as JSON' )
447
- print ('Press "3" to see the bounding boxes in 3D' )
448
- print ('Press "2" to see the bounding boxes in 2D' )
480
+ print ('R : toggle recording images as PNG and bounding boxes as JSON' )
481
+ print ('3 : view the bounding boxes in 3D' )
482
+ print ('2 : view the bounding boxes in 2D' )
483
+ print ('ESC : quit' )
449
484
main ()
0 commit comments