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。
参考:
史上最全Carla教程