Created
September 16, 2019 22:49
-
-
Save soulslicer/abaf642244c882b436e4d537c7d5aa56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de | |
# Barcelona (UAB). | |
# | |
# This work is licensed under the terms of the MIT license. | |
# For a copy, see <https://opensource.org/licenses/MIT>. | |
import glob | |
import os | |
import sys | |
import time | |
try: | |
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % ( | |
sys.version_info.major, | |
sys.version_info.minor, | |
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0]) | |
except IndexError: | |
pass | |
import carla | |
import random | |
import time | |
import numpy as np | |
import cv2 | |
counter = 0 | |
def main(): | |
actor_list = [] | |
all_id = [] | |
# In this tutorial script, we are going to add a vehicle to the simulation | |
# and let it drive in autopilot. We will also create a camera attached to | |
# that vehicle, and save all the images generated by the camera to disk. | |
try: | |
# First of all, we need to create the client that will send the requests | |
# to the simulator. Here we'll assume the simulator is accepting | |
# requests in the localhost at port 2000. | |
client = carla.Client('localhost', 2000) | |
client.set_timeout(2.0) | |
# Once we have a client we can retrieve the world that is currently | |
# running. | |
world = client.get_world() | |
# The world contains the list blueprints that we can use for adding new | |
# actors into the simulation. | |
blueprint_library = world.get_blueprint_library() | |
### Listed below | |
# Now let's filter all the blueprints of type 'vehicle' and choose one | |
# at random. | |
bp = random.choice(blueprint_library.filter('vehicle')) | |
# A blueprint contains the list of attributes that define a vehicle's | |
# instance, we can read them and modify some of them. For instance, | |
# let's randomize its color. | |
if bp.has_attribute('color'): | |
color = random.choice(bp.get_attribute('color').recommended_values) | |
bp.set_attribute('color', color) | |
# Now we need to give an initial transform to the vehicle. We choose a | |
# random transform from the list of recommended spawn points of the map. | |
transform = random.choice(world.get_map().get_spawn_points()) | |
transform.location.x=9.33 | |
transform.location.y=134 | |
transform.location.z=1.84 | |
transform.rotation.pitch=0 | |
transform.rotation.yaw=0 | |
transform.rotation.roll=0 | |
# So let's tell the world to spawn the vehicle. | |
vehicle = world.spawn_actor(bp, transform) | |
# It is important to note that the actors we create won't be destroyed | |
# unless we call their "destroy" function. If we fail to call "destroy" | |
# they will stay in the simulation even after we quit the Python script. | |
# For that reason, we are storing all the actors we create so we can | |
# destroy them afterwards. | |
actor_list.append(vehicle) | |
print('created %s' % vehicle.type_id) | |
# Let's put the vehicle to drive around. | |
vehicle.set_autopilot(True) | |
for j in range(0, 10): | |
# Let's add now a "depth" camera attached to the vehicle. Note that the | |
# transform we give here is now relative to the vehicle. | |
camera_bp = blueprint_library.find('sensor.camera.depth') | |
#fov = camera_bp.get_attribute('fov').recommended_values | |
#fov.ActorAttribute.value = 45 | |
#print(fov) | |
camera_bp.set_attribute('fov', '140') | |
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4)) | |
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle) | |
actor_list.append(camera) | |
print('created %s' % camera.type_id) | |
# Now we register the function that will be called each time the sensor | |
# receives an image. In this example we are saving the image to disk | |
# converting the pixels to gray-scale. | |
cc = carla.ColorConverter.Depth | |
def imageCb(image): | |
#print("A") | |
global counter | |
#image.convert(cc) | |
counter+=1 | |
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8")) | |
array = np.reshape(array, (image.height, image.width, 4)) # RGBA format | |
array = array[:, :, :3] # Take only RGB | |
array = array[:, :, ::-1] # BGR | |
array = array.astype(np.float32) # 2ms | |
gray_depth = ((array[:,:,0] + array[:,:,1]*256.0 + array[:,:,2]*256.0*256.0)/((256.0*256.0*256.0) - 1)) # 2.5ms | |
gray_depth = 1000 * gray_depth | |
# image.frame | |
#cv2.imwrite('_out/%06d.png' % counter, gray_depth) | |
camera.listen(imageCb) | |
# Oh wait, I don't like the location we gave to the vehicle, I'm going | |
# to move it a bit forward. | |
location = vehicle.get_location() | |
location.x += 40 | |
vehicle.set_location(location) | |
print('moved vehicle to %s' % location) | |
time.sleep(5) | |
finally: | |
print('destroying actors') | |
for actor in actor_list: | |
actor.destroy() | |
print('done.') | |
if __name__ == '__main__': | |
main() | |
# [ActorBlueprint(id=static.prop.advertisement,tags=[advertisement, prop, static]) | |
# ActorBlueprint(id=static.prop.doghouse,tags=[doghouse, prop, static]) | |
# ActorBlueprint(id=static.prop.travelcase,tags=[travelcase, prop, static]) | |
# ActorBlueprint(id=static.prop.trashcan02,tags=[trashcan02, prop, static]) | |
# ActorBlueprint(id=static.prop.trashbag,tags=[trashbag, prop, static]) | |
# ActorBlueprint(id=static.prop.trampoline,tags=[prop, trampoline, static]) | |
# ActorBlueprint(id=static.prop.trafficcone02,tags=[trafficcone02, prop, static]) | |
# ActorBlueprint(id=static.prop.trafficcone01,tags=[prop, trafficcone01, static]) | |
# ActorBlueprint(id=static.prop.trashcan03,tags=[prop, trashcan03, static]) | |
# ActorBlueprint(id=static.prop.swingcouch,tags=[prop, swingcouch, static]) | |
# ActorBlueprint(id=static.prop.streetfountain,tags=[streetfountain, prop, static]) | |
# ActorBlueprint(id=static.prop.streetsign04,tags=[streetsign04, prop, static]) | |
# ActorBlueprint(id=static.prop.streetsign,tags=[streetsign, prop, static]) | |
# ActorBlueprint(id=static.prop.slide,tags=[slide, prop, static]) | |
# ActorBlueprint(id=static.prop.wateringcan,tags=[wateringcan, prop, static]) | |
# ActorBlueprint(id=static.prop.shoppingbag,tags=[shoppingbag, prop, static]) | |
# ActorBlueprint(id=static.prop.shop01,tags=[shop01, prop, static]) | |
# ActorBlueprint(id=static.prop.platformgarbage01,tags=[platformgarbage01, prop, static]) | |
# ActorBlueprint(id=static.prop.plastictable,tags=[plastictable, prop, static]) | |
# ActorBlueprint(id=static.prop.plantpot08,tags=[plantpot08, prop, static]) | |
# ActorBlueprint(id=static.prop.trashcan01,tags=[prop, trashcan01, static]) | |
# ActorBlueprint(id=static.prop.plantpot07,tags=[plantpot07, prop, static]) | |
# ActorBlueprint(id=static.prop.plantpot06,tags=[plantpot06, prop, static]) | |
# ActorBlueprint(id=static.prop.plantpot05,tags=[prop, plantpot05, static]) | |
# ActorBlueprint(id=static.prop.pergola,tags=[pergola, prop, static]) | |
# ActorBlueprint(id=static.prop.mobile,tags=[prop, mobile, static]) | |
# ActorBlueprint(id=static.prop.shoppingcart,tags=[shoppingcart, prop, static]) | |
# ActorBlueprint(id=static.prop.maptable,tags=[maptable, prop, static]) | |
# ActorBlueprint(id=static.prop.trafficwarning,tags=[trafficwarning, prop, static]) | |
# ActorBlueprint(id=static.prop.mailbox,tags=[mailbox, prop, static]) | |
# ActorBlueprint(id=static.prop.gnome,tags=[gnome, prop, static]) | |
# ActorBlueprint(id=static.prop.glasscontainer,tags=[glasscontainer, prop, static]) | |
# ActorBlueprint(id=static.prop.trashcan05,tags=[trashcan05, prop, static]) | |
# ActorBlueprint(id=static.prop.gardenlamp,tags=[gardenlamp, prop, static]) | |
# ActorBlueprint(id=static.prop.garbage06,tags=[garbage06, prop, static]) | |
# ActorBlueprint(id=static.prop.swing,tags=[swing, prop, static]) | |
# ActorBlueprint(id=static.prop.guitarcase,tags=[guitarcase, prop, static]) | |
# ActorBlueprint(id=static.prop.garbage05,tags=[garbage05, prop, static]) | |
# ActorBlueprint(id=static.prop.ironplank,tags=[prop, ironplank, static]) | |
# ActorBlueprint(id=static.prop.garbage02,tags=[prop, garbage02, static]) | |
# ActorBlueprint(id=static.prop.garbage01,tags=[garbage01, prop, static]) | |
# ActorBlueprint(id=static.prop.fountain,tags=[fountain, prop, static]) | |
# ActorBlueprint(id=static.prop.dirtdebris03,tags=[prop, dirtdebris03, static]) | |
# ActorBlueprint(id=static.prop.constructioncone,tags=[prop, constructioncone, static]) | |
# ActorBlueprint(id=static.prop.colacan,tags=[prop, colacan, static]) | |
# ActorBlueprint(id=static.prop.chainbarrier,tags=[chainbarrier, prop, static]) | |
# ActorBlueprint(id=static.prop.vendingmachine,tags=[vendingmachine, prop, static]) | |
# ActorBlueprint(id=static.prop.busstop,tags=[prop, busstop, static]) | |
# ActorBlueprint(id=static.prop.brokentile04,tags=[brokentile04, prop, static]) | |
# ActorBlueprint(id=static.prop.creasedbox03,tags=[creasedbox03, prop, static]) | |
# ActorBlueprint(id=static.prop.brokentile03,tags=[brokentile03, prop, static]) | |
# ActorBlueprint(id=static.prop.brokentile01,tags=[brokentile01, prop, static]) | |
# ActorBlueprint(id=static.prop.box03,tags=[box03, prop, static]) | |
# ActorBlueprint(id=static.prop.box01,tags=[box01, prop, static]) | |
# ActorBlueprint(id=static.prop.purse,tags=[prop, purse, static]) | |
# ActorBlueprint(id=static.prop.bin,tags=[bin, prop, static]) | |
# ActorBlueprint(id=static.prop.bikeparking,tags=[bikeparking, prop, static]) | |
# ActorBlueprint(id=static.prop.bike helmet,tags=[bike helmet, prop, static]) | |
# ActorBlueprint(id=static.prop.garbage04,tags=[garbage04, prop, static]) | |
# ActorBlueprint(id=static.prop.bench03,tags=[bench03, prop, static]) | |
# ActorBlueprint(id=static.prop.container,tags=[container, prop, static]) | |
# ActorBlueprint(id=static.prop.bench02,tags=[bench02, prop, static]) | |
# ActorBlueprint(id=static.prop.bench01,tags=[bench01, prop, static]) | |
# ActorBlueprint(id=static.prop.streetsign01,tags=[streetsign01, prop, static]) | |
# ActorBlueprint(id=static.prop.barbeque,tags=[prop, barbeque, static]) | |
# ActorBlueprint(id=static.prop.plantpot03,tags=[plantpot03, prop, static]) | |
# ActorBlueprint(id=static.prop.clothcontainer,tags=[clothcontainer, prop, static]) | |
# ActorBlueprint(id=static.prop.atm,tags=[atm, prop, static]) | |
# ActorBlueprint(id=walker.pedestrian.0014,tags=[0014, pedestrian, walker]) | |
# ActorBlueprint(id=walker.pedestrian.0011,tags=[0011, pedestrian, walker]) | |
# ActorBlueprint(id=walker.pedestrian.0010,tags=[0010, pedestrian, walker]) | |
# ActorBlueprint(id=static.trigger.friction,tags=[friction, trigger, static]) | |
# ActorBlueprint(id=static.prop.brokentile02,tags=[prop, brokentile02, static]) | |
# ActorBlueprint(id=walker.pedestrian.0008,tags=[0008, pedestrian, walker]) | |
# ActorBlueprint(id=static.prop.kiosk_01,tags=[kiosk_01, prop, static]) | |
# ActorBlueprint(id=static.prop.creasedbox02,tags=[creasedbox02, prop, static]) | |
# ActorBlueprint(id=walker.pedestrian.0007,tags=[0007, pedestrian, walker]) | |
# ActorBlueprint(id=walker.pedestrian.0006,tags=[0006, pedestrian, walker]) | |
# ActorBlueprint(id=walker.pedestrian.0004,tags=[pedestrian, 0004, walker]) | |
# ActorBlueprint(id=static.prop.dirtdebris02,tags=[prop, dirtdebris02, static]) | |
# ActorBlueprint(id=walker.pedestrian.0003,tags=[0003, pedestrian, walker]) | |
# ActorBlueprint(id=walker.pedestrian.0002,tags=[pedestrian, 0002, walker]) | |
# ActorBlueprint(id=static.prop.plantpot04,tags=[plantpot04, prop, static]) | |
# ActorBlueprint(id=walker.pedestrian.0001,tags=[0001, pedestrian, walker]) | |
# ActorBlueprint(id=sensor.other.lane_invasion,tags=[lane_invasion, other, sensor]) | |
# ActorBlueprint(id=walker.pedestrian.0012,tags=[0012, pedestrian, walker]) | |
# ActorBlueprint(id=sensor.other.gnss,tags=[gnss, other, sensor]) | |
# ActorBlueprint(id=walker.pedestrian.0009,tags=[0009, pedestrian, walker]) | |
# ActorBlueprint(id=sensor.other.collision,tags=[collision, other, sensor]) | |
# ActorBlueprint(id=static.prop.motorhelmet,tags=[motorhelmet, prop, static]) | |
# ActorBlueprint(id=sensor.lidar.ray_cast,tags=[ray_cast, lidar, sensor]) | |
# ActorBlueprint(id=sensor.camera.semantic_segmentation,tags=[semantic_segmentation, camera, sensor]) | |
# ActorBlueprint(id=static.prop.chainbarrierend,tags=[chainbarrierend, prop, static]) | |
# ActorBlueprint(id=sensor.camera.rgb,tags=[rgb, camera, sensor]) | |
# ActorBlueprint(id=static.prop.briefcase,tags=[briefcase, prop, static]) | |
# ActorBlueprint(id=walker.pedestrian.0013,tags=[0013, pedestrian, walker]) | |
# ActorBlueprint(id=vehicle.audi.etron,tags=[etron | |
# Audi, vehicle]) | |
# ActorBlueprint(id=walker.pedestrian.0005,tags=[0005, pedestrian, walker]) | |
# ActorBlueprint(id=vehicle.diamondback.century,tags=[diamondback, century, vehicle]) | |
# ActorBlueprint(id=vehicle.gazelle.omafiets,tags=[omafiets, gazelle, vehicle]) | |
# ActorBlueprint(id=vehicle.bh.crossbike,tags=[crossbike, bh, vehicle]) | |
# ActorBlueprint(id=vehicle.kawasaki.ninja,tags=[ninja, kawasaki, vehicle]) | |
# ActorBlueprint(id=static.prop.plantpot01,tags=[plantpot01, prop, static]) | |
# ActorBlueprint(id=vehicle.yamaha.yzf,tags=[yzf, yamaha, vehicle]) | |
# ActorBlueprint(id=vehicle.harley-davidson.low rider,tags=[low rider, harley-davidson, vehicle]) | |
# ActorBlueprint(id=vehicle.lincoln.mkz2017,tags=[mkz2017, lincoln, vehicle]) | |
# ActorBlueprint(id=vehicle.volkswagen.t2,tags=[t2, volkswagen, vehicle]) | |
# ActorBlueprint(id=vehicle.toyota.prius,tags=[prius, toyota, vehicle]) | |
# ActorBlueprint(id=static.prop.plasticchair,tags=[prop, plasticchair, static]) | |
# ActorBlueprint(id=static.prop.clothesline,tags=[prop, clothesline, static]) | |
# ActorBlueprint(id=sensor.camera.depth,tags=[camera, depth, sensor]) | |
# ActorBlueprint(id=vehicle.tesla.model3,tags=[model3, tesla, vehicle]) | |
# ActorBlueprint(id=static.prop.streetbarrier,tags=[streetbarrier, prop, static]) | |
# ActorBlueprint(id=static.prop.creasedbox01,tags=[creasedbox01, prop, static]) | |
# ActorBlueprint(id=vehicle.seat.leon,tags=[leon, seat, vehicle]) | |
# ActorBlueprint(id=static.prop.plantpot02,tags=[plantpot02, prop, static]) | |
# ActorBlueprint(id=static.prop.garbage03,tags=[garbage03, prop, static]) | |
# ActorBlueprint(id=vehicle.nissan.patrol,tags=[nissan, patrol, vehicle]) | |
# ActorBlueprint(id=vehicle.nissan.micra,tags=[micra, nissan, vehicle]) | |
# ActorBlueprint(id=vehicle.ford.mustang,tags=[ford, mustang, vehicle]) | |
# ActorBlueprint(id=static.prop.trashcan04,tags=[trashcan04, prop, static]) | |
# ActorBlueprint(id=static.prop.box02,tags=[box02, prop, static]) | |
# ActorBlueprint(id=vehicle.mini.cooperst,tags=[mini, cooperst, vehicle]) | |
# ActorBlueprint(id=vehicle.jeep.wrangler_rubicon,tags=[wrangler_rubicon, jeep, vehicle]) | |
# ActorBlueprint(id=vehicle.dodge_charger.police,tags=[dodge_charger, police, vehicle]) | |
# ActorBlueprint(id=static.prop.table,tags=[table, prop, static]) | |
# ActorBlueprint(id=static.prop.plasticbag,tags=[plasticbag, prop, static]) | |
# ActorBlueprint(id=vehicle.citroen.c3,tags=[c3, citroen, vehicle]) | |
# ActorBlueprint(id=static.prop.barrel,tags=[barrel, prop, static]) | |
# ActorBlueprint(id=vehicle.chevrolet.impala,tags=[chevrolet, impala, vehicle]) | |
# ActorBlueprint(id=vehicle.carlamotors.carlacola,tags=[carlacola, carlamotors, vehicle]) | |
# ActorBlueprint(id=vehicle.bmw.isetta,tags=[isetta, bmw, vehicle]) | |
# ActorBlueprint(id=static.prop.dirtdebris01,tags=[dirtdebris01, prop, static]) | |
# ActorBlueprint(id=sensor.other.obstacle,tags=[obstacle, other, sensor]) | |
# ActorBlueprint(id=vehicle.bmw.grandtourer,tags=[grandtourer, bmw, vehicle]) | |
# ActorBlueprint(id=controller.ai.walker,tags=[walker | |
# Ai, controller]) | |
# ActorBlueprint(id=static.prop.shoppingtrolley,tags=[shoppingtrolley, prop, static]) | |
# ActorBlueprint(id=vehicle.mercedes-benz.coupe,tags=[mercedes-benz, coupe, vehicle]) | |
# ActorBlueprint(id=vehicle.audi.tt,tags=[tt | |
# Audi, vehicle]) | |
# ActorBlueprint(id=vehicle.audi.a2,tags=[a2 | |
# Audi, vehicle])] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment