1+
2+ import glob
3+ import os
4+ import sys
5+ import random
6+ import time
7+ import numpy as np
8+ import cv2
9+ import pygame
10+ from pygame .locals import KMOD_CTRL
11+ from pygame .locals import K_ESCAPE
12+ from pygame .locals import K_q
13+ from pygame import key
14+ import numpy as np
15+
16+ # ==============================================================================
17+ # -- Find CARLA module ---------------------------------------------------------
18+ # ==============================================================================
19+ try :
20+ sys .path .append (glob .glob ('../carla/dist/carla-*%d.%d-%s.egg' % (
21+ sys .version_info .major ,
22+ sys .version_info .minor ,
23+ 'win-amd64' if os .name == 'nt' else 'linux-x86_64' ))[0 ])
24+ except IndexError :
25+ pass
26+
27+ # ==============================================================================
28+ # -- Add PythonAPI for release mode --------------------------------------------
29+ # ==============================================================================
30+ try :
31+ sys .path .append (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))) + '/carla' )
32+ except IndexError :
33+ pass
34+
35+ import carla
36+
37+ #*******************************************************************************
38+ IM_WIDTH = 640
39+ IM_HEIGHT = 480
40+ IM_FOV = 100
41+ IM_SHUTTER = 30
42+ TIMEOUT = 20
43+ CAM_LOC_X = 3
44+ CAM_LOC_Z = 1.3
45+
46+
47+ class sensor_platform ():
48+ """Class to create a car with RGB sensors and pass the image to the visualizer"""
49+ def __init__ (self ):
50+ self .width = IM_WIDTH
51+ self .height = IM_HEIGHT
52+ self .fov = IM_FOV
53+ self .shutter = IM_SHUTTER
54+ self .sensor = None
55+ self .actor_list = []
56+ self .sensor_list = []
57+ self .client = carla .Client ("localhost" , 2000 )
58+ self .client .set_timeout (TIMEOUT )
59+ self .world = self .client .get_world ()
60+ traffic_manager = self .client .get_trafficmanager (8000 )
61+ traffic_manager .set_synchronous_mode (True )
62+
63+ # synchronous mode:
64+ settings = self .world .get_settings ()
65+ print ("Is client in synchrony mode? " ,settings .synchronous_mode )
66+ settings .synchronous_mode = True
67+ settings .fixed_delta_seconds = 0.05
68+ self .world .apply_settings (settings )
69+
70+ self .blueprint_library = self .world .get_blueprint_library ()
71+ bp = self .blueprint_library .filter ("mustang" )[0 ]
72+ spawn_point = random .choice (self .world .get_map ().get_spawn_points ())
73+ # To solve spawn collision for the sensor platform and avoid execution crash
74+ for spawn_collision in range (1 , 10 ):
75+ try :
76+ self .vehicle = self .world .spawn_actor (bp , spawn_point )
77+ self .vehicle .set_autopilot (True ) #initiate with autopilot
78+ self .actor_list .append (self .vehicle )
79+ except :
80+ print (f'Sensor platform spawn failed. collision counter: { spawn_collision } ' )
81+ spawn_point = random .choice (self .world .get_map ().get_spawn_points ())
82+ time .sleep (0.5 )
83+ pass
84+
85+ def __del__ (self ):
86+ for actor in self .actor_list :
87+ print (f'actor { actor } , destroyed' )
88+ actor .destroy ()
89+ for sensor in self .sensor_list :
90+ print (f'sensor { sensor } , destroyed' )
91+ sensor .destroy ()
92+ print ("all actors and sensors destroyed" )
93+
94+ def set_sensor (self , bp_name = "sensor.camera.rgb" ):
95+ cam_bp = self .blueprint_library .find (bp_name )
96+ cam_bp .set_attribute ("image_size_x" , f"{ self .width } " )
97+ cam_bp .set_attribute ("image_size_y" , f"{ self .height } " )
98+ cam_bp .set_attribute ("fov" , f"{ self .fov } " )
99+ cam_bp .set_attribute ("shutter_speed" , f"{ self .shutter } " )
100+ spawn_point = carla .Transform (carla .Location (x = CAM_LOC_X , z = CAM_LOC_Z ))
101+ self .sensor = self .world .spawn_actor (cam_bp , spawn_point , attach_to = self .vehicle )
102+ self .sensor_list .append (self .sensor )
103+ return self .sensor
104+
105+
106+ # to RGB avoiding alpha
107+ def carla_to_cv (self , image , visualize = False ):
108+ i = np .array (image .raw_data ) # one dimension array
109+ i2 = i .reshape ((IM_HEIGHT , IM_WIDTH , 4 ))
110+ i3 = i2 [:,:,:3 ] #only get rgb avoids alpha
111+ if visualize :
112+ cv2 .imshow ("RGB Sensor: Front Camera" ,i3 )
113+ c = cv2 .waitKey (1 ) # ASCII 'Esc' value
114+ if c == 27 :
115+ print ('Closing simulator camera, shutting down application...' )
116+ cv2 .destroyAllWindows ()
117+ exit ()
118+ return i3
119+
120+ def main ():
121+ platform = sensor_platform ()
122+ sensor = platform .set_sensor ()
123+ sensor .listen (lambda data : platform .carla_to_cv (data ))
124+ while 1 :
125+ time .sleep (20 )
126+
127+ if __name__ == "__main__" :
128+ main ()
0 commit comments