data-collector
data-collector copied to clipboard
Using Image Converter within Carla Client
I've used some portion of your code. In Client what I did to save the camera images:
cam_bp = world.get_blueprint_library().find('sensor.camera.rgb')
cam_bp.set_attribute("image_size_x",str(800))
cam_bp.set_attribute("image_size_y",str(600))
cam_bp.set_attribute("fov",str(105))
cam_location = carla.Location(2,0,1)
cam_rotation = carla.Rotation(0,90,0)
cam_transform = carla.Transform(cam_location,cam_rotation)
ego_cam = world.spawn_actor(cam_bp,cam_transform,attach_to=ego_vehicle, attachment_type=carla.AttachmentType.Rigid)
def to_bgra_array(image):
"""Convert a CARLA raw image to a BGRA numpy array."""
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
return array
def to_rgb_array(image):
"""Convert a CARLA raw image to a RGB numpy array."""
array = to_bgra_array(image)
# Convert BGRA to RGB.
array = array[:, :, :3]
array = array[:, :, ::-1]
return array
def image_callback(image):
print("Image is coming")
image_no = image.frame
image_rgb = to_bgra_array(image)
path = '~/output/' + str(image_no)
cv2.imwrite(path +'.jpg', image_rgb)
ego_cam.listen(lambda image: image_callback(image))