Carla设置传感器
下面的学习来自于carla的官方文档,将一步步介绍如何在carla中设置传感器,并成功获得数据。
什么是传感器
传感器是Carla专门为了接受数据而设置的。虽然传感器的类型非常多样,但他们接收到的数据一般都是SensorData的继承类。传感器会在每个仿真周期内收集一次数据,或者当某些特定事件触发时,也会收集数据。这些数据会通过listen接口反馈给使用者。
如何设置传感器
如何安装、运行carla可以参考其他博客,这里提示在测试传感器前,需要运行carla的simulation。
传感器蓝图及参数
设置传感器前,需要找到所需要传感器的蓝图,并设置关于传感器的合理参数。
这里以RGB摄像头为例,python的设置情况如下:
# 从carla包中找到传感器的蓝图,传感器的蓝图可以从相关的文件设置中查到
blueprint = world.get_blueprint_library().find('sensor.camera.rgb')
# Modify the attributes of the blueprint to set image resolution and field of view.
# 设置传感器采集图片的尺寸
blueprint.set_attribute('image_size_x', '1920')
blueprint.set_attribute('image_size_y', '1080')
#设置摄像头的水平翻转
blueprint.set_attribute('fov', '110')
# Set the time in seconds between sensor captures
blueprint.set_attribute('sensor_tick', '1.0')
下图为RGB摄像头传感器的各种可设置参数,可以通过RGB摄像头参数查找您所需要的参数。
提醒这里的参数设置都是关于传感器自身的,例如传感器放置的位置等是需要接下来的设置。
传感器生成
这一步是为了建立传感器与模拟器世界的关联。
传感器的生成通过spawn_actor
函数与相关对象连接。通常,传感器是与车辆进行连接,跟随车辆收集周围信息。
按照官方文档的说明,在生成过程中,可以设置连接状态,连接对象,连接类型等参数。
连接状态如下:通常包括传感器相对于车辆的位置,以及自身旋转的状态。
transform = carla.Transform(carla.Location(x=2.5, z=0.7), carla.Rotation(0,270,0))
连接对象是通过attach_to
参数建立的。
sensor = world.spawn_actor(blueprint, transform, attach_to=my_vehicle)
但在相关介绍中,并未找到有关于attach_type
的设置,猜测这个属性属于连接传感器的自身属性。如果猜错了,请大神纠正。
数据接收
carla中利用listening接听传感器数据。每个传感器测量的数据都是基于SensorData
,每个传感器接收的数据类型不一定相同,例如RGB摄像头接收的就是IMAGE对象。你可以用过读取image.raw_data
获取摄像头拍到的图片信息,但是别忘记进行一定的处理之后在输出保存。
# do_something() will be called each time a new image is generated by the camera.
sensor.listen(lambda data: do_something(data))
...
# 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)
通过以上步骤,你就可以在所需要的车辆上添加合适的传感器了。
传感器类型
为了方便传感器的配置,依据官方文档对传感器的分类,我对这部分进行转述。
摄像头类型传感器
摄像头传感器,都是对仿真世界进行拍照的传感器。输出数据的格式基本是carla.Image
。
这种类型的传感器会在每个仿真期间,拍摄一张图像。
传感器 | 输出 | 介绍 |
---|---|---|
深度摄像头 | carla.Image | 在灰度图中呈现视场中元素的深度。 |
RGB摄像头 | carla.Image | 提供清晰的视觉环境。看起来像一个正常的现场照片。 |
光流摄像头 | carla.Image | 呈现相机中每个像素的运动。 |
语义分割摄像头 | carla.Image | 根据视图字段中的元素的标签,呈现具有特定颜色的元素。 |
实例分割摄像头 | carla.Image | 根据视图字段中的元素的标签和唯一的对象 ID,呈现具有特定颜色的元素。 |
动态视觉摄像头 | carla.DVSEventArray | 以事件流的形式异步测量亮度的变化。 |
测量类型传感器
测量类型传感器,只有在特定事件触发时,检测数据。
传感器 | 输出 | 介绍 |
---|---|---|
碰撞传感器 | carla.CollisionEvent | 检索其父参与者(关联车辆)和其他参与者之间的冲突。 |
车道入侵检测器 | carla.LaneInvasionEvent | 当它的父母(关联车辆)越过车道标记时检测。 |
障碍检测器 | carla.ObstacleDetectionEvent | 在其父级(关联车辆)检测可能的障碍。 |
其他类型传感器
示例
安装前置摄像头示例
# 加载传感器蓝图设置
# 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'{640}')
blueprint.set_attribute('image_size_y', f'{480}')
blueprint.set_attribute('fov', '90')
# Set the time in seconds between sensor captures
blueprint.set_attribute('sensor_tick', '1')
# Adjust sensor relative to vehicle
spawn_point = carla.Transform(carla.Location(x=2.5, z=0.7), carla.Rotation(0,90,0))
# spawn the sensor and attach to vehicle.
sensor = world.spawn_actor(blueprint, spawn_point, attach_to=vehicle, attach_type='Rigid attachment')
sensor.listen(lambda data: process_img(data))
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)
img_index += 1
return i3 / 255.0 # normalize
安装雷达摄像头示例:
# ——————————————————————————————————加载雷达传感器——————————————————————————
blueprint_lidar = blueprint_library.find('sensor.lidar.ray_cast')
blueprint_lidar.set_attribute('points_per_second', str(3000))
blueprint_lidar.set_attribute('sensor_tick', '1')
spawn_point_lidar = carla.Transform(carla.Location(0,0,2), carla.Rotation(0,0,0))
lidar = world.spawn_actor(blueprint_lidar, spawn_point_lidar, attach_to=vehicle)
lidar.listen(lambda data: \
data.save_to_disk(os.path.join(savepath, '%06d.ply' % data.frame)))
注意:查看雷达摄像头产生的3d点云,需要安装特定的软件meshlab。
参考: