import carla
import time
import numpy as np
defprocess_image(image):# Convert the image from a carla.Image to a numpy array
image_data = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
image_data = np.reshape(image_data,(image.height, image.width,4))# Do something with the image data (e.g. save it to disk)# ...
actor_list =[]try:
client = carla.Client('172.16.183.208',2000)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
model_3 = blueprint_library.filter('model3')[0]# Define the vehicle's location and orientation
location = carla.Location(x=230, y=195, z=40)
rotation = carla.Rotation(yaw=180)
transform = carla.Transform(location, rotation)
vehicle = world.spawn_actor(model_3, transform)
actor_list.append(vehicle)# Add a camera sensor to the vehicle
camera_bp = blueprint_library.find('sensor.camera.rgb')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
actor_list.append(camera)# Register the callback function to process the camera data
camera.listen(process_image)# 获取观察者actor
spectator = world.get_spectator()# 计算观察者位置
location = vehicle.get_location()
location += carla.Location(x=-5.5, z=2.8)
rotation = vehicle.get_transform().rotation
rotation.yaw +=180# 更新观察者位置和方向
spectator.set_transform(carla.Transform(location, rotation))# Turn left 90 degrees
vehicle.apply_control(carla.VehicleControl(throttle=0.5, steer=-1.0))
time.sleep(1.2)# Turn right 90 degrees
vehicle.apply_control(carla.VehicleControl(throttle=0.5, steer=1.0))
time.sleep(1.2)finally:for actor in actor_list:
actor.destroy()