简体   繁体   中英

How do I save the camera Data of Carla to my disk?

I am working with the Carla Simulator and want to make my own Dataset for semantic segmentation. But the problem is I can not save them on my computer. I got a camera and everything but I can not save it to the disk. I am working with Jupyter Notebooks and the kernel just shut down himself. In the end, I want to have png data of my camera. The problem is at the end ( image.save_to_disk )

import glob
import os
import sys
import random
from turtle import width
import carla
from matplotlib import image
import tensorflow as tf
import cv2
import numpy as np
import time
import argparse
import imutils

print("")
# Laden von Carla,Library,Map, erstellen des Spectators
client = carla.Client('localhost', 2000)
client.set_timeout(500.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
spectator = world.get_spectator()
bp_lib = world.get_blueprint_library()  
spawn_points = world.get_map().get_spawn_points() 


```
#Laden von Carla,Library,Map, erstellen des Spectators
client = carla.Client('localhost', 2000)
client.set_timeout(500.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
spawnpoints = world.get_map().get_spawn_points()
spectator = world.get_spectator()

#Nun Einstellungen zum Fahrzeug
vehicle_blueprint = blueprint_library.find('vehicle.audi.etron')
#Fahrzeug wird an einem zufälligen Spawnpunkt gespawnt
vehicle = world.try_spawn_actor(vehicle_blueprint,random.choice(spawnpoints))
transform = carla.Transform(vehicle.get_transform().transform(carla.Location(x=2,z=0.5)),vehicle.get_transform().rotation)
spectator.set_transform(transform)

#Kamera
camera_blueprint = blueprint_library.find('sensor.camera.rgb')
camera_transform = carla.Transform(carla.Location(z=2,x=0.5))
camera = world.spawn_actor(camera_blueprint,camera_transform,attach_to=vehicle)

def camera_callback(image,data):
    data['image'] = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))

image_width = camera_blueprint.get_attribute("image_size_x").as_int()
image_height = camera_blueprint.get_attribute("image_size_y").as_int()
camera_data = {'image': np.zeros((image_height,image_width, 4))}

camera.listen(lambda image: camera_callback(image, camera_data))



#Anzeigen des Bildes
cv2.namedWindow('Camera', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Camera', camera_data['image'])
cv2.waitKey(1)
while True:
    img = cv2.imshow('Camera', camera_data['image'])
    vehicle.set_autopilot(True)
    camera.listen(lambda image: image.save_to_disk(r"C:\Users\Joshi\Desktop\CARLA_0.9.13\WindowsNoEditor\PythonAPI\examples\out",color_converter=None))

    if cv2.waitKey(1) == ord('p'):
        client.reload_world()
        break
cv2.destroyAllWindows()
```

```
`

Why don't you save the image inside camera_callback itself?

Eg:

def camera_callback(image, data):
    capture = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))
    data['image'] = capture
    cv2.imwrite(f"{image.frame}.png", capture)

Or:

def camera_callback(image, data):
    data['image'] = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))
    image.save_to_disk(f"{image.frame}.png")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM