英文:
Camera.listen() not giving proper image output in Carla
问题
camera.listen(lambda image: image.save_to_disk('out/%06d.png' % image.frame))
这一行是问题所在。根据Carla文档,它应该返回单独的图像帧。但实际上,它返回了多个帧组合在一张图像中。你尝试导入了Pillow库的Image模块并使用Image的.save
方法来保存图片,但没有成功。你还尝试创建一个保存函数,也没有成功。
英文:
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 is actually returning:
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.
答案1
得分: 0
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')
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论