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

 

你可能感兴趣的:(自动驾驶)