-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.py
80 lines (60 loc) · 2.04 KB
/
test1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import glob
import os
import sys
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
IM_WIDTH = 640
IM_HEIGHT = 480
def process_img(image):
i = np.array(image.raw_data)
i2 = i.reshape((IM_HEIGHT, IM_WIDTH, 4))
i3 = i2[:, :, :3]
cv2.imshow("", i3)
cv2.waitKey(1)
return i3/255.0
actor_list = []
try:
client = carla.Client('127.0.0.1', 2000)
client.set_timeout(2.0)
# world = client.get_world()
world = client.load_world('Town01')
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.filter('model3')[0]
print(bp)
spawn_point = random.choice(world.get_map().get_spawn_points())
spawn_point = world.get_map().get_spawn_points()[150]
vehicle = world.spawn_actor(bp, spawn_point)
# vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=0.0))
# vehicle.set_autopilot(True) # if you just wanted some NPCs to drive.
actor_list.append(vehicle)
# https://carla.readthedocs.io/en/latest/cameras_and_sensors
# get the blueprint for this sensor
blueprint = blueprint_library.find('sensor.camera.rgb')
# change the dimensions of the image
blueprint.set_attribute('image_size_x', f'{IM_WIDTH}')
blueprint.set_attribute('image_size_y', f'{IM_HEIGHT}')
blueprint.set_attribute('fov', '110')
# Adjust sensor relative to vehicle
spawn_point = carla.Transform(carla.Location(x=2.5, z=0.7))
# spawn the sensor and attach to vehicle.
sensor = world.spawn_actor(blueprint, spawn_point, attach_to=vehicle)
# add sensor to list of actors
actor_list.append(sensor)
# do something with this sensor
sensor.listen(lambda data: process_img(data))
time.sleep(500)
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')