Carla系列——3.Cara模拟器添加摄像头

这一节承接之前生成车辆的内容(Carla生成车辆),在向Carla中添加vehicle后,继续添加Camera sensor

1.添加车辆的完整代码为:

import glob
import os
import sys
import time
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
 
actor_list =  []
try:
	#create client
	client = carla.Client('localhost', 2000)
	client.set_timeout(2.0)
	#world connection
	world = client.get_world() 
	#get blueprint libarary
	blueprint_library = world.get_blueprint_library()
	#Choose a vehicle blueprint which name is model3 and select the first one
	bp = blueprint_library.filter("model3")[0]
	print(bp)
	#Returns a list of recommended spawning points and random choice one from it
	spawn_point = random.choice(world.get_map().get_spawn_points())
	#spawn vehicle to the world by spawn_actor method
	vehicle = world.spawn_actor(bp,spawn_point)
	#control the vehicle
	vehicle.set_autopilot(enabled=True)
	# vehicle.apply_control(carla.VehicleControl(throttle=0.1,steer=0.0))
	#add vehicle to the actor list
	actor_list.append(vehicle)
	time.sleep(50)
finally:
	for actor in actor_list:
		actor.destroy()
	print("All cleaned up!")

2.添加camera, camera在carla中也算是一种actor(carla核心概念),所以生成camera的方法类似于生成vehicle。

    cam_bp = blueprint_library.find("sensor.camera.rgb")

3.设置camera的属性

    IM_WIDTH = 640
    IM_HEIIGHT = 480 
    #set the attribute of camera
    cam_bp.set_attribute("image_size_x",f"{IM_WIDTH}")
    cam_bp.set_attribute("image_size_y",f"{IM_HEIIGHT}")
    cam_bp.set_attribute("fov","110")

4.添加camera到world中,并在actor list 中添加camera。

    #add camera sensor to the vehicle
    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)

5.监听camera 数据,并处理camera的数据

    sensor.listen(lambda data: process_img(data))
    def process_img(image):
        i = np.array(image.raw_data)
        i2 = i.reshape((IM_HEIIGHT,IM_WIDTH,4))
        i3 = i2[:,:,:3]
        cv2.imshow("",i3)
        cv2.waitKey(10)
        return i3/255.0

6.最终完整的代码为

import glob
import os
import sys
import time
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

IM_WIDTH = 640
IM_HEIIGHT = 480 

def process_img(image):
	i = np.array(image.raw_data)
	i2 = i.reshape((IM_HEIIGHT,IM_WIDTH,4))
	i3 = i2[:,:,:3]
	cv2.imshow("",i3)
	cv2.waitKey(10)
	return i3/255.0

actor_list =  []
try:
	#create client
	client = carla.Client('localhost', 2000)
	client.set_timeout(2.0)
	#world connection
	world = client.get_world() 
	#get blueprint libarary
	blueprint_library = world.get_blueprint_library()
	#Choose a vehicle blueprint which name is model3 and select the first one
	bp = blueprint_library.filter("model3")[0]
	print(bp)
	#Returns a list of recommended spawning points and random choice one from it
	spawn_point = random.choice(world.get_map().get_spawn_points())
	#spawn vehicle to the world by spawn_actor method
	vehicle = world.spawn_actor(bp,spawn_point)
	#control the vehicle
	vehicle.set_autopilot(enabled=True)
	# vehicle.apply_control(carla.VehicleControl(throttle=0.1,steer=0.0))
	#add vehicle to the actor list
	actor_list.append(vehicle)
	#as the same use find method to find a sensor actor.
	cam_bp = blueprint_library.find("sensor.camera.rgb")
	#set the attribute of camera
	cam_bp.set_attribute("image_size_x",f"{IM_WIDTH}")
	cam_bp.set_attribute("image_size_y",f"{IM_HEIIGHT}")
	cam_bp.set_attribute("fov","110")
	#add camera sensor to the vehicle
	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(50)
finally:
	for actor in actor_list:
		actor.destroy()
	print("All cleaned up!")

7.保存代码为spawn_camera.py

8.运行CarlaUE4.exe(位于WindowsNoEditor\文件夹下)

9.打开终端运行spawn_camera.py (注意terminal的路径应该是你存放py文件的位置) python spawn_camera.py 

10.至此完成了添加vehicle以及camera

 
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值