-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry.py
More file actions
65 lines (51 loc) · 1.82 KB
/
try.py
File metadata and controls
65 lines (51 loc) · 1.82 KB
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
import carla
import time
import cv2
import numpy as np
# Connect to CARLA
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
# Spawn the car
car_bp = blueprint_library.find("vehicle.mercedes.coupe_2020")
spawn_point = world.get_map().get_spawn_points()[0]
car = world.spawn_actor(car_bp, spawn_point)
# Enable autopilot
car.set_autopilot(True)
# Camera setup
cam_bp = blueprint_library.find("sensor.camera.rgb")
cam_bp.set_attribute("image_size_x", "1920")
cam_bp.set_attribute("image_size_y", "1080")
cam_bp.set_attribute("fov", "110")
cam_bp.set_attribute("sensor_tick", "0.5")
cam_transform = carla.Transform(carla.Location(x=-0.2, z=1.8))
camera = world.spawn_actor(cam_bp, cam_transform, attach_to=car)
# List to hold image frames
frames = []
# Callback to process images
def process_image(image):
array = np.frombuffer(image.raw_data, dtype=np.uint8)
array = array.reshape((image.height, image.width, 4))[:, :, :3] # Drop alpha
array = cv2.cvtColor(array, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
frames.append(array)
# Attach listener
camera.listen(lambda image: process_image(image))
# Let it record for 10 seconds (adjust as needed)
print("Recording started...")
time.sleep(200)
print("Recording finished.")
# Stop camera
camera.stop()
# Save as video
output_filename = 'output_video.mp4'
fps = 2 # sensor_tick = 0.5s => 2 FPS
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(output_filename, fourcc, fps, (1920, 1080))
for frame in frames:
video_writer.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) # Convert back for OpenCV
video_writer.release()
print(f"Video saved as {output_filename}")
# Cleanup
camera.destroy()
car.destroy()