-
Notifications
You must be signed in to change notification settings - Fork 0
/
carla_first.py
242 lines (197 loc) · 8.39 KB
/
carla_first.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from __future__ import print_function
from __future__ import print_function
import argparse
import logging
import random
import sys
import time
import numpy as np
from matplotlib import pyplot as plt
import cv2
from carla.client import make_carla_client
from carla.sensor import Camera
from carla.settings import CarlaSettings
from carla.tcp import TCPConnectionError
from carla.util import print_over_same_line
show_camera = False
save_to_disk = False
def distance_to_side(sem, depth):
img_size = sem.shape
center = int(img_size[1] / 2)
offroad, left, right, d_left, d_right = -1, -1, -1, -1, -1
for i in range(img_size[0] - 1, -1, -1):
if (sem[i][center] != 7): continue
offroad = i
if (right >= 0) & (left >= 0): continue # already found
for j in range(img_size[1]):
if sem[i][j] == 7: continue
if (j < center):
if (j > left) | (left < 0): left = j
else:
if (j < right) | (right < 0): right = j
if (left >= 0) & (d_left < 0): d_left = depth[i][left] * 1000
if (right >= 0) & (d_right < 0): d_right = depth[i][right] * 1000
return depth[offroad][center] * 1000, d_left, d_right
# 21.8104733116 5.60647282639 8.56894305759
def show_and_save(sensor_data, frame):
if show_camera:
img_sem = sensor_data.get('CameraSemanticSegmentation').data
img = np.copy(sensor_data.get('CameraRGB').data)
# img_sem = warper(img_sem)
img_size = img.shape
for i in range(img_size[0]):
for j in range(img_size[1]):
sem = img_sem[i][j]
if sem == 0:
img[i][j] = [0, 0, 0]
elif sem == 1:
img[i][j] = [255, 0, 0]
elif sem == 2:
img[i][j] = [0, 255, 0]
elif sem == 3:
img[i][j] = [0, 0, 128]
elif sem == 4:
img[i][j] = [0, 0, 255]
elif sem == 5:
img[i][j] = [255, 255, 0]
elif sem == 6:
img[i][j] = [0, 255, 255]
elif sem == 7:
img[i][j] = [255, 0, 255]
elif sem == 8:
img[i][j] = [128, 0, 0]
elif sem == 9:
img[i][j] = [128, 128, 0]
elif sem == 10:
img[i][j] = [0, 128, 0]
elif sem == 11:
img[i][j] = [128, 0, 128]
elif sem == 12:
img[i][j] = [0, 128, 128]
else:
img[i][j] = [255, 255, 255]
plt.imshow(img)
plt.pause(0.001)
if save_to_disk:
image_filename_format = '_images/{:s}/image_{:0>5d}.png'
# for name, image in sensor_data.items():
sensor_data.get('CameraRGB').save_to_disk(image_filename_format.format('CameraRGB', frame))
def run_carla_client(host, port):
# We assume the CARLA server is already waiting for a client to connect at
# host:port. To create a connection we can use the `make_carla_client`
# context manager, it creates a CARLA client object and starts the
# connection. It will throw an exception if something goes wrong. The
# context manager makes sure the connection is always cleaned up on exit.
with make_carla_client(host, port) as client:
print('CarlaClient connected')
# Create a CarlaSettings object. This object is a wrapper around
# the CarlaSettings.ini file. Here we set the configuration we
# want for the new episode.
settings = CarlaSettings()
settings.set(
SynchronousMode=True,
SendNonPlayerAgentsInfo=True,
NumberOfVehicles=20,
NumberOfPedestrians=40,
WeatherId=1) # random.choice([1, 3, 7, 8, 14]))
settings.randomize_seeds()
# Now we want to add a couple of cameras to the player vehicle.
# We will collect the images produced by these cameras every
# frame.
# The default camera captures RGB images of the scene.
camera0 = Camera('CameraRGB')
# Set image resolution in pixels.
camera0.set_image_size(800, 600)
# Set its position relative to the car in centimeters.
camera0.set_position(30, 0, 130)
settings.add_sensor(camera0)
# Let's add another camera producing ground-truth depth.
camera1 = Camera('CameraDepth', PostProcessing='Depth')
camera1.set_image_size(800, 600)
camera1.set_position(30, 0, 130)
settings.add_sensor(camera1)
camera2 = Camera('CameraSemanticSegmentation', PostProcessing='SemanticSegmentation')
camera2.set_image_size(800, 600)
camera2.set_position(30, 0, 130)
settings.add_sensor(camera2)
# Now we load these settings into the server. The server replies
# with a scene description containing the available start spots for
# the player. Here we can provide a CarlaSettings object or a
# CarlaSettings.ini file as string.
scene = client.load_settings(settings)
# Choose one player start at random.
number_of_player_starts = len(scene.player_start_spots)
player_start = 0 # random.randint(0, max(0, number_of_player_starts - 1))
# Notify the server that we want to start the episode at the
# player_start index. This function blocks until the server is ready
# to start the episode.
print('Starting...')
client.start_episode(player_start)
if show_camera:
plt.ion()
plt.show()
frame = 0
while (True):
frame += 1
# Read the data produced by the server this frame.
measurements, sensor_data = client.read_data()
# Print some of the measurements.
print_measurements(measurements)
show_and_save(sensor_data, frame)
sem = sensor_data.get('CameraSemanticSegmentation').data
depth = sensor_data.get('CameraDepth').data
start_offroad, left, right = distance_to_side(sem, depth)
print(start_offroad, left, right)
# We can access the encoded data of a given image as numpy
# array using its "data" property. For instance, to get the
# depth value (normalized) at pixel X, Y
#
# depth_array = sensor_data['CameraDepth'].data
# value_at_pixel = depth_array[Y, X]
#
client.send_control(
steer=0,
throttle=1,
brake=False,
hand_brake=False,
reverse=False)
def print_measurements(measurements):
number_of_agents = len(measurements.non_player_agents)
player_measurements = measurements.player_measurements
message = 'Vehicle at ({pos_x:.1f}, {pos_y:.1f}), '
message += '{speed:.2f} km/h, '
message += 'Collision: {{vehicles={col_cars:.0f}, pedestrians={col_ped:.0f}, other={col_other:.0f}}}, '
message += '{other_lane:.0f}% other lane, {offroad:.0f}% off-road, '
message += '({agents_num:d} non-player agents in the scene)'
message = message.format(
pos_x=player_measurements.transform.location.x / 100, # cm -> m
pos_y=player_measurements.transform.location.y / 100,
speed=player_measurements.forward_speed,
col_cars=player_measurements.collision_vehicles,
col_ped=player_measurements.collision_pedestrians,
col_other=player_measurements.collision_other,
other_lane=100 * player_measurements.intersection_otherlane,
offroad=100 * player_measurements.intersection_offroad,
agents_num=number_of_agents)
print_over_same_line(message)
def main():
argparser = argparse.ArgumentParser(description=__doc__)
args = argparser.parse_args()
log_level = logging.DEBUG
logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
while True:
try:
run_carla_client('localhost', 2000)
print('Done.')
return
except TCPConnectionError as error:
logging.error(error)
time.sleep(1)
except Exception as exception:
logging.exception(exception)
sys.exit(1)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')