def world_loop(opts_dict): params = { 'spawn': 15, 'weather': 'clear_noon', 'n_vehicles': 0 } with cu.CarlaWrapper(TOWN, cu.VEHICLE_NAME, PORT) as env: env.init(**params) agent = RoamingAgent(env._player, False, opts_dict) # Hack: fill up controller experience. for _ in range(30): env.tick() env.apply_control(agent.run_step()[0]) for _ in tqdm.tqdm(range(125)): env.tick() observations = env.get_observations() inputs = cu.get_inputs(observations) debug = dict() control, command = agent.run_step(inputs, debug_info=debug) env.apply_control(control) observations.update({'control': control, 'command': command}) processed = cu.process(observations) yield debug bzu.show_image('rgb', processed['rgb']) bzu.show_image('birdview', cu.visualize_birdview(processed['birdview']))
def get_episode(env, params): data = list() progress = tqdm.tqdm(range(params.frames_per_episode), desc='Frame') start, target = env.pose_tasks[np.random.randint(len(env.pose_tasks))] env_params = { 'weather': np.random.choice(list(cu.TRAIN_WEATHERS.keys())), 'start': start, 'target': target, 'n_pedestrians': params.n_pedestrians, 'n_vehicles': params.n_vehicles, } env.init(**env_params) env.success_dist = 5.0 agent = NoisyAgent(env) agent.set_route(env._start_pose.location, env._target_pose.location) # Real loop. while len(data) < params.frames_per_episode and not env.is_success( ) and not env.collided: for _ in range(params.frame_skip): env.tick() observations = env.get_observations() control, command, last_status, real_control = agent.run_step( observations) agent_debug = agent.debug env.apply_control(control) observations['command'] = command observations['control'] = control observations['real_control'] = real_control if not params.nodisplay: _debug(observations, agent_debug) observations['control'] = real_control processed = cu.process(observations) data.append(processed) progress.update(1) progress.close() if (not env.is_success() and not env.collided) or len(data) < 500: return None return data
def _debug(observations, agent_debug): import cv2 processed = cu.process(observations) control = observations['control'] control = [control.steer, control.throttle, control.brake] control = ' '.join(str('%.2f' % x).rjust(5, ' ') for x in control) real_control = observations['real_control'] real_control = [ real_control.steer, real_control.throttle, real_control.brake ] real_control = ' '.join( str('%.2f' % x).rjust(5, ' ') for x in real_control) canvas = np.uint8(observations['rgb']).copy() rows = [x * (canvas.shape[0] // 10) for x in range(10 + 1)] cols = [x * (canvas.shape[1] // 10) for x in range(10 + 1)] WHITE = (255, 255, 255) CROP_SIZE = 192 X = 176 Y = 192 // 2 R = 2 def _write(text, i, j): cv2.putText(canvas, text, (cols[j], rows[i]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, WHITE, 1) _command = { 1: 'LEFT', 2: 'RIGHT', 3: 'STRAIGHT', 4: 'FOLLOW', }.get(int(observations['command']), '???') _write('Command: ' + _command, 1, 0) _write('Velocity: %.1f' % np.linalg.norm(observations['velocity']), 2, 0) _write('Real: %s' % control, -5, 0) _write('Control: %s' % control, -4, 0) r = 2 birdview = cu.visualize_birdview(crop_birdview(processed['birdview'])) def _dot(x, y, color): x = int(x) y = int(y) birdview[176 - r - x:176 + r + 1 - x, 96 - r + y:96 + r + 1 + y] = color _dot(0, 0, [255, 255, 255]) ox, oy = observations['orientation'] R = np.array([[ox, oy], [-oy, ox]]) u = np.array(agent_debug['waypoint']) - np.array(agent_debug['vehicle']) u = R.dot(u[:2]) u = u * 4 _dot(u[0], u[1], [255, 255, 255]) def _stick_together(a, b): h = min(a.shape[0], b.shape[0]) r1 = h / a.shape[0] r2 = h / b.shape[0] a = cv2.resize(a, (int(r1 * a.shape[1]), int(r1 * a.shape[0]))) b = cv2.resize(b, (int(r2 * b.shape[1]), int(r2 * b.shape[0]))) return np.concatenate([a, b], 1) full = _stick_together(canvas, birdview) bu.show_image('full', full)
def get_episode(env, params): data = list() progress = tqdm.tqdm(range(params.frames_per_episode), desc='Frame') start, target = env.pose_tasks[np.random.randint(len(env.pose_tasks))] env_params = { 'weather': np.random.choice(list(cu.TRAIN_WEATHERS.keys())), 'start': start, 'target': target, 'n_pedestrians': params.n_pedestrians, 'n_vehicles': params.n_vehicles, } env.init(**env_params) env.success_dist = 5.0 agent = NoisyAgent(env) agent.set_route(env._start_pose.location, env._target_pose.location) world = env._client.get_world() traffic_light_list = world.get_actors().filter('*traffic_light*') # Real loop. while len(data) < params.frames_per_episode and not env.is_success( ) and not env.collided: for _ in range(params.frame_skip): env.tick() observations = env.get_observations() control, command, last_status, real_control = agent.run_step( observations) agent_debug = agent.debug env.apply_control(control) observations['command'] = command observations['control'] = control observations['real_control'] = real_control # Traffic lights is_intersection_marin, traffic_light, light_state, distance_to_traffic_light = \ agent.detect_traffic_light(traffic_light_list) if traffic_light: print("INTERSECTION MARIN = ", is_intersection_marin) print( "incoming traffic light is at " + str(distance_to_traffic_light) + "meters and is of color ", agent.LightState(light_state).name) # visualise traffic lights with a green rectangle # green = carla.Color(0, 255, 0) # size_plot_point = 0.1 # life_time_plot_point = 1.0 # world.debug.draw_point(traffic_light.get_location(), color=green, size=size_plot_point * 10, # life_time=life_time_plot_point) observations['is_traffic_light'] = True observations['traffic_light_color'] = light_state - 1 observations[ 'traffic_light_distance'] = distance_to_traffic_light else: observations['is_traffic_light'] = False observations['traffic_light_color'] = 0 observations['traffic_light_distance'] = 0 print(f'is traffic light: {observations["is_traffic_light"]}') print(f'traffic color: {observations["traffic_light_color"]}') print( f'traffic distance: {observations["traffic_light_distance"]}') if not params.nodisplay: _debug(observations, agent_debug) observations['control'] = real_control processed = cu.process(observations) data.append(processed) progress.update(1) progress.close() if (not env.is_success() and not env.collided) or len(data) < 500: return None return data