-
Notifications
You must be signed in to change notification settings - Fork 2
/
car_chase_demo_v2.py
398 lines (311 loc) · 15 KB
/
car_chase_demo_v2.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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 cv2
from pathlib import Path
import numpy as np
import pygame
import math
import weakref
import pickle
from lane_tracking.lane_track import get_speed, send_control
from car_chasing.util.carla_util import carla_vec_to_np_array, carla_img_to_array, CarlaSyncMode, find_weather_presets, get_font, should_quit #draw_image
from car_chasing.util.geometry_util import dist_point_linestring
# For birdeye view
from carla_birdeye_view import (
BirdViewProducer,
BirdView,
DEFAULT_HEIGHT,
DEFAULT_WIDTH,
BirdViewCropType,
)
from carla_birdeye_view.mask import PixelDimensions
# For Object avoidance
import imageio
from copy import deepcopy
def draw_image(surface, image, image2, location1, location2, blend=False, record=False,driveName='',smazat=[]):
if record:
driveName = driveName.split('/')[1]
dirName = os.path.join('output',driveName)
if not os.path.exists(dirName):
os.mkdir(dirName)
# image.save_to_disk(dirName+'/%07d' % image.frame)
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
array = array[:, :, :3]
array = array[:, :, ::-1]
image_surface = pygame.surfarray.make_surface(array.swapaxes(0, 1))
if blend:
image_surface.set_alpha(100)
surface.blit(image_surface, (0, 0))
def DrawDrivable(indexes, w, h, display):
if len(indexes) != 0:
BB_COLOR = (11, 102, 35)
for i in range(10):
for j in range(10):
if indexes[i*10+j] == 1:
pygame.draw.line(display, BB_COLOR, (j*w,i*h) , (j*w+w,i*h))
pygame.draw.line(display, BB_COLOR, (j*w,i*h), (j*w,i*h+h))
pygame.draw.line(display, BB_COLOR, (j*w+w,i*h), (j*w+w,i*h+h))
pygame.draw.line(display, BB_COLOR, (j*w,i*h+h), (j*w+w,i*h+h))
# New Classes
class Evaluation():
def __init__(self):
self.sumMAE = 0
self.sumRMSE = 0
self.n_of_frames = 0
self.n_of_collisions = 0
self.history = []
def AddError(self, distance, goalDistance):
self.n_of_frames += 1
self.sumMAE += abs(goalDistance-distance)
self.sumRMSE += abs(goalDistance-distance)*abs(goalDistance-distance)
def WriteIntoFileFinal(self, filename, driveName):
if self.n_of_frames > 0:
self.sumMAE = self.sumMAE / float(self.n_of_frames)
self.sumRMSE = self.sumRMSE / float(self.n_of_frames)
with open(filename,'a') as f:
f.write(str(driveName)+', '+str(self.sumMAE)+', '+str(self.sumRMSE)+', '+str(self.n_of_collisions)+'\n')
def LoadHistoryFromFile(self, fileName):
self.history = pickle.load( open(fileName, "rb"))
def CollisionHandler(self,event):
self.n_of_collisions += 1
def main(optimalDistance, followDrivenPath, chaseMode, evaluateChasingCar, driveName='',record=False, followMode=False,
resultsName='results',P=None,I=None,D=None,nOfFramesToSkip=0):
# New imports
from car_chasing.DrivingControl import DrivingControl
from car_chasing.DrivingControlAdvanced import DrivingControlAdvanced
from car_chasing.CarDetector import CarDetector
from car_chasing.SemanticSegmentation import SemanticSegmentation
# New Variables
extrapolate = True
optimalDistance = 1
followDrivenPath = True
evaluateChasingCar = True
record = False
chaseMode = True
followMode = False
counter = 1
sensors = []
vehicleToFollowSpawned = False
obsticle_vehicleSpawned = False
# New objects
carDetector = CarDetector()
drivingControl = DrivingControl(optimalDistance=optimalDistance)
drivingControlAdvanced = DrivingControlAdvanced(optimalDistance=optimalDistance)
evaluation = Evaluation()
semantic = SemanticSegmentation()
actor_list = []
pygame.init()
display = pygame.display.set_mode(
(800, 600),
pygame.HWSURFACE | pygame.DOUBLEBUF)
font = get_font()
clock = pygame.time.Clock()
client = carla.Client('localhost', 2000)
client.set_timeout(80.0)
world = client.get_world()
weather_presets = find_weather_presets()
world.set_weather(weather_presets[3][0])
# Set BirdView
birdview_producer = BirdViewProducer(
client,
PixelDimensions(width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT),
pixels_per_meter=4,
crop_type=BirdViewCropType.FRONT_AND_REAR_AREA,
render_lanes_on_junctions=False,
)
try:
m = world.get_map()
blueprint_library = world.get_blueprint_library()
veh_bp = random.choice(blueprint_library.filter('vehicle.dodge_charger.police'))
vehicle = world.spawn_actor(
veh_bp,
m.get_spawn_points()[90])
actor_list.append(vehicle)
# New vehicle property
vehicle.set_simulate_physics(True)
if followDrivenPath:
evaluation.LoadHistoryFromFile(driveName)
first = evaluation.history[0]
start_pose = carla.Transform(carla.Location(first[0], first[1], first[2]),
carla.Rotation(first[3], first[4], first[5]))
vehicle.set_transform(start_pose)
# New Sensors
# front cam for object detection
camera_rgb_blueprint = world.get_blueprint_library().find('sensor.camera.rgb')
camera_rgb_blueprint.set_attribute('fov', '90')
camera_rgb = world.spawn_actor(
camera_rgb_blueprint,
carla.Transform(carla.Location(x=1.5, z=1.4,y=0.3), carla.Rotation(pitch=0)),
attach_to=vehicle)
actor_list.append(camera_rgb)
sensors.append(camera_rgb)
# segmentation camera
camera_segmentation = world.spawn_actor(
blueprint_library.find('sensor.camera.semantic_segmentation'),
carla.Transform(carla.Location(x=1.5, z=1.4,y=0), carla.Rotation(pitch=0)), #5,3,0 # -0.3
attach_to=vehicle)
actor_list.append(camera_segmentation)
sensors.append(camera_segmentation)
# Set up local planner and behavnioural planner
# --------------------------------------------------------------
frame = 0
max_error = 0
FPS = 30
speed = 0
cross_track_error = 0
start_time = 0.0
times = 8
LP_FREQUENCY_DIVISOR = 8 # Frequency divisor to make the
# local planner operate at a lower
# frequency than the controller
# (which operates at the simulation
# frequency). Must be a natural
# number.
# Create a synchronous mode context.
with CarlaSyncMode(world, *sensors, fps=FPS) as sync_mode:
while True:
if should_quit():
return
clock.tick()
start_time += clock.get_time()
# Display BirdView
# Input for your model - call it every simulation step
# returned result is np.ndarray with ones and zeros of shape (8, height, width)
birdview = birdview_producer.produce(agent_vehicle=vehicle)
bgr = cv2.cvtColor(BirdViewProducer.as_rgb(birdview), cv2.COLOR_BGR2RGB)
# NOTE imshow requires BGR color model
cv2.imshow("BirdView RGB", bgr)
cv2.waitKey(1)
# snapshot, image_rgb, image_segmentation = tick_response
snapshot, img_rgb, image_segmentation = sync_mode.tick(timeout=2.0)
# detect car in image with semantic segnmentation camera
carInTheImage = semantic.IsThereACarInThePicture(image_segmentation)
line = []
# Spawn a vehicle to follow
if not vehicleToFollowSpawned and followDrivenPath:
vehicleToFollowSpawned = True
location1 = vehicle.get_transform()
newX, newY = carDetector.CreatePointInFrontOFCar(location1.location.x, location1.location.y,
location1.rotation.yaw)
diffX = newX - location1.location.x
diffY = newY - location1.location.y
newX = location1.location.x - (diffX*5)
newY = location1.location.y - (diffY*5)
start_pose.location.x = newX
start_pose.location.y = newY
vehicle.set_transform(start_pose)
start_pose2 = random.choice(m.get_spawn_points())
bp = blueprint_library.filter('model3')[0]
bp.set_attribute('color', '0,101,189')
vehicleToFollow = world.spawn_actor(
bp,
start_pose2)
start_pose2 = carla.Transform()
start_pose2.rotation = start_pose.rotation
start_pose2.location.x = start_pose.location.x
start_pose2.location.y = start_pose.location.y
start_pose2.location.z = start_pose.location.z
vehicleToFollow.set_transform(start_pose2)
actor_list.append(vehicleToFollow)
vehicleToFollow.set_simulate_physics(True)
# vehicleToFollow.set_autopilot(False)
if followDrivenPath:
if counter >= len(evaluation.history):
break
tmp = evaluation.history[counter]
currentPos = carla.Transform(carla.Location(tmp[0] ,tmp[1],tmp[2]),carla.Rotation(tmp[3],tmp[4],tmp[5]))
vehicleToFollow.set_transform(currentPos)
counter += 1
# ------------------- Control for ego --------------------------------
# Set up new locationss
location1 = vehicle.get_transform()
location2 = vehicleToFollow.get_transform()
possibleAngle = 0
drivableIndexes = []
bbox = []
bbox, predicted_distance,predicted_angle = carDetector.getDistance(vehicleToFollow, camera_rgb,carInTheImage,extrapolation=extrapolate,nOfFramesToSkip=nOfFramesToSkip)
if frame % LP_FREQUENCY_DIVISOR == 0:
# This is the bottle neck and takes times to run. But it is necessary for chasing around turns
predicted_angle, drivableIndexes = semantic.FindPossibleAngle(image_segmentation,bbox,predicted_angle) # This is still necessary need to optimize it
steer, throttle = drivingControlAdvanced.PredictSteerAndThrottle(predicted_distance,predicted_angle,None)
# This is a new method
send_control(vehicle, throttle, steer, 0)
speed = np.linalg.norm(carla_vec_to_np_array(vehicle.get_velocity()))
real_dist = location1.location.distance(location2.location)
fps = round(1.0 / snapshot.timestamp.delta_seconds)
# Draw the display.
# draw_image(display, image_rgb)
draw_image(display, img_rgb, image_segmentation,location1, location2,record=record,driveName=driveName,smazat=line)
display.blit(
font.render(' FPS (real) % 5d ' % clock.get_fps(), True, (255, 255, 255)),
(8, 10))
display.blit(
font.render(' FPS (simulated): % 5d ' % fps, True, (255, 255, 255)),
(8, 28))
display.blit(
font.render(' speed: {:.2f} m/s'.format(speed), True, (255, 255, 255)),
(8, 46))
# display.blit(
# font.render(' cross track error: {:03d} cm'.format(cross_track_error), True, (255, 255, 255)),
# (8, 64))
# display.blit(
# font.render(' max cross track error: {:03d} cm'.format(max_error), True, (255, 255, 255)),
# (8, 82))
# Draw bbox on following vehicle
if len(bbox) != 0:
points = [(int(bbox[i, 0]), int(bbox[i, 1])) for i in range(8)]
BB_COLOR = (248, 64, 24)
# draw lines
# base
pygame.draw.line(display, BB_COLOR, points[0], points[1])
pygame.draw.line(display, BB_COLOR, points[1], points[2])
pygame.draw.line(display, BB_COLOR, points[2], points[3])
pygame.draw.line(display, BB_COLOR, points[3], points[0])
# top
pygame.draw.line(display, BB_COLOR, points[4], points[5])
pygame.draw.line(display, BB_COLOR, points[5], points[6])
pygame.draw.line(display, BB_COLOR, points[6], points[7])
pygame.draw.line(display, BB_COLOR, points[7], points[4])
# base-top
pygame.draw.line(display, BB_COLOR, points[0], points[4])
pygame.draw.line(display, BB_COLOR, points[1], points[5])
pygame.draw.line(display, BB_COLOR, points[2], points[6])
pygame.draw.line(display, BB_COLOR, points[3], points[7])
DrawDrivable(drivableIndexes, image_segmentation.width // 10, image_segmentation.height // 10, display)
pygame.display.flip()
frame += 1
cv2.destroyAllWindows()
finally:
print('destroying actors.')
for actor in actor_list:
actor.destroy()
pygame.quit()
print('done.')
if __name__ == '__main__':
try:
optimalDistance = 8
followDrivenPath = True
evaluateChasingCar = True
record = False
chaseMode = True
followMode = False
# Using preset way points
drivesDir = './car_chasing/drives'
drivesFileNames = os.listdir(drivesDir)
drivesFileNames.sort()
drivesFileNames = ['ride5.p'] # ['ride8.p'] ['ride10.p'] for testing advance angle turns # turnel ['ride15.p']
for fileName in drivesFileNames:
main(optimalDistance=optimalDistance,followDrivenPath=followDrivenPath,chaseMode=chaseMode, evaluateChasingCar=evaluateChasingCar,driveName=os.path.join(drivesDir,fileName),record=record,followMode=followMode)
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')