CARLA simulation car with two left and right cameras and a roof lidar

#!/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])
## Find the version of python started
except IndexError:
    pass

import carla

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

def collision_data(self, event):
    self.collision_hist.append(event)
def add_camera_to_vehicle(vehicle, spawn_point, im_width=640, im_height=480):
    """
    Add a camera to the vehicle

    :param vehicle: vehicle object
    :param spawn_point: The position where the camera spawns
    :param im_width: The width of the camera image, the default value is 640
    :param im_height: The height of the camera image, the default value is 480
    :return: return camera object
    """
    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 add_camera_to_vehicle(vehicle, spawn_point, im_width=640, im_height=480):
    """
    Add a camera to the vehicle

    :param vehicle: vehicle object
    :param spawn_point: The position where the camera spawns
    :param im_width: The width of the camera image, the default value is 640
    :param im_height: The height of the camera image, the default value is 480
    :return: return camera object
    """
    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 add_collision_sensor_to_vehicle(vehicle):
    """
    Add a crash sensor to the vehicle

    :param vehicle: vehicle object
    :return: Returns the collision sensor object
    """
    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

def process_lidar_measurement(measurement):
    points = np.frombuffer(measurement.raw_data, dtype=np.dtype('f4'))
    points = np. reshape(points, (int(points. shape[0] / 4), 4))
    lidar_data = np.array(points[:, :3])
    # Display the point cloud image
    image = np.zeros((600, 800, 3), dtype=np.uint8)
    lidar_data *= 50
    lidar_data += [400, 300, 0]
    lidar_data = lidar_data.astype(np.int32)
    for point in lidar_data:
        cv2. circle(image, (point[0], point[1]), 1, (0, 0, 255), -1)
    cv2.imshow('Lidar data', image)
    cv2.waitKey(1)
def add_lidar_to_vehicle(vehicle, spawn_point, num_points=100000):
    # define radar parameters
    blueprint_library = vehicle. get_world(). get_blueprint_library()
    lidar_bp = blueprint_library. find("sensor. lidar. ray_cast")
    lidar_bp.set_attribute("channels", "360") # 360°
    lidar_bp.set_attribute("range", "500")
    lidar_bp.set_attribute("points_per_second", "100000")
    lidar_bp.set_attribute("rotation_frequency", "200") # set rotation_frequency to 40
    lidar_bp.set_attribute("upper_fov", "50") # set upper_fov to 30
    lidar_bp.set_attribute("lower_fov", "-45")
    lidar = vehicle.get_world().spawn_actor(lidar_bp, spawn_point, attach_to=vehicle)
    return lidar


def start():
    client = carla. Client("localhost", 2000)
    client.set_timeout(2.2)
    world = client. get_world()
    world = client.load_world('Town05')
    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_1 = carla. Transform(carla. Location(x=1.5, y=0, z=2.4))
    camera_1 = add_camera_to_vehicle(vehicle, spawn_point_1)
    spawn_point_2 = carla. Transform(carla. Location(x=-1.5, y=0, z=2.4))
    camera_2 = add_camera_to_vehicle(vehicle, spawn_point_2)
    spawn_point_3 = carla. Transform(carla. Location(x=0, y=0, z=2.5))
    lidar_1 = add_lidar_to_vehicle(vehicle, spawn_point_3)
    # configure and start radar
    actor_list.append(lidar_1)
    col = add_collision_sensor_to_vehicle(vehicle)
    actor_list.append(camera_1)#### is very important for displaying images#####
    actor_list.append(camera_2) ####It is very important for displaying images#####
    actor_list.append(col)
    camera_1.listen(lambda data: process_img(data, "camera_1"))
    camera_2.listen(lambda data: process_img(data, "camera_2"))
    lidar_1.listen(process_lidar_measurement)
    col.listen(lambda event: collision_data(event))
try:
    start()
    time. sleep(100)

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

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treeHomepageOverview 258581 people are studying systematically