Пример #1
0
def insert_timestamp_header(sheet, col_index: int):
    try:
        sheet.update_cell(1, col_index, get_current_datetime())
    except gspread_exceptions.IncorrectCellLabel:
        print(
            f"There was an issue inserting a timestamp at position: (1, {col_index}) for sheet: {sheet.title}"
        )
Пример #2
0
def registration():
    if request.method == 'POST':
        user = {
            'id': util.key_generator(),
            'registration_time': util.get_current_datetime(),
            'username': request.form.get('username'),
            'email': request.form.get('email'),
            'password': util.hash_password(request.form.get('password')),
            'role': 'user'
        }
        data_handler.add_new_user(user)
        return redirect(url_for('route_list'))

    return render_template('registration.html',
                           page_title='Registration',
                           button_title='Registrate')
Пример #3
0
def new_question_comment(question_id=None):
    if request.method == 'POST':
        question_comment = {
            'id': util.key_generator(),
            'question_id': question_id,
            'message': request.form.get('message'),
            'submission_time': util.get_current_datetime(),
            'edited_count': '0'
        }
        data_handler.add_new_question_comment(question_comment)
        return redirect(url_for('view_question', question_id=question_id))

    return render_template('add_edit_question_answer_comments.html',
                           page_title='Add comment to question',
                           button_title='Submit comment',
                           question_id=question_id)
Пример #4
0
def check():
    if session.get('id') is None or session.get('limit') is None:
        logout()
        return False
    if not session['id'].endswith('@teec.in.net') and not users.exists(
            session['id']):
        logout()
        return False

    dt = util.get_datetime_with_timezone(session['limit'], _STR_FORMAT,
                                         datetime.timezone.utc)
    current = util.get_current_datetime()

    if dt <= current:
        logout()
        return False

    return login(session['id'])
Пример #5
0
def main():
    argparser = argparse.ArgumentParser(description=__doc__)
    argparser.add_argument(
        '-v', '--verbose',
        action='store_true',
        dest='debug',
        help='print debug information')
    argparser.add_argument(
        '--host',
        metavar='H',
        default='localhost',
        help='IP of the host server (default: localhost)')
    argparser.add_argument(
        '-p', '--port',
        metavar='P',
        default=2000,
        type=int,
        help='TCP port to listen to (default: 2000)')
    argparser.add_argument(
        '-a', '--autopilot',
        action='store_true',
        help='enable autopilot')
    argparser.add_argument(
        '-d', '--depth',
        action='store_true',
        help='enable depth camera')
    argparser.add_argument(
        '-l', '--lidar',
        action='store_true',
        help='enable Lidar')
    argparser.add_argument(
        '-q', '--quality-level',
        choices=['Low', 'Epic'],
        type=lambda s: s.title(),
        default='Epic',
        help='graphics quality level, a lower level makes the simulation run considerably faster.')
    argparser.add_argument(
        '-i', '--images-to-disk',
        action='store_true',
        dest='save_images_to_disk',
        help='save images (and Lidar data if active) to disk')
    argparser.add_argument(
        '-c', '--carla-settings',
        metavar='PATH',
        dest='settings_filepath',
        default=None,
        help='Path to a "CarlaSettings.ini" file')
    argparser.add_argument(
        '-r', '--split_ratio',
        default=0.8,
        type=float,
        help='train val split ratio')
    argparser.add_argument(
        '-e', '--episodes',
        default=3,
        type=int,
        help='# of epsiodes to run')
    argparser.add_argument(
        '-f', '--frames',
        default=300,
        type=int,
        help='# of frames per episode')
    argparser.add_argument(
        '--camera_rgb_width',
        default=800,
        type=int,
        help='width of rgb camera')
    argparser.add_argument(
        '--camera_rgb_height',
        default=600,
        type=int,
        help='height of rgb camera')
    args = argparser.parse_args()

    log_level = logging.DEBUG if args.debug else logging.INFO
    logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)

    logging.info('listening to server %s:%s', args.host, args.port)

    current_datetime = get_current_datetime()
    args.out_filename_format = 'data/' + current_datetime + '/episode_{:0>4d}/{:s}/{:0>6d}'
    args.out_labelname_format = 'data/' + current_datetime + '/episode_{:0>4d}/label.pickle'

    while True:
        try:
            run_carla_client(args)
            print('Finished simulation.')

            split_data(f'data/{current_datetime}', args.episodes, args.split_ratio)
            print('Done.')
            return

        except TCPConnectionError as error:
            logging.error(error)
            time.sleep(1)
Пример #6
0
def login(uid):
    session['id'] = uid
    session['limit'] = (util.get_current_datetime() +
                        _LIMIT).strftime(_STR_FORMAT)
    return True
def main(args):
    actor_list = []

    # In this tutorial script, we are going to add a vehicle to the simulation
    # and let it drive in autopilot. We will also create a camera attached to
    # that vehicle, and save all the images generated by the camera to disk.

    try:
        # First of all, we need to create the client that will send the requests
        # to the simulator. Here we'll assume the simulator is accepting
        # requests in the localhost at port 2000.
        client = carla.Client('localhost', 2000)
        client.set_timeout(2.0)

        # Once we have a client we can retrieve the world that is currently
        # running.
        world = client.get_world()

        world = client.load_world('Town04')
        world.set_weather(carla.WeatherParameters.ClearNoon)

        original_settings = world.get_settings()
        settings = world.get_settings()

        settings.fixed_delta_seconds = 0.1  # https://carla.readthedocs.io/en/latest/adv_synchrony_timestep/#time-step-limitations
        settings.synchronous_mode = True
        world.apply_settings(settings)

        # The world contains the list blueprints that we can use for adding new
        # actors into the simulation.
        blueprint_library = world.get_blueprint_library()

        # Now let's filter all the blueprints of type 'vehicle' and choose one
        # at random. Exclude all bikes
        vehicles = blueprint_library.filter('vehicle.*')
        cars = [
            v for v in vehicles
            if int(v.get_attribute('number_of_wheels')) != 2
        ]
        bp = random.choice(cars)

        # A blueprint contains the list of attributes that define a vehicle's
        # instance, we can read them and modify some of them. For instance,
        # let's randomize its color.
        if bp.has_attribute('color'):
            color = random.choice(bp.get_attribute('color').recommended_values)
            bp.set_attribute('color', color)

        # Now we need to give an initial transform to the vehicle. We choose a
        # random transform from the list of recommended spawn points of the map.
        transform = random.choice(world.get_map().get_spawn_points())

        # So let's tell the world to spawn the vehicle.
        vehicle = world.spawn_actor(bp, transform)

        # It is important to note that the actors we create won't be destroyed
        # unless we call their "destroy" function. If we fail to call "destroy"
        # they will stay in the simulation even after we quit the Python script.
        # For that reason, we are storing all the actors we create so we can
        # destroy them afterwards.
        actor_list.append(vehicle)
        print('created my %s' % vehicle.type_id)

        # RGB Blueprint
        camera_bp = blueprint_library.find('sensor.camera.rgb')
        camera_bp.set_attribute(
            'sensor_tick', '0.1')  # take frame every 1/10 of sec i.e. 10fps
        camera_bp.set_attribute('enable_postprocess_effects', 'True')

        # Get current datetime for versioning
        current_datetime = get_current_datetime()

        # CenterRGB
        camera_transform = carla.Transform(carla.Location(x=1.6, z=1.7))
        center_rgb = world.spawn_actor(camera_bp,
                                       camera_transform,
                                       attach_to=vehicle)
        actor_list.append(center_rgb)
        print('created %s' % center_rgb.type_id)
        center_rgb.listen(lambda image: image.save_to_disk(
            f'data/{current_datetime}/CenterRGB/{image.frame:06d}.png', carla.
            ColorConverter.Raw))

        # LeftRGB
        camera_transform = carla.Transform(
            carla.Location(x=1.6, y=-1.25, z=1.7))
        left_rgb = world.spawn_actor(camera_bp,
                                     camera_transform,
                                     attach_to=vehicle)
        actor_list.append(left_rgb)
        print('created %s' % left_rgb.type_id)
        left_rgb.listen(lambda image: image.save_to_disk(
            f'data/{current_datetime}/LeftRGB/{image.frame:06d}.png', carla.
            ColorConverter.Raw))

        # RightRGB
        camera_transform = carla.Transform(carla.Location(x=1.6, y=1.25,
                                                          z=1.7))
        right_rgb = world.spawn_actor(camera_bp,
                                      camera_transform,
                                      attach_to=vehicle)
        actor_list.append(right_rgb)
        print('created %s' % right_rgb.type_id)
        right_rgb.listen(lambda image: image.save_to_disk(
            f'data/{current_datetime}/RightRGB/{image.frame:06d}.png', carla.
            ColorConverter.Raw))

        # But the city now is probably quite empty, let's add a few more
        # vehicles.
        # transform.location += carla.Location(x=40, y=-3.2)
        # transform.rotation.yaw = -180.0
        num_of_npcs = 20
        npc_list = []
        for _ in range(num_of_npcs):
            # transform.location.x += 8.0
            npc_bp = random.choice(blueprint_library.filter('vehicle'))
            npc_transform = random.choice(world.get_map().get_spawn_points())
            # This time we are using try_spawn_actor. If the spot is already
            # occupied by another object, the function will return None.
            npc = world.try_spawn_actor(npc_bp, npc_transform)
            if npc is not None:
                actor_list.append(npc)
                npc_list.append(npc)
                npc.set_autopilot(True)
                print('created %s' % npc.type_id)

        label = {}
        for _ in range(args.frames):
            # TODO: 'remind' these guys to move!
            vehicle.set_autopilot(True)
            for a in npc_list:
                a.set_autopilot(True)

            world.tick()
            w_frame = world.get_snapshot().frame

            control_dict = generate_control_dict(vehicle.get_control(),
                                                 offset='center')
            label[f'CenterRGB/{w_frame:06d}'] = control_dict

            control_dict = generate_control_dict(vehicle.get_control(),
                                                 offset='left')
            label[f'LeftRGB/{w_frame:06d}'] = control_dict

            control_dict = generate_control_dict(vehicle.get_control(),
                                                 offset='right')
            label[f'RightRGB/{w_frame:06d}'] = control_dict
            # print("\nWorld's frame: %d" % w_frame)
            # print(vehicle.get_control())

        print('all frames collected')
        with open(f'data/{current_datetime}/label.pickle', 'wb') as f:
            pickle.dump(label, f, pickle.HIGHEST_PROTOCOL)
        print('label saved')

    finally:
        world.apply_settings(original_settings)
        print('destroying actors')
        for actor in actor_list:
            actor.destroy()
        print('done.')
