import sys
try:
sys.path.append('D:\work_software\Carla\Carla_0.9.10\CARLA_0.9.10\WindowsNoEditor\PythonAPI\carla\dist\carla-0.9.10-py3.7-win-amd64.egg')
except IndexError:
pass
import carla
import random
import time
client = carla.Client("localhost", 2000)
client.set_timeout(5.0)
world = client.get_world()
vehicle_actor_blueprint = world.get_blueprint_library().filter("vehicle.*.*")[0]
vehicle_actor_transform = carla.Transform(carla.Location(-88.5, -65.0, 0.1), carla.Rotation(yaw=90))
vehicle = world.spawn_actor(vehicle_actor_blueprint, vehicle_actor_transform)
# Find the blueprint of the sensor
camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')
# Modify the attributes of the blueprint to set image resolution and field of view
camera_bp.set_attribute('image_size_x', '1920')
camera_bp.set_attribute('image_size_y', '1080')
camera_bp.set_attribute('fov', '110')
# Set the time in seconds between sensor captures
camera_bp.set_attribute('sensor_tick', '1.0')
camera_transform = carla.Transform(carla.Location(x=0.8, z=1.7))
sensor = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
# do_something() will be call each time a new image is generated by the camera
# sensor.listen(lambda data: do_something(date))
sensor.listen(lambda image: image.save_to_disk('./output/%06d.png' % image.frame))
# This collision sensor would print everytime a collision is detected
# def callback(event):
# for actor_id in event:
# vehicle = world_ref().get_actor(actor_id)
# print('Vehicle too close: %s' % vehicle.type_id)
# sensor02.listen(callback)
# Main loop
while True:
# Tick the server
world.tick()
w_frame = world.get_snapshot().frame
print("World's frame: %d" % w_frame)
time.sleep(1)
注:my_script_test_11.py