import carla
import pygame
import numpy as np
import cv2
import random # For randomizing pedestrian behavior
# ... (CARLA setup, vehicle spawning, and camera attachment remain the same)
# Connect to the CARLA Simulator
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Choose a blueprint and spawn the vehicle
blueprint_library = world.get_blueprint_library()
vehicle_bp = blueprint_library.find('vehicle.tesla.model3')
spawn_point = world.get_map().get_spawn_points()[0]
vehicle = world.spawn_actor(vehicle_bp, spawn_point)
# Enable autopilot for the main vehicle
vehicle.set_autopilot(True)
# Create and attach camera sensor
camera_bp = blueprint_library.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', '800')
camera_bp.set_attribute('image_size_y', '600')
camera_bp.set_attribute('fov', '110')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
# --- Traffic and Pedestrian Spawning (Improved) ---
def get_safe_spawn_point(world, actor_list, min_distance=5.0): # Minimum distance between actors
spawn_points = world.get_map().get_spawn_points()
while True:
transform = random.choice(spawn_points)
if all(transform.location.distance(actor.get_location()) > min_distance for actor in actor_list):
return transform # Found a safe spawn point
traffic_manager = client.get_trafficmanager()
traffic_manager.set_global_distance_to_leading_vehicle(2.0)
# traffic_manager.set_synchronous_mode(True) # Optional
vehicle_list = [] # Store spawned vehicles
for _ in range(10):
bp = random.choice(blueprint_library.filter('vehicle.*'))
if bp.has_attribute('color'):
bp.set_attribute('color', random.choice(bp.get_attribute('color').recommended_values))
transform = get_safe_spawn_point(world, vehicle_list)
vehicle = world.try_spawn_actor(bp, transform)
if vehicle is not None:
vehicle.set_autopilot(True)
vehicle_list.append(vehicle)
pedestrian_list = [] # Store spawned pedestrians
for _ in range(5):
bp = random.choice(blueprint_library.filter('walker.pedestrian.*'))
transform = get_safe_spawn_point(world, pedestrian_list)
pedestrian = world.try_spawn_actor(bp, transform)
if pedestrian is not None:
# Control pedestrian behavior (optional)
control = carla.WalkerControl()
control.direction = carla.Vector3D(1, 0, 0)
control.speed = 1.0 # You can adjust the speed
pedestrian.apply_control(control)
# ... (control pedestrian behavior if desired)
pedestrian_list.append(pedestrian)
# --- Pedestrian Crossing Behavior ---
def make_pedestrian_cross(pedestrian, crosswalk_location):
pedestrian.set_simulate_physics(True)
# Get the nearest waypoint on a drivable road
waypoint = world.get_map().get_waypoint(
crosswalk_location,
project_to_road=True,
lane_type=carla.LaneType.Any
)
if waypoint:
lane_width = waypoint.lane_width
destination = waypoint.transform.location + carla.Location(x=lane_width)
# Create a walker control object
walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker')
walker_controller = world.spawn_actor(walker_controller_bp, carla.Transform(), pedestrian)
# Start the walker controller
walker_controller.start()
# Set the destination for the walker controller
walker_controller.go_to_location(destination)
else:
print("Could not find a suitable waypoint for pedestrian crossing.")
# Get Crosswalks
crosswalks = world.get_map().get_crosswalks()
# Choose a Random Crosswalk and Pedestrian
selected_crosswalk = random.choice(crosswalks)
selected_pedestrian = random.choice(pedestrian_list)
# Make the Pedestrian Cross
make_pedestrian_cross(selected_pedestrian, selected_crosswalk)
# ... (Rest of the Pygame event loop remains the same)
# Initialize Pygame (same as before)
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("CARLA Dashboard")
# Function to process the camera image
#def process_img(image, screen):
# image_data = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
# image_reshaped = np.reshape(image_data, (image.height, image.width, 4))
# image_bgr = cv2.cvtColor(image_reshaped, cv2.COLOR_RGBA2BGR)
#
# dashboard_image = cv2.resize(image_bgr, (800, 600))
# pygame_surface = pygame.surfarray.make_surface(dashboard_image.swapaxes(0, 1))
# screen.blit(pygame_surface, (0, 0))
def process_img(image, screen):
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
array = array[:, :, :3]
# Drop the alpha channel
array = array[:, :, ::-1] # Convert BGR to RGB
# ... (Your traffic light detection using OpenCV could go here)
image_surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
screen.blit(image_surface, (0, 0))
# Start listening for camera data (same as before)
camera.listen(lambda image: process_img(image, screen))
# Enable autopilot for automatic driving
vehicle.set_autopilot(True)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# Autopilot handles the driving automatically
# No need for manual control input
# Update the Pygame display
pygame.display.flip()
Comments