Skip to content

Commit f212bfd

Browse files
authored
Bounding boxes script improvements (#9188)
1 parent cfc5779 commit f212bfd

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

PythonAPI/examples/bounding_boxes.py

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88

99
# Generates 2D and 3D bounding boxes for a simulation and can save them as JSON
1010
# 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+
"""
1419

1520
import carla
16-
import math
1721
import json
1822
import random
19-
import time
2023
import queue
2124
import pygame
25+
import argparse
2226
import numpy as np
2327
from math import radians
2428

@@ -253,6 +257,34 @@ def vehicle_light_state_to_dict(vehicle: carla.Vehicle):
253257

254258
def main():
255259

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+
256288
pygame.init()
257289

258290
# State variables
@@ -263,13 +295,13 @@ def main():
263295
clock = pygame.time.Clock()
264296
pygame.display.set_caption("Bounding Box Visualization")
265297
display = pygame.display.set_mode(
266-
(1280, 720),
298+
(args.width, args.height),
267299
pygame.HWSURFACE | pygame.DOUBLEBUF)
268300
display.fill((0,0,0))
269301
pygame.display.flip()
270302

271303
# Connect to the CARLA server and get the world object
272-
client = carla.Client('localhost', 2000)
304+
client = carla.Client(args.host, args.port)
273305
world = client.get_world()
274306

275307
# Set up the simulator in synchronous mode
@@ -293,15 +325,15 @@ def main():
293325

294326
# spawn RGB camera
295327
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))
298330
camera_init_trans = carla.Transform(carla.Location(z=2))
299331
camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=ego_vehicle)
300332

301333
# spawn instance segmentation camera
302334
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))
305337
camera_init_trans = carla.Transform(carla.Location(z=2))
306338
inst_camera = world.spawn_actor(inst_camera_bp, camera_init_trans, attach_to=ego_vehicle)
307339

@@ -356,6 +388,9 @@ def main():
356388
inst_seg_image = inst_queue.get()
357389
inst_seg = np.reshape(np.copy(inst_seg_image.raw_data), (inst_seg_image.height, inst_seg_image.width, 4))
358390

391+
# Decode instance segmentation image
392+
semantic_labels, actor_ids = decode_instance_segmentation(inst_seg)
393+
359394
# Empty list to collect bounding boxes for this frame
360395
frame_bboxes = []
361396

@@ -368,8 +403,8 @@ def main():
368403
npc_bbox = npc.bounding_box
369404
dist = npc.get_transform().location.distance(ego_vehicle.get_transform().location)
370405

371-
# Filter for the vehicles within 100m
372-
if dist < 50:
406+
# Filter for the vehicles within 50m
407+
if dist < args.distance:
373408

374409
# Limit to vehicles in front of the camera
375410
forward_vec = camera.get_transform().get_forward_vector()
@@ -378,7 +413,6 @@ def main():
378413
if forward_vec.dot(inter_vehicle_vec) > 0:
379414

380415
# Generate 2D and 2D bounding boxes for each actor
381-
semantic_labels, actor_ids = decode_instance_segmentation(inst_seg)
382416
npc_bbox_2d = bbox_2d_for_actor(npc, actor_ids, semantic_labels)
383417
npc_bbox_3d = bbox_3d_for_actor(npc, ego_vehicle, camera_bp, camera)
384418

@@ -443,7 +477,8 @@ def main():
443477

444478
if __name__ == '__main__':
445479
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')
449484
main()

0 commit comments

Comments
 (0)