CARLA 函数大全

1、对小车添加摄像头

def add_camera_to_vehicle(vehicle, spawn_point, im_width=640, im_height=480):
    """
    为车辆添加一个摄像头

    :param vehicle: 车辆对象
    :param spawn_point: 摄像头生成的位置
    :param im_width: 摄像头图像的宽度,默认值为 640
    :param im_height: 摄像头图像的高度,默认值为 480
    :return: 返回摄像头对象
    """
    blueprint_library = vehicle.get_world().get_blueprint_library()
    cam_bp = blueprint_library.find("sensor.camera.rgb")
    cam_bp.set_attribute("image_size_x", f"{im_width}")
    cam_bp.set_attribute("image_size_y", f"{im_height}")
    cam_bp.set_attribute("fov", "110")
    camera = vehicle.get_world().spawn_actor(cam_bp, spawn_point, attach_to=vehicle)
    return camera

使用例子:

#!/usr/bin/env python

# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.

import glob
import os
import sys
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])
##寻找启动的版本的python
except IndexError:
    pass

import carla

import random
import time
IM_WIDTH = 640
IM_HEIGHT = 480
SHOW_PREVIEW = False
actor_list = []
def process_img(image):
    i = np.array(image.raw_data)
   # print(i.shape)
    i2 = i.reshape((IM_HEIGHT, IM_WIDTH, 4))
    i3 = i2[:, :, :3]
    cv2.imshow("", i3)
    cv2.waitKey(1)
    return i3/255.0

def add_camera_to_vehicle(vehicle, spawn_point, im_width=640, im_height=480):
    """
    为车辆添加一个摄像头

    :param vehicle: 车辆对象
    :param spawn_point: 摄像头生成的位置
    :param im_width: 摄像头图像的宽度,默认值为 640
    :param im_height: 摄像头图像的高度,默认值为 480
    :return: 返回摄像头对象
    """
    blueprint_library = vehicle.get_world().get_blueprint_library()
    cam_bp = blueprint_library.find("sensor.camera.rgb")
    cam_bp.set_attribute("image_size_x", f"{im_width}")
    cam_bp.set_attribute("image_size_y", f"{im_height}")
    cam_bp.set_attribute("fov", "110")
    camera = vehicle.get_world().spawn_actor(cam_bp, spawn_point, attach_to=vehicle)
    return camera


def start():
    client = carla.Client("localhost", 2000)
    client.set_timeout(2.2)
    world = client.get_world()
    world = client.load_world('Town06')
    blueprint_library = world.get_blueprint_library()
    bp = blueprint_library.filter("model3")[0]
    print(bp)
    spawn_point = random.choice(world.get_map().get_spawn_points())
    vehicle = world.spawn_actor(bp, spawn_point)
    # vehicle.set_autopilot(True)
    vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=0.0))
    actor_list.append(vehicle)
    spawn_point = carla.Transform(carla.Location(x=1.5, z=2.4))
    camera = add_camera_to_vehicle(vehicle, spawn_point)
    actor_list.append(camera)####对于显示图像十分重要#####
    camera.listen(lambda data: process_img(data))###展示出来
    #camera.listen(lambda image: image.save_to_disk('output_camera.png'))##存入磁盘
try:
    start()
    time.sleep(10)

finally:
    for actor in actor_list:
        actor.destroy()
    print("ALL cleaned up!!!")

2、添加碰撞感应器

def add_collision_sensor_to_vehicle(vehicle):
    """
    为车辆添加一个碰撞感应器

    :param vehicle: 车辆对象
    :return: 返回碰撞感应器对象
    """
    blueprint_library = vehicle.get_world().get_blueprint_library()
    collision_bp = blueprint_library.find("sensor.other.collision")
    collision_sensor = vehicle.get_world().spawn_actor(collision_bp, carla.Transform(), attach_to=vehicle)
    return collision_sensor

3、使用def add_camera_to_vehicle(vehicle, spawn_point, im_width=640, im_height=480)函数,添加两个摄像头,给出两个窗口的实例

import carla
import cv2

# Connect to the Carla simulator
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)

# Get the Carla world object
world = client.get_world()

# Get a random vehicle from the world
vehicle = world.get_random_vehicle()

# Add two cameras to the vehicle
camera1 = add_camera_to_vehicle(vehicle, carla.Transform(carla.Location(x=1.0, y=0.0, z=1.4)))
camera2 = add_camera_to_vehicle(vehicle, carla.Transform(carla.Location(x=-1.0, y=0.0, z=1.4)))

# Create two OpenCV windows to display camera images
cv2.namedWindow("Camera 1", cv2.WINDOW_NORMAL)
cv2.namedWindow("Camera 2", cv2.WINDOW_NORMAL)

# Define a callback function to process camera images
def process_image(image, window_name):
    # Convert the image to a numpy array
    array = np.frombuffer(image.raw_data, dtype=np.uint8).reshape((image.height, image.width, 4))

    # Remove the alpha channel
    array = array[:, :, :3]

    # Flip the image vertically (OpenCV uses a different coordinate system than Carla)
    array = np.flipud(array)

    # Show the image in the window
    cv2.imshow(window_name, array)

# Start listening to camera images
camera1.listen(lambda image: process_image(image, "Camera 1"))
camera2.listen(lambda image: process_image(image, "Camera 2"))

# Keep the windows open until the user closes them
cv2.waitKey(0)
cv2.destroyAllWindows()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值