Search code examples
pythoncarla

Camera.listen() not giving proper image output in Carla


The listen method of the Carla sensor has the ability to take images. When I attach an RBG sensors to my vehicle, instead of returning a clear image every frame, it returns what I think (I am not sure if that is what the issue is) a bunch of frames grouped together into one image.

Below is my code:

import carla
import random
import time
from PIL import Image
Image


# Connect to the client and retrieve the world object
client = carla.Client('localhost', 2000)
world = client.get_world()
spectator = world.get_spectator()
getLocation = spectator.get_transform()

# Set the spectator with an empty transform
spectator.set_transform(carla.Transform())

# Get the blueprint library and filter for the vehicle blueprints
vehicle_blueprints = world.get_blueprint_library().filter('*vehicle*')

# Get the map's spawn points
spawn_points = world.get_map().get_spawn_points()

# Spawn 50 vehicles randomly distributed throughout the map
for _ in range(50):
    world.try_spawn_actor(random.choice(vehicle_blueprints), random.choice(spawn_points))

ego_vehicle = world.spawn_actor(random.choice(vehicle_blueprints), random.choice(spawn_points))

# Create a transform to place the camera on top of the vehicle
camera_init_trans = carla.Transform(carla.Location(z=4))

# We create the camera through a blueprint that defines its properties
camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')

# We spawn the camera and attach it to our ego vehicle
camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=ego_vehicle)

# Register the callback function to receive the image data
camera.listen(lambda image: image.save_to_disk('out/%06d.png' % image.frame))

EgoLocation = ego_vehicle.get_location()

for vehicle in world.get_actors().filter('*vehicle*'):
    vehicle.set_autopilot(True)

spectator.set_transform(carla.Transform(EgoLocation))
time.sleep(10)
camera.stop()

This line is the problem:

# Register the callback function to receive the image data
camera.listen(lambda image: image.save_to_disk('out/%06d.png' % image.frame))

What it should be returning according to Carla documentation: What it should be returning according to Carla documentation

What it is actually returning: What it is actually returning

and its not just one of those its returning. Its multiple: and its not just one of those its returning. Its multiple:

I tried importing Image from pillow and using Image's .save method to try and save the photo that way which didn't work. I also tried to make a save function which didn't work.


Solution

  • Add this code

    self.IM_WIDTH = 640
    self.IM_HEIGHT = 480
    camera_bp.set_attribute('image_size_x', str(self.IM_WIDTH))
    camera_bp.set_attribute('image_size_y', str(self.IM_HEIGHT))
    

    After

    camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')