在上节我们已经创建了一个可以形式的特斯拉model3,在本节,我们将给汽车上添加摄像头。
了解更多有关各种传感器以及如何使用它们的信息:Carla Sensors。现在,我将仅展示如何使用RGB相机。
1. 创建摄像头
在脚本的顶部,让我们设置几个常量:
IM_WIDTH = 640
IM_HEIGHT = 480
现在,我们加载传感器的蓝图并设置一些属性:
# https://carla.readthedocs.io/en/latest/cameras_and_sensors
# get the blueprint for this sensor
blueprint = blueprint_library.find('sensor.camera.rgb')
# change the dimensions of the image
blueprint.set_attribute('image_size_x', f'{IM_WIDTH}') # 图像宽度
blueprint.set_attribute('image_size_y', f'{IM_HEIGHT}') # 图像高度
blueprint.set_attribute('fov', '110') # 视场角
2. 将摄像头安装在车上
接下来,我们需要将其添加到我们的汽车中。首先,我们将从相对位置调整传感器,然后将其连接到汽车上。因此,我们要说的是这个传感器,从它的相对位置(汽车)出发,我们想向前移动2.5米并向上移动0.7米。可以根据您选择的车辆随意调整这些值,或者只是使用我的车辆即可。
# Adjust sensor relative to vehicle
spawn_point = carla.Transform(carla.Location(x=2.5, z=0.7))
# spawn the sensor and attach to vehicle.
sensor = world.spawn_actor(blueprint, spawn_point, attach_to=vehicle)
现在,我们想将此传感器添加到actor_list
角色列表中:
# add sensor to list of actors
actor_list.append(sensor)
3. 获取摄像头图像
最后,我们想对此传感器做些事情。我们想要从中获取图像,所以我们想要listen。
为了处理从传感器获取的数据,我们可以使用一个lambda函数:
sensor.listen(lambda data: process_img(data))
在这种情况下,我们将要从传感器中获取数据,并将其通过某个名为的函数传递process_img。这还不存在,所以让我们创建一个:
def process_img(image):
i = np.array(image.raw_data) # convert to an array
i2 = i.reshape((IM_HEIGHT, IM_WIDTH, 4)) # was flattened, so we're going to shape it.
i3 = i2[:, :, :3] # remove the alpha (basically, remove the 4th index of every pixel. Converting RGBA to RGB)
cv2.imshow("", i3) # show it.
cv2.waitKey(1)
return i3/255.0 # normalize
- 现在的完整代码如下:
import glob
import os
import sys
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:
pass
import carla
import random
import time
import numpy as np
import cv2
IM_WIDTH = 640
IM_HEIGHT = 480
def process_img(image):
i = np.array(image.raw_data)
i2 = i.reshape((IM_HEIGHT, IM_WIDTH, 4))
i3 = i2[:, :, :3]
cv2.imshow("", i3)
cv2.waitKey(1)
return i3/255.0
actor_list = []
try:
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.filter('model3')[0]
print(bp)
spawn_point = random.choice(world.get_map().get_spawn_points())
vehicle = world.spawn_actor(bp, spawn_point)
vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=0.0))
# vehicle.set_autopilot(True) # if you just wanted some NPCs to drive.
actor_list.append(vehicle)
# https://carla.readthedocs.io/en/latest/cameras_and_sensors
# get the blueprint for this sensor
blueprint = blueprint_library.find('sensor.camera.rgb')
# change the dimensions of the image
blueprint.set_attribute('image_size_x', f'{IM_WIDTH}')
blueprint.set_attribute('image_size_y', f'{IM_HEIGHT}')
blueprint.set_attribute('fov', '110')
# Adjust sensor relative to vehicle
spawn_point = carla.Transform(carla.Location(x=2.5, z=0.7))
# spawn the sensor and attach to vehicle.
sensor = world.spawn_actor(blueprint, spawn_point, attach_to=vehicle)
# add sensor to list of actors
actor_list.append(sensor)
# do something with this sensor
sensor.listen(lambda data: process_img(data))
time.sleep(5)
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')
这应该弹出一个新窗口以显示摄像机传感器: