-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_data.py
82 lines (64 loc) · 2.22 KB
/
generate_data.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
import glob
import os
import sys
try:
sys.path.append(glob.glob('../CARLA_0.9.11/PythonAPI/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 = 256
IM_HEIGHT = 128
j = [0]
def process_img(image, vehicle, j):
i = np.array(image.raw_data)
i2 = i.reshape((IM_HEIGHT, IM_WIDTH, 4))
i3 = i2[:, :, :3]
data_val = vehicle.get_control()
print(data_val.steer, data_val.throttle, data_val.brake, data_val.reverse)
with open('output/data.txt', 'a', encoding='utf-8') as f:
f.write(f"{data_val.steer} {data_val.throttle} {data_val.brake} {data_val.reverse} \n")
# f.close()
cv2.imwrite(f"output/{j[0]}.jpg", i3)
j[0]+=1
cv2.imshow("", i3)
cv2.waitKey(20)
return i3/255.0
actor_list = []
try:
with open('output/data.txt', 'a', encoding='utf-8') as f:
f.write("steer throttle brake reverse \n")
# f.close()
client = carla.Client('localhost', 2000)
client.set_timeout(200.0)
# world = client.get_world()
world = client.load_world('Town04')
# print(client.get_available_maps())
client.reload_world()
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.filter('model3')[0]
spawn_point = random.choice(world.get_map().get_spawn_points())
vehicle = world.spawn_actor(bp, spawn_point)
# vehicle.apply_control(carla.VehicleControl(throttle=1.0, steer=0.0))
vehicle.set_autopilot(True)
actor_list.append(vehicle)
blueprint = blueprint_library.find('sensor.camera.rgb')
blueprint.set_attribute('image_size_x', f"{IM_WIDTH}")
blueprint.set_attribute('image_size_y', f"{IM_HEIGHT}")
# blueprint.set_attribute('fov', '110')
spawn_point = carla.Transform(carla.Location(x=2.5, z=0.7))
sensor = world.spawn_actor(blueprint, spawn_point, attach_to=vehicle)
actor_list.append(sensor)
sensor.listen(lambda data: process_img(data, vehicle, j))
time.sleep(60)
finally:
print('destroying actors')
for actor in actor_list:
actor.destroy()
print('done.')