Пример #8
0
def main():
    argparser = argparse.ArgumentParser()
    argparser.add_argument('-r',
                           '--split_ratio',
                           default=0.8,
                           type=float,
                           help='train val split ratio')
    argparser.add_argument('-t',
                           '--time_to_run',
                           default=5,
                           type=int,
                           help='time for data collection vehicle to run')
    argparser.add_argument('-m',
                           '--map',
                           default='Town04',
                           type=str,
                           help='town to drive in')
    argparser.add_argument('-e',
                           '--episodes',
                           default=3,
                           type=int,
                           help='# of epsiodes to run')
    argparser.add_argument('-f',
                           '--frames',
                           default=100,
                           type=int,
                           help='# of frames')
    args = argparser.parse_args()

    actor_list = []

    client = carla.Client('localhost', 2000)
    client.set_timeout(2.0)

    world = client.get_world()
    world = client.load_world(args.map)
    world.set_weather(carla.WeatherParameters.ClearNoon)

    # Get current datetime for versioning
    current_datetime = get_current_datetime()

    m = world.get_map()
    spawn_point = m.get_spawn_points()
    # good_spawn_indices = [224, 186, 320, 321, 220, 221, 273, 307, 298, 338, 308, 343, 342, 230] # curves
    # good_spawn_indices = [348, 223, 227, 370, 366, 328] # dotted lanes
    # good_spawn_indices = list(range(100)) # just random
    good_spawn_indices = [
        224, 186, 320, 321, 220, 221, 273, 307, 298, 338, 308, 343, 342, 230
    ] + [348, 223, 227, 370, 366, 328, 132, 133, 305, 304, 371, 318, 315, 125]
    spawn_index = 0
    random.shuffle(good_spawn_indices)

    blueprint_library = world.get_blueprint_library()

    bp = blueprint_library.find('vehicle.tesla.model3')
    # vehicle_transform = random.choice(m.get_spawn_points())
    vehicle_transform = spawn_point[good_spawn_indices[spawn_index]]

    # Spawn test vehicle at start pose
    vehicle = world.spawn_actor(bp, vehicle_transform)

    actor_list.append(vehicle)
    print(f'created my {vehicle.type_id}')

    vehicle.set_autopilot(True)

    # RGB Blueprint
    camera_bp = blueprint_library.find('sensor.camera.rgb')
    camera_bp.set_attribute('enable_postprocess_effects', 'True')

    # CenterRGB
    camera_transform = carla.Transform(carla.Location(x=1.6, z=1.7))
    center_rgb = world.spawn_actor(camera_bp,
                                   camera_transform,
                                   attach_to=vehicle)
    actor_list.append(center_rgb)
    print(f'created center {center_rgb.type_id}')

    # LeftRGB
    camera_transform = carla.Transform(carla.Location(x=1.6, y=-1.25, z=1.7))
    left_rgb = world.spawn_actor(camera_bp,
                                 camera_transform,
                                 attach_to=vehicle)
    actor_list.append(left_rgb)
    print(f'created left {left_rgb.type_id}')

    # RightRGB
    camera_transform = carla.Transform(carla.Location(x=1.6, y=1.25, z=1.7))
    right_rgb = world.spawn_actor(camera_bp,
                                  camera_transform,
                                  attach_to=vehicle)
    actor_list.append(right_rgb)
    print(f'created right {right_rgb.type_id}')

    # Init sensor list
    sensor_name = ['CenterRGB', 'LeftRGB', 'RightRGB']

    # Set data parent folder
    parent_dir = f'data'

    # Create a synchronous mode context.
    with CarlaSyncMode(world, center_rgb, left_rgb, right_rgb,
                       fps=10) as sync_mode:
        for e in range(args.episodes):
            print(f'Episode {e}')

            episode_label = {}
            episode_dir = f'episode_{e:0>4d}'

            for f in trange(args.frames):
                # Advance the simulation and wait for the data.
                sensor_data = sync_mode.tick(timeout=5.0)

                # No more stops at red light
                if vehicle.is_at_traffic_light():
                    traffic_light = vehicle.get_traffic_light()
                    if traffic_light.get_state(
                    ) == carla.TrafficLightState.Red:
                        traffic_light.set_state(carla.TrafficLightState.Green)

                center_control_dict = generate_control_dict(
                    vehicle.get_control())
                left_control_dict = generate_control_dict(
                    vehicle.get_control(), steer=0.25)
                right_control_dict = generate_control_dict(
                    vehicle.get_control(), steer=-0.25)
                control_data = [
                    center_control_dict, left_control_dict, right_control_dict
                ]

                for name, control_dict, img_data in zip(
                        sensor_name, control_data, sensor_data[1:]):
                    label_key = f'{current_datetime}/{episode_dir}/{name}/{f:06d}'

                    filename = f'{parent_dir}/{label_key}.png'
                    img_data.save_to_disk(filename, carla.ColorConverter.Raw)
                    episode_label[label_key] = control_dict

                    # Data Augmentation
                    img = Image.open(filename)
                    translated_img, translated_steering_angle = translate_img(
                        img, control_dict['steer'], 100, 0)
                    control_dict['steer'] = translated_steering_angle

                    augmented_filename = f'{parent_dir}/{label_key}_augmented.png'
                    translated_img.save(augmented_filename)
                    episode_label[f'{label_key}_augmented'] = control_dict

            # Save episode label dict.
            with open(f'data/{current_datetime}/episode_{e:0>4d}/label.pickle',
                      'wb') as f:
                pickle.dump(episode_label, f, pickle.HIGHEST_PROTOCOL)

            # Move vehicle to another random spawn point for the next episode
            vehicle.set_simulate_physics(False)
            spawn_index += 1
            vehicle.set_transform(spawn_point[good_spawn_indices[spawn_index]])
            vehicle.set_simulate_physics(True)
    print('data collection finished')

    print('destroying actors.')
    for actor in actor_list:
        actor.destroy()
    print('done.')