前言
关于显示Carla中RGB相机的画面,我找到的几乎都是使用cv来显示的画面的,但是经过我自己尝试发现,利用cv来显示的画面帧数非常低,画面及其不流畅。如果你尝试过Carla自带的demo就会发现demo中用pygame制作窗口显示的画面就十分流畅,所以我就试着模仿demo利用pygame来显示RGB相机画面。
先给大家推荐一篇文章,里面十分详细地介绍了Carla中大部分的传感器,大家可以根据这篇文章来试用cv来显示传感器中的画面。
https://blog.csdn.net/weixin_44169614/article/details/119824465
开始主题
1.创建Pygame窗口:
下面是使用pygame编写游戏的基本结构:
def main():
#初始化游戏并创建一个屏幕对象
pygame.init()
pygame.font.init()
display = pygame.display.set_mode((1920, 1080), pygame.HWSURFACE | pygame.DOUBLEBUF) # 窗口可以自己修改大小
pygame.display.set_caption("Camera Vision") # 窗口名称
# 开始游戏的主循环
while True:
# 监视键盘和鼠标事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# display.blit()将画面加载到窗口,暂时先不加画面
# 让最近绘制的屏幕可见
pygame.display.flip()
2.连接Carla:略
3.Carla内放置一辆车:
这个很多教程的啦,我就不解释了,用类来写方便管理文章来源:https://www.toymoban.com/news/detail-589013.html
class CarManager(object):
def __init__(self, world): # world = client.get_world()
self.world = world
self.ego_vehicle = None
blueprint_library = world.get_blueprint_library()
self.ego_vehicle_bp = blueprint_library.find('vehicle.audi.a2')
self.ego_vehicle_bp.set_attribute('color', '0, 0, 0')
transform = random.choice(world.get_map().get_spawn_points())
# transform = carla.Transform(carla.Location(x=-78.034149, y=12.967159, z=0.6), carla.Rotation(yaw=-179.84079))
self.ego_vehicle = self.world.spawn_actor(self.ego_vehicle_bp, transform)
self.ego_vehicle.set_autopilot(True)
4.给车加传感器,处理传感器数据
class SensorManager(object):
def __init__(self, world, car): # world = client.get_world() car = CarManager(world)
self.surface = None
self.world = world
self.car = car
# 添加RGB相机,相机的图像大小记得和pygame窗口一样哦
blueprint_library = world.get_blueprint_library()
self.camera_bp = blueprint_library.find('sensor.camera.rgb')
self.camera_bp.set_attribute('image_size_x', '1920')
self.camera_bp.set_attribute('image_size_y', '1080')
self.camera_bp.set_attribute('fov', '110')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
self.camera = self.world.spawn_actor(self.camera_bp, camera_transform, attach_to=self.car.ego_vehicle)
# 设置监听传感器的处理函数
self.camera.listen(lambda image: self._parse_image(image))
# 这个函数是绘制图像的,提供给pygame来显示
def render(self, display):
if self.surface is not None:
display.blit(self.surface, (0, 0))
# 对传感器的raw图像进行处理
def _parse_image(self, image):
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
array = array[:, :, :3]
array = array[:, :, ::-1]
self.surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
5.完整代码:
import glob
import carla
import sys
import os
import numpy as np
import pygame
from numpy import random
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
class SensorManager(object):
def __init__(self, world, car):
self.surface = None
self.world = world
self.car = car
# 添加RGB相机
blueprint_library = world.get_blueprint_library()
self.camera_bp = blueprint_library.find('sensor.camera.rgb')
self.camera_bp.set_attribute('image_size_x', '1920')
self.camera_bp.set_attribute('image_size_y', '1080')
self.camera_bp.set_attribute('fov', '110')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
self.camera = self.world.spawn_actor(self.camera_bp, camera_transform, attach_to=self.car.ego_vehicle)
# set the callback function
self.camera.listen(lambda image: self._parse_image(image))
def render(self, display):
if self.surface is not None:
display.blit(self.surface, (0, 0))
def _parse_image(self, image):
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
array = array[:, :, :3]
array = array[:, :, ::-1]
self.surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
class CarManager(object):
def __init__(self, world):
self.world = world
self.ego_vehicle = None
blueprint_library = world.get_blueprint_library()
self.ego_vehicle_bp = blueprint_library.find('vehicle.audi.a2')
self.ego_vehicle_bp.set_attribute('color', '0, 0, 0')
# transform = random.choice(world.get_map().get_spawn_points())
transform = carla.Transform(carla.Location(x=-78.034149, y=12.967159, z=0.6), carla.Rotation(yaw=-179.84079))
self.ego_vehicle = self.world.spawn_actor(self.ego_vehicle_bp, transform)
self.ego_vehicle.set_autopilot(True)
def main():
pygame.init()
pygame.font.init()
try:
client = carla.Client('127.0.0.1', 2000)
client.set_timeout(20.0)
world = client.get_world()
display = pygame.display.set_mode((1920, 1080), pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption("Camera Vision")
car = CarManager(world)
sensor = SensorManager(world, car)
clock = pygame.time.Clock()
while True:
clock.tick_busy_loop(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
world.wait_for_tick()
sensor.render(display)
pygame.display.flip()
finally:
car.ego_vehicle.destroy()
sensor.camera.stop()
sensor.camera.destroy()
print('done.')
pygame.quit()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print(' - Exited by user -')
各位加油,冲冲冲!!!文章来源地址https://www.toymoban.com/news/detail-589013.html
到了这里,关于利用Pygame实时显示Carla中RGB相机的画面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!