carla环境下基于强化学习的自动驾驶_哔哩哔哩_bilibili
本篇文章是小编在pycharm上自己手敲代码学习自动驾驶的第一篇文章,主要讲述如何在Carla中控制我们自己生成的汽车并且使用rgb摄像头传感器获取图像数据。
以下代码参考自:(如有侵权,请联系我将立即删除)使用 Carla 和 Python 的自动驾驶汽车第 2 部分 —— 控制汽车并获取传感器数据-CSDN博客
1、导入carla(其中的路径根据自己的实际情况修改)
import glob
import os
import sys
try:
sys.path.append(glob.glob(
r'D:\postgraduate\code\CARLA_0.9.14\WindowsNoEditor\PythonAPI\carla\dist\carla-0.9.14-py3.7-win-amd64.egg')[0])
except IndexError:
pass
import carla
2、我们将立即处理的第一件事是演员列表,并在我们完成后清理它们
actor_list = []
try:
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')
在这里,我们将在 try/finally 中封装主要的大部分代码。我们将把所有的逻辑和actor创建放在try中,然后finally会为我们清理它。
3、接下来,回想一下我们在 Carla 中有 3 个主要“事物”:世界、蓝图和演员。首先,我们将连接到我们的服务器,获取世界,然后访问蓝图
actor_list = []
try:
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
4、有了蓝图之后,我们就可以选取某个点来生成我们的汽车,并且控制它
bp = blueprint_library.filter('model3')[0]
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))
5、最后,我们不要忘记将这辆车添加到我们需要跟踪和清理的演员列表中,现在我们有一辆车,让我们运行 5 秒钟然后清理
actor_list.append(vehicle)
time.sleep(5)
好的,到这里,我们的第一个任务就完成了:我们可以在carla上生成一辆自己的车并控制它行驶了,效果图如下所示:
当然如果您没在carla中找到自己的车,这也没关系,因为这确实不太容易,因为车对于地图来说太小了,你可以多运行几次代码。
我们接下来想要的是在我们的汽车上安装一个摄像头,并弄清楚如何访问这些数据。理想情况下,这款引擎盖相机将是我们的主要传感器,我们以后也可以合并其他传感器。
CARLA传感器详细文档介绍+python实例(持续更新ing)_carla 语义分割相机-CSDN博客
6、在我们脚本的顶部,让我们设置几个常量,然后我们加载传感器的蓝图并设置一些属性
IM_WIDTH = 640
IM_HEIGHT = 480
# get the blueprint for this sensor
rgb_camera = blueprint_library.find('sensor.camera.rgb')
# change the dimensions of the image
rgb_camera.set_attribute('image_size_x', f'{IM_WIDTH}')
rgb_camera.set_attribute('image_size_y', f'{IM_HEIGHT}')
rgb_camera.set_attribute('fov', '110')
Blueprint attribute | Type | Default | Description |
---|---|---|---|
fov | float | 90.0 | 水平视野角度 |
image_size_x | int | 800 | 图像宽度像素数 |
image_size_y | int | 600 | 图像高度像素数 |
7、接下来,我们需要将它添加到我们的汽车中,并将其添加到我们的演员列表中。首先,我们将从相对位置调整传感器,然后将其连接到我们的汽车上。所以我们会说这个传感器,从它的相对位置(汽车),我们想要向前移动 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(rgb_camera, spawn_point, attach_to=vehicle)
# add sensor to list of actors
actor_list.append(sensor)
8、最后,我们想用这个传感器做点什么。我们想从中获得图像,并对我们从传感器获得的数据做一些事情,我们可以使用 lambda 函数:
sensor.listen(lambda data: process_img(data))
在这种情况下,我们将从传感器获取数据,并将其传递给一个名为 process_img 的函数。我们的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
在process_img函数中我们做了以下事情:
(1)将图像的原始raw数据转换成一维的numpy数组,长度恰好是IM_HEIGHT * IM_WIDTH * 4,这里的4表示的是RGBA(A为透明度alpha)4个通道
(注:RAW在英文中的解释是未处理的、自然状态的,这也就是RAW文件的真谛。RAW图像就是CMOS或者CCD图像感应器将捕捉到的光源信号转化为数字信号的原始数据,因此RAW文件也被人们称之为“数码底片”。)
(2)改变数组的形状,将其变成一个三维数组,共有4个二维数组,每个二维数组的行是IM_HEIGHT,列是IM_WIDTH
有关python中一维,二维,三维数组的理解_python一维数组和二维数组的区别-CSDN博客
(3)去掉图像的透明度属性,将数组的大小变为(IM_HEIGHT , IM_WIDTH ,3)
(4)利用opencv展示图像
(5)最后返回的图像数据是归一化后的
好的,到这里,本篇文章的内容结束。
以下是完整代码:
import glob
import os
import sys
import random
import time
import numpy as np
import cv2
try:
sys.path.append(glob.glob(
r'D:\postgraduate\code\CARLA_0.9.14\WindowsNoEditor\PythonAPI\carla\dist\carla-0.9.14-py3.7-win-amd64.egg')[0])
except IndexError:
pass
import carla
actor_list = []
IM_WIDTH = 640
IM_HEIGHT = 480
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
try:
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.filter('model3')[0]
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))
actor_list.append(vehicle)
# get the blueprint for this sensor
rgb_camera = blueprint_library.find('sensor.camera.rgb')
# change the dimensions of the image
rgb_camera.set_attribute('image_size_x', f'{IM_WIDTH}')
rgb_camera.set_attribute('image_size_y', f'{IM_HEIGHT}')
rgb_camera.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(rgb_camera, spawn_point, attach_to=vehicle)
# add sensor to list of actors
actor_list.append(sensor)
sensor.listen(lambda data: process_img(data))
time.sleep(20)
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')
代码运行效果如下:
自动驾驶