Commit 477df5d2 authored by Sacha Pronost's avatar Sacha Pronost
Browse files

Clean

parent 5611e60d
Loading
Loading
Loading
Loading
+259 −0
Changes for carla/Dataset_Carla.ipynb: 259 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id:a5a81071 tags:

``` python
import carla
import time
import numpy as np
import keyboard
import cv2
import random


# Connect to the CARLA simulator
client = carla.Client("localhost", 2000)
client.set_timeout(10.0)
world = client.get_world()

```

%% Cell type:code id: tags:

``` python
carla = client.load_world('Town01')
```

%% Cell type:code id: tags:

``` python
# Obtention des coordonnées de la caméra
spectator = world.get_spectator()
camera = world.get_spectator()
camera_transform = camera.get_transform()
camera_location = camera_transform.location
camera_rotation = camera_transform.rotation
print(f"Camera location: {camera_location}")
print(f"Camera rotation: {camera_rotation}")
```

%% Output

    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    ~\AppData\Local\Temp\ipykernel_3172\3509461821.py in <module>
    ----> 1 camera = world.get_spectator()
          2 camera_transform = camera.get_transform()
          3 camera_location = camera_transform.location
          4 camera_rotation = camera_transform.rotation
          5 print(f"Camera location: {camera_location}")
    RuntimeError: internal error: unable to find spectator

%% Cell type:code id: tags:

``` python
# Tentative de faire créer un long trajet pour faire un dataset valide
# Concluion : Très long à faire pour le peu d'image obtenu
def generation_trajet():
    blueprint = world.get_blueprint_library().find("vehicle.tesla.model3")
    spawn_point = carla.Transform(carla.Location(x=330.845581, y=195.527863, z=3), carla.Rotation(pitch=0.000000, yaw=180.000000, roll=0.000000))
    vehicle = world.spawn_actor(blueprint, spawn_point)

    blueprint = world.get_blueprint_library().find("vehicle.audi.a2")
    spawn_point = carla.Transform(carla.Location(x=309.853577, y=199.227951, z=3))
    vehicle2 = world.spawn_actor(blueprint, spawn_point)

    blueprint = random.choice(world.get_blueprint_library().filter('vehicle'))
    spawn_point = carla.Transform(carla.Location(x=270.685883, y=198.910965, z=3))
    vehicle3 = world.spawn_actor(blueprint, spawn_point)

    blueprint = random.choice(world.get_blueprint_library().filter('vehicle'))
    spawn_point = carla.Transform(carla.Location(x=245.644699, y=199.270432, z=3))
    vehicle4 = world.spawn_actor(blueprint, spawn_point)

    blueprint = random.choice(world.get_blueprint_library().filter('vehicle'))
    spawn_point = carla.Transform(carla.Location(x=197.804962, y=199.209579, z=3))
    vehicle5 = world.spawn_actor(blueprint, spawn_point)

    blueprint = random.choice(world.get_blueprint_library().filter('walker'))
    spawn_point = carla.Transform(carla.Location(x=306.125458, y=191.453323, z=3))
    pieton1 = world.spawn_actor(blueprint, spawn_point)

    blueprint = random.choice(world.get_blueprint_library().filter('walker'))
    spawn_point = carla.Transform(carla.Location(x=285.016296, y=191.565521, z=3))
    pieton2 = world.spawn_actor(blueprint, spawn_point)



generation_trajet()
```

%% Cell type:code id: tags:

``` python
for v in world.get_actors():
    v.destroy()
```

%% Cell type:code id:0c29a8da tags:

``` python
# Fonction créer qui permet de changer de météo afin d'améliorer notre modèle de réseau de neurones dans le futur
weatherList = np.array([carla.WeatherParameters.WetCloudySunset,carla.WeatherParameters.CloudyNoon,carla.WeatherParameters.WetNoon,carla.WeatherParameters.WetCloudyNoon,carla.WeatherParameters.MidRainyNoon,carla.WeatherParameters.HardRainNoon,carla.WeatherParameters.SoftRainNoon,carla.WeatherParameters.ClearSunset,carla.WeatherParameters.CloudySunset,carla.WeatherParameters.WetSunset,carla.WeatherParameters.MidRainSunset,carla.WeatherParameters.HardRainSunset,carla.WeatherParameters.SoftRainSunset])
np.random.shuffle(weatherList)
world.set_weather(weatherList[0])
```

%% Cell type:code id:ff384592 tags:

``` python
weather_parameters = {
    'ClearNoon': carla.WeatherParameters.ClearNoon,
    'WetCloudySunset': carla.WeatherParameters.WetCloudySunset,
    'SoftRainNoon': carla.WeatherParameters.SoftRainNoon,
    'MidRainSunset': carla.WeatherParameters.MidRainSunset,
    'HardRainSunset': carla.WeatherParameters.HardRainSunset,
    'ClearSunset': carla.WeatherParameters.ClearSunset,
    'WetSunset': carla.WeatherParameters.WetSunset,
    'SoftRainSunset': carla.WeatherParameters.SoftRainSunset
}
random_key = random.choice(list(weather_parameters.keys()))
random_value = weather_parameters[random_key]
world.set_weather(random_value)
```

%% Cell type:code id:94260e0d tags:

``` python
# Charger le véhicule principal
def charger_vehicle():
    bp_lib = world.get_blueprint_library()
    spawn_points = world.get_map().get_spawn_points()
    vehicule_bp = bp_lib.find('vehicle.lincoln.mkz_2020')
    vehicle =world.try_spawn_actor(vehicule_bp,random.choice(spawn_points))
    return vehicle
```

%% Cell type:code id:680a1e1a tags:

``` python
# Faire spawner plusieurs voiture dans le monde
def spanwnVehicle(n):
    for i in range (n):
        vehicle_bp = random.choice(bp_lib.filter('vehicle'))
        npc = world.try_spawn_actor(vehicle_bp,random.choice(spawn_points))
```

%% Cell type:code id: tags:

``` python
# Faire supprimer tous les véhicules du monde
def destruction():
    for v in world.get_actors().filter('*vehicle*'):
        v.destroy()
```

%% Cell type:code id:33170479 tags:

``` python
def isCirculation(value):
    for v in world.get_actors().filter('*vehicle*'):
        v.set_autopilot(value)
```

%% Cell type:code id:bae6d847 tags:

``` python
def attach_cam(vehicle):
    # Génération d'une caméra RGB
    camera = world.get_blueprint_library().find("sensor.camera.rgb")
    camera.set_attribute("image_size_x", str(800))
    camera.set_attribute("image_size_y", str(600))

    # Génération d'une caméra sémantique
    cameraS = world.get_blueprint_library().find("sensor.camera.semantic_segmentation")
    cameraS.set_attribute("image_size_x", str(800))
    cameraS.set_attribute("image_size_y", str(600))

    # Attacher les caméras au véhicule
    spawn_point = carla.Transform(carla.Location(z=2))
    sensor = world.spawn_actor(camera, spawn_point, attach_to=vehicle)
    sensorS = world.spawn_actor(cameraS, spawn_point, attach_to=vehicle)
    return sensor,sensorS
```

%% Cell type:code id:47110331 tags:

``` python
# Fonction qui permet de récupérer les images des caméras
def process_img(image,typeCam,weather):
    time.sleep(2)
    # Si on souhaite avoir des images de segmentation selon la palette de couleurs de CityScapes ( évite d'avoir une image noir car l'image contient les numéros des classes )
   # if(typeCam == "seg"):
   #      image.convert(carla.ColorConverter.CityScapesPalette)
    # Convert the image to a numpy array
    np_image = np.array(image.raw_data)
    np_image = np_image.reshape((image.height, image.width, 4))

    # Compteurs globaux pour les images RGB et Segmentation
    global counterRGB
    counterRGB += 1

    global counterSEG
    counterSEG += 1

    #Enregistrement des images RGB et Segmentation
    if(typeCam == "rgb" ):
        filename = "dataset/"+weather+"/rgb/image{}.png".format(counterRGB)
        filenameAll = "dataset/All_Image/rgb/image{}.png".format(counterRGB)
        cv2.imwrite(filename, np_image)
        cv2.imwrite(filenameAll,np_image)
    elif(typeCam == "seg"):
        filename = "dataset/"+weather+"/segmentation/image{}.png".format(counterSEG)
        filenameAll = "dataset/All_Image/segmentation/image{}.png".format(counterSEG)
        cv2.imwrite(filename, np_image)
        cv2.imwrite(filenameAll,np_image)
```

%% Cell type:code id:f268b919 tags:

``` python
def dataset():
    weather_parameters = {
    'ClearNoon': carla.WeatherParameters.ClearNoon,
    'WetCloudySunset': carla.WeatherParameters.WetCloudySunset,
    'SoftRainNoon': carla.WeatherParameters.SoftRainNoon,
    'MidRainSunset': carla.WeatherParameters.MidRainSunset,
    'HardRainSunset': carla.WeatherParameters.HardRainSunset,
    'ClearSunset': carla.WeatherParameters.ClearSunset,
    'WetSunset': carla.WeatherParameters.WetSunset,
    'SoftRainSunset': carla.WeatherParameters.SoftRainSunset
    }
    list_weather = list(weather_parameters.keys())
    for weather in list_weather :
        params = weather_parameters[weather]
        world.set_weather(params)
        vehicle = charger_vehicle()
        spanwnVehicle(15)
        isCirculation(True)
        sensor,sensorS = attach_cam(vehicle)

        # Enregistrement des images
        sensor.listen(lambda im:process_img(im,"rgb",weather))
        sensorS.listen(lambda im:process_img(im,"seg",weather))

        # Lance la simulation pendant 60 secondes
        time.sleep(60)
        # Arrête la simulation, arrête l'enregistrement des images et détruit les véhicules
        sensor.stop()
        sensorS.stop()

        destruction()


bp_lib = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()
# Initialisation des compteurs
counterRGB = 0
counterSEG = 0

dataset()
```

carla/Scenarios.ipynb

0 → 100644
+350 −0

File added.

Preview size limit exceeded, changes collapsed.

simulator/Camera_sensors.ipynb

deleted100644 → 0
+0 −159
Changes for simulator/Camera_sensors.ipynb: 0 added lines, 159 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id:bfaff6d1 tags:

``` python
# Fichier qui vient d'une vidéo youtube Carla qui permet de voir comment gérer les capteurs Carla
# https://www.youtube.com/watch?v=om8klsBj4rc
```

%% Cell type:code id:156fe94b tags:

``` python
# Ce fichier a été modifier pour répondre à notre problème (choix des caméras utilisées, possibilité de changer de météo...)
```

%% Cell type:code id:6e6b6dec-c0f6-4496-9b75-d74bcba07183 tags:

``` python
import carla
import math
import random
import time
import numpy as np
import cv2

# Connect the client and set up bp library and spawn points
client = carla.Client('localhost', 2000)
world = client.get_world()
bp_lib = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()
```

%% Cell type:code id:ce48bb74 tags:

``` python
# Fonction créer qui permet de changer de météo afin d'améliorer notre modèle de réseau de neurones dans le futur
weatherList = np.array([carla.WeatherParameters.WetCloudySunset,carla.WeatherParameters.CloudyNoon,carla.WeatherParameters.WetNoon,carla.WeatherParameters.WetCloudyNoon,carla.WeatherParameters.MidRainyNoon,carla.WeatherParameters.HardRainNoon,carla.WeatherParameters.SoftRainNoon,carla.WeatherParameters.ClearSunset,carla.WeatherParameters.CloudySunset,carla.WeatherParameters.WetSunset,carla.WeatherParameters.MidRainSunset,carla.WeatherParameters.HardRainSunset,carla.WeatherParameters.SoftRainSunset])
np.random.shuffle(weatherList)
world.set_weather(weatherList[0])
```

%% Cell type:code id:c9675857 tags:

``` python
# Add the ego vehicle
vehicle_bp = bp_lib.find('vehicle.lincoln.mkz_2020')
vehicle = world.try_spawn_actor(vehicle_bp, spawn_points[79])
```

%% Cell type:code id:93c94c6a-4c3e-487b-8de9-823e440f3c43 tags:

``` python

# Move the spectator behind the vehicle to view it
spectator = world.get_spectator()
transform = carla.Transform(vehicle.get_transform().transform(carla.Location(x=-4,z=2.5)),vehicle.get_transform().rotation)
spectator.set_transform(transform)

```

%% Cell type:code id:b500fb9b tags:

``` python
# Add traffic
for i in range(50):
    vehicle_bp = random.choice(bp_lib.filter('vehicle'))
    npc = world.try_spawn_actor(vehicle_bp, random.choice(spawn_points))
```

%% Cell type:code id:85f22a80-52d1-4706-9231-5835bfc27a2e tags:

``` python
# Set traffic in motion
for v in world.get_actors().filter('*vehicle*'):
    v.set_autopilot(True)
```

%% Cell type:code id:2db29235-7d72-43e3-9b34-89a92ff9fbf2 tags:

``` python
# Set initial camera translation
camera_init_trans = carla.Transform(carla.Location(z=2))

# On choisit de types de caméras (caméra rgb et caméra de detection sémantique)
camera_bp = bp_lib.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', '500')
camera_bp.set_attribute('image_size_y', '500')
camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=vehicle)
sem_camera_bp = bp_lib.find('sensor.camera.semantic_segmentation')
sem_camera_bp.set_attribute('image_size_x', '500')
sem_camera_bp.set_attribute('image_size_y', '500')
sem_camera = world.spawn_actor(sem_camera_bp, camera_init_trans, attach_to=vehicle)
```

%% Cell type:code id:6bb223b5-c5a0-4797-9055-79afafd24349 tags:

``` python
# Define respective callbacks
def rgb_callback(image, data_dict):
    data_dict['rgb_image'] = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))

def sem_callback(image, data_dict):
    image.convert(carla.ColorConverter.CityScapesPalette)
    data_dict['sem_image'] = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))
```

%% Cell type:code id:74ea1188-96b4-452a-9932-c8ab42241588 tags:

``` python
# Initialise parameters and data
image_w = camera_bp.get_attribute("image_size_x").as_int()
image_h = camera_bp.get_attribute("image_size_y").as_int()


sensor_data = {'rgb_image': np.zeros((image_h, image_w, 4)),
               'sem_image': np.zeros((image_h, image_w, 4))}

# OpenCV named window for display
cv2.namedWindow('All cameras', cv2.WINDOW_AUTOSIZE)

# Tile all data in one array
top_row = np.concatenate((sensor_data['rgb_image'], sensor_data['sem_image']),axis=1)

# Display with imshow
cv2.imshow('All cameras',top_row)
cv2.waitKey(1)

# Set sensors recording
camera.listen(lambda image: rgb_callback(image, sensor_data))
sem_camera.listen(lambda image: sem_callback(image, sensor_data))

# Indefinite while loop
while True:

    # Tile camera images into one array
    top_row = np.concatenate((sensor_data['rgb_image'], sensor_data['sem_image']),axis=1)

    # Dispaly with imshow
    cv2.imshow('All cameras',top_row)

    # Break loop if user presses q
    if cv2.waitKey(1) == ord('q'):
        break

# Stop sensors and destroy OpenCV window
camera.stop()
sem_camera.stop()
cv2.destroyAllWindows()
```

%% Cell type:code id:c8c88800 tags:

``` python
for v in world.get_actors().filter('*vehicle*'):
    v.destroy()
```

%% Cell type:code id:4a2bcb8c tags:

``` python
```

simulator/fondamental.ipynb

deleted100644 → 0
+0 −316

File deleted.

Preview size limit exceeded, changes collapsed.

test_dir/image.png

deleted100644 → 0
−593 KiB
Loading image diff...
Loading