-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_image_hyunkoo.py
executable file
·90 lines (74 loc) · 3.11 KB
/
save_image_hyunkoo.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
# import glob
# import os
# import sys
import random
# from turtle import width
import carla
# from matplotlib import image
# import tensorflow as tf
import cv2
import numpy as np
# import time
# import argparse
# import imutils
def camera_callback(image, data, save_dir):
data['image'] = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))
image.save_to_disk(f"{save_dir}/{image.frame}.png")
def camera_callback2(image, data, save_dir):
capture = np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))
data['image'] = capture
cv2.imwrite(f"{save_dir}/{image.frame}.png", capture)
def main():
save_dir = '/home/hyunkoo/DATA/Study_Carla/SaveCarla/images'
print("")
# Laden von Carla,Library,Map, erstellen des Spectators
client = carla.Client('localhost', 2000)
client.set_timeout(500.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
spectator = world.get_spectator()
bp_lib = world.get_blueprint_library()
spawn_points = world.get_map().get_spawn_points()
# Laden von Carla,Library,Map, erstellen des Spectators
client = carla.Client('localhost', 2000)
client.set_timeout(500.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
spawnpoints = world.get_map().get_spawn_points()
spectator = world.get_spectator()
# Nun Einstellungen zum Fahrzeug
vehicle_blueprint = blueprint_library.find('vehicle.audi.etron')
# Fahrzeug wird an einem zufälligen Spawnpunkt gespawnt
vehicle = world.try_spawn_actor(vehicle_blueprint, random.choice(spawnpoints))
transform = carla.Transform(vehicle.get_transform().transform(carla.Location(x=2, z=0.5)),
vehicle.get_transform().rotation)
spectator.set_transform(transform)
# Kamera
camera_blueprint = blueprint_library.find('sensor.camera.rgb')
camera_transform = carla.Transform(carla.Location(z=2, x=0.5))
camera = world.spawn_actor(camera_blueprint, camera_transform, attach_to=vehicle)
image_width = camera_blueprint.get_attribute("image_size_x").as_int()
image_height = camera_blueprint.get_attribute("image_size_y").as_int()
camera_data = {'image': np.zeros((image_height, image_width, 4))}
camera.listen(lambda image: camera_callback(image, camera_data, save_dir))
# Anzeigen des Bildes
while True:
cv2.namedWindow('Camera', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Camera', camera_data['image'])
vehicle.set_autopilot(True)
# cv2.waitKey(1)
if cv2.waitKey(1) == ord('q'):
client.reload_world()
break
# while True:
# img = cv2.imshow('Camera', camera_data['image'])
# vehicle.set_autopilot(True)
# camera.listen(lambda image: image.save_to_disk(r"/home/hyun/DATA/Codes/CARLA_0.9.14/PythonAPI/carla_hyunkoo_examples/images",
# color_converter=None))
#
# if cv2.waitKey(1) == ord('p'):
# client.reload_world()
# break
cv2.destroyAllWindows()
if __name__ == '__main__':
main()