自动驾驶模拟器Carla之python编程-(3)获取汽车摄像头数据

该博客介绍了如何在CARLA模拟环境中为一辆特斯拉Model3车辆添加RGB摄像头。首先,通过设置图像尺寸和视场角来配置摄像头蓝图。接着,调整传感器在车辆上的位置,并将其附加到车辆上。然后,监听传感器数据,用`process_img`函数处理图像并在OpenCV窗口中显示。整个过程展示了如何在CARLA中操作传感器并获取实时图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上节我们已经创建了一个可以形式的特斯拉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.')

这应该弹出一个新窗口以显示摄像机传感器:
在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值