安装所需模块

首先下载carla
链接: https://pan.baidu.com/s/1JTfm93EjYNXBgeUrN6Z8lQ 提取码: qxf4
注意:python的版本使用3.7 不然的话会报错
安装tensorflow

pip install tensorflow==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install tensorflow-gpu==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

carla创建一个汽车并添加摄像头展示摄像头获取的画面

import glob
import os
import sys
import random
import time
import numpy as np
import cv2

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

IMG_WIDTH=600
IMG_HEIGHT=480
def process_img(image):
    i=np.array(image.raw_data)
    print(i.shape)
    i2=i.reshape((IMG_HEIGHT,IMG_WIDTH,4))
    i3=i2[:,:,:3] #所有的高度和rgb,不要alpha通道,快速获取RGB
    #print(dir(image))
    cv2.imshow('',i3)
    cv2.waitKey(1)
    return i3/255.0

actor_list=[]

try:

    ##生成一个车辆
    client=carla.Client(host='127.0.0.1', port=2000) #连接主机
    client.set_timeout(2.0)

    world=client.get_world()

    blueprint_library=world.get_blueprint_library()

    bp=blueprint_library.filter('model3')[0]#获取车辆
    print(bp)

    spaw_point=random.choice(world.get_map().get_spawn_points())#在随机点生成车辆 ,随机点
    vehicle=world.spawn_actor(bp,spaw_point)#随机点生成车辆
    #vehicle.set_autopilot(True) #基于规则的行驶
    vehicle.apply_control(carla.VehicleControl(throttle=1.0,steer=0.0))
    actor_list.append(vehicle)

    ##在车辆上方安装传感器,以满足后面的碰撞检测
    cam_bp=blueprint_library.find('sensor.camera.rgb')
    cam_bp.set_attribute('image_size_x',f"{IMG_WIDTH}")
    cam_bp.set_attribute('image_size_x', f"{IMG_HEIGHT}")
    cam_bp.set_attribute('fov','110')

    spawn_point=carla.Transform(carla.Location(x=2.5,z=0.7))#将摄像机的位置进行设置
    sensor=world.spawn_actor(cam_bp,spawn_point,attach_to=vehicle)
    actor_list.append(sensor)
    sensor.listen(lambda data:process_img(data))
    time.sleep(10)
finally:
    for actor in actor_list:
        actor.destroy()
        actor.destroy()
        print('All cleaned up!')

图片的reshape报错

ValueError: cannot reshape array of size 149184 into shape (28,28,1)
由于图片的w,h,c相乘不等于149184所导致的。也就是说这张图片的shape不能是(28,28,1)。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