Skip to content
Snippets Groups Projects
Commit 2994f0c1 authored by Armin Co's avatar Armin Co
Browse files

Camera update

parent e88e8237
Branches
No related tags found
No related merge requests found
""" Carla.org environment""" """ Carla.org environment"""
import glob import glob
import os import os
import sys
import random import random
import sys
import numpy as np import numpy as np
import pygame import pygame
import math
import atexit
from steering_wheel import Controller from steering_wheel import Controller
# find carla module # find carla module
try: try:
sys.path.append(glob.glob('/media/armin/Games/carla/PythonAPI/carla/dist/carla-*%d.%d-%s.egg' % ( CARLA_PATH='/media/armin/Games/carla/PythonAPI/carla/dist/carla-*%d.%d-%s.egg'
sys.path.append(glob.glob(CARLA_PATH % (
sys.version_info.major, sys.version_info.major,
sys.version_info.minor, sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0]) 'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
...@@ -20,31 +21,37 @@ except IndexError: ...@@ -20,31 +21,37 @@ except IndexError:
import carla import carla
class Camera: class Camera:
""" Add camera sensor to the carla world """ """ Add camera sensor to the carla world """
sensor = None sensor = None
display = None
surface = None surface = None
display = None
width = None width = None
height = None height = None
surface = None surface = None
camera_type = None
def __init__(self, world, width=640, height=480): def __init__(self, world, camera_type='rgb', width=1280, height=720):
self.width = width self.width = width
self.height = height self.height = height
self.camera_type = camera_type
self.display = pygame.display.set_mode( self.display = pygame.display.set_mode(
(width, height), (width, height),
pygame.HWSURFACE | pygame.DOUBLEBUF) pygame.HWSURFACE | pygame.DOUBLEBUF)
self.display.fill((0,0,0)) self.display.fill((0,0,0))
camera_blueprint = world.world.get_blueprint_library().find('sensor.camera.rgb') camera_blueprint = world.world.get_blueprint_library().find('sensor.camera.' + camera_type)
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
camera_blueprint.set_attribute('image_size_x', f'{self.width}') camera_blueprint.set_attribute('image_size_x', f'{self.width}')
camera_blueprint.set_attribute('image_size_y', f'{self.height}') camera_blueprint.set_attribute('image_size_y', f'{self.height}')
camera_blueprint.set_attribute('fov', '110') camera_blueprint.set_attribute('fov', '95')
camera_transform = carla.Transform(carla.Location(x=-3.0, z=2.2))
self.sensor = world.world.spawn_actor(camera_blueprint, camera_transform, attach_to=world.player) self.sensor = world.world.spawn_actor(camera_blueprint, camera_transform, attach_to=world.player)
self.sensor.listen(lambda data: self.process_img(data)) self.sensor.listen(lambda data: self.process_img(data))
def process_img(self, data): def process_img(self, data):
""" Callback for rgb-camera sensor. """
if self.camera_type == 'semantic_segmentation':
data.convert(carla.ColorConverter.CityScapesPalette)
array = np.frombuffer(data.raw_data, dtype=np.dtype("uint8")) array = np.frombuffer(data.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (self.height, self.width, 4)) array = np.reshape(array, (self.height, self.width, 4))
array = array[:, :, :3] array = array[:, :, :3]
...@@ -52,10 +59,12 @@ class Camera: ...@@ -52,10 +59,12 @@ class Camera:
self.surface = pygame.surfarray.make_surface(array.swapaxes(0, 1)) self.surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
def on_update(self): def on_update(self):
""" Update the pygame display. """
if self.surface is not None: if self.surface is not None:
self.display.blit(self.surface, (0, 0)) self.display.blit(self.surface, (0, 0))
pygame.display.flip() pygame.display.flip()
class World: class World:
""" Wrapper for the carla environment, incl. player/vehicle """ """ Wrapper for the carla environment, incl. player/vehicle """
player = None player = None
...@@ -70,47 +79,61 @@ class World: ...@@ -70,47 +79,61 @@ class World:
self.reset() self.reset()
def reset(self): def reset(self):
""" Remove and create new player/vehicle. """
self.destroy() self.destroy()
self.spawn_player() self.spawn_player()
def spawn_player(self): def spawn_player(self):
""" Add a vehicle to the world. """
while self.player is None: while self.player is None:
blueprint = random.choice(self.blueprint_library.filter('vehicle')) blueprint = random.choice(self.blueprint_library.filter('model3'))
position = random.choice(self.spawn_points) position = random.choice(self.spawn_points)
self.player = self.world.try_spawn_actor(blueprint, position) self.player = self.world.try_spawn_actor(blueprint, position)
start_location = self.player.get_location()
print(str(start_location))
# start_location.x = 288.0
# start_location.y = 55.0
# self.player.set_location(start_location)
def destroy(self): def destroy(self):
""" Remove vehicle from the world. """
if self.player is not None: if self.player is not None:
self.player.destroy() self.player.destroy()
def step(self, action): def step(self, action):
""" Apply controls to vehicle. """
self.player.apply_control(action) self.player.apply_control(action)
# print(str(self.player.get_location()))
# print(str(self.player.get_velocity()))
class CarlaEnvironment: class CarlaEnvironment:
world = None world = None
client = None client = None
def __init__(self, host="127.0.0.1", port=2000, width=1280, height=720): def __init__(self, host="127.0.0.1", port=2000):
self.client = carla.Client(host, port) self.client = carla.Client(host, port)
self.client.set_timeout(2.0) self.client.set_timeout(2.0)
self.client.load_world('Town07')
self.world = World(self.client.get_world()) self.world = World(self.client.get_world())
def step(self, action): def step(self, action):
control = carla.VehicleControl(throttle=action[0], steer=action[2], brake=action[1]) control = carla.VehicleControl(throttle=action[0], steer=action[2], brake=action[1], reverse=action[3])
self.world.step(control) self.world.step(control)
if __name__ == "__main__": if __name__ == "__main__":
LOCALHOST="127.0.0.1"
SERVER="192.168.188.20"
pygame.init() pygame.init()
clock = pygame.time.Clock()
env = CarlaEnvironment() env = CarlaEnvironment(host=LOCALHOST)
cam = Camera(env.world) cam = Camera(env.world, camera_type='semantic_segmentation')
ctrl = Controller() ctrl = Controller()
running = True
while ctrl.is_running(): while ctrl.is_running():
clock.tick(60)
ctrl.on_update() ctrl.on_update()
env.step(ctrl.get_action()) env.step(ctrl.get_action())
cam.on_update() cam.on_update()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment