def homepage():
    tagger_id = 'test_tagger'
    # image_data = db.get_untagged_screenshot(tagger_id)
    image_data = db.get_screenshot_by_id(
        '670fecdf-02c2-47ad-becd-20b6ddac5fc0')
    image_id = image_data['image_id']

    game = image_data['game']
    y_offset = image_data['y_offset']
    x_offset = image_data['x_offset']
    # width = image_data['width']
    # height = image_data['height']
    data = image_data['data']
    logger.debug("Untagged Image data retrieved image_id: {}".format(image_id))

    orig_cv, encoded_img = P.from_data_to_cv(data)
    image_string = b64_string(encoded_img)
    logger.debug('Image stringified')

    # known_game_tiles = db.get_tiles_by_game(game)
    # logger.debug('Known tiles loaded')

    output = {
        'tagger_id': tagger_id,
        'image': image_string,
        'image_id': image_id,
    }
    tags = db.get_screenshot_affordances(image_id)
    if len(tags) % 9 != 0:
        logger.debug(f'WRONG NUM OF AFFORDANCES FOR IMAGE: {image_id}')
        return render_template('homepage.html', **output)

    to_convert = []
    for affordance in range(9):
        db_entry = tags[affordance + 9]
        if db_entry['affordance'] % 9 != affordance:
            logger.debug(
                f'AFFORDANCE TAG WRONG ORDER {affordance}, for image: {image_id}')
            return render_template('homepage.html', **output)
        orig_bw, encoded_tag = P.from_data_to_cv(db_entry['tags'])
        logger.debug(f'test db tags: {orig_bw.shape}, {type(orig_bw)}')
        output[P.AFFORDANCES[affordance]] = b64_string(encoded_tag)
        to_convert.append(orig_bw)

    stacked_array = P.images_to_numpy(to_convert)
    logger.debug(
        f'got tag array: {stacked_array.shape}, {type(stacked_array)}, {stacked_array.min()}, {stacked_array.max()}')
    # if len(to_convert) != 9:
    #     logger.debug(f'NOT ALL AFFORDANCES FOR IMAGE: {image_id}')
    #     return 0

    # map_dict(b64_string, to_convert)
    logger.debug('{}'.format(output.keys()))
    return render_template('homepage.html', **output)
def get_image_to_tag():
    """Return random image, list of unique tiles and locations"""
    tagger_id = request.args.get(
        'tagger-id', default='default-tagger', type=str)
    if tagger_id == 'default-tagger':
        logger.debug("NO TAGGER ID IN GET IMAGE")

    logger.debug('Fetching image for tagger: {}'.format(tagger_id))

    image_data = db.get_untagged_screenshot(tagger_id)
    image_id = image_data['image_id']
    game = image_data['game']
    data = image_data['data']

    meta = {i: image_data[i] for i in image_data if i
            != 'data' and i != 'game' and i != 'image_id'}
    y_offset = image_data['y_offset']
    x_offset = image_data['x_offset']
    logger.debug("Untagged Image data retrieved image_id: {}".format(image_id))
    logger.debug(f'image meta info: {meta}')

    orig_cv, encoded_img = P.from_data_to_cv(data)
    image_string = b64_string(encoded_img)
    logger.debug('Image stringified')

    # known_game_tiles = db.get_tiles_by_game(game)

    unique_tiles = P.unique_tiles_using_meta(
        (orig_cv), **meta)

    tiles_to_tag = get_tile_ids(unique_tiles, game)
    # map_dict(encode_tile_from_dict, tiles_to_tag)
    logger.debug("Unique TILES found: {}".format(len(unique_tiles)))
    logger.debug('Tiles id-d, LEN: {}'.format(len(tiles_to_tag)))

    # tags = P.load_label(image_file)
    # tag_images = P.numpy_to_images(tags)
    # map_dict(b64_string, tag_images)
    output = {
        'image': image_string,
        'image_id': image_id,
        'tiles': tiles_to_tag,
        'y_offset': y_offset,
        'x_offset': x_offset
    }
    logger.debug('base route ok')
    return jsonify({'output': output})
def get_tile_ids(unique_tiles, game):
    known_game_tiles = db.get_tiles_by_game(game)

    tiles_to_tag = {}
    logger.debug('LEN KNOWN TILES: {}'.format(len(known_game_tiles)))
    hit_ctr = 0
    miss_ctr = 0
    for idx, screenshot_tile in enumerate(unique_tiles):
        to_compare = screenshot_tile['tile_data']
        is_in_db = False
        for tile_info in known_game_tiles:
            cv_img, encoded_img = P.from_data_to_cv(tile_info['data'])
            err = P.mse(to_compare, (cv_img))
            if err < 0.001:
                is_in_db = True
                hit_ctr += 1
                # logger.debug("MATCHED {}".format(tile_info['tile_id']))
                # logger.debug("NUM LOCS {}".format(
                #     len(screenshot_tile['locations'])))
                tiles_to_tag['tile_{}'.format(idx)] = {
                    'tile_id': tile_info['tile_id'],
                    'tile_data': b64_string(P.from_cv_to_bytes(to_compare)),
                    'locations': screenshot_tile['locations']
                    }
                break
        if not is_in_db:
            logger.debug("TILE NOT MATCHED IN DB")
            miss_ctr += 1
            tiles_to_tag['tile_{}'.format(idx)] = {
                'tile_id': -1,
                'tile_data': b64_string(P.from_cv_to_bytes(to_compare)),
                'locations': screenshot_tile['locations']
                }
        # idx = 0
        # if idx == -1:
        #     logger.debug('NEW TILE FOUND')
        #     height, width, channels = screenshot_tile['tile_data'].shape
        #     tile_data = P.from_cv_to_bytes(screenshot_tile['tile_data'])
        #     db.insert_tile(game, width, height, tile_data)
    logger.debug(f'db tile hits: {hit_ctr}, misses: {miss_ctr}')
    return tiles_to_tag
def save_labels(image_uuid, image_folder):
    # label_file = os.path.join(image_folder, 'label', f'{image_uuid}.npy')
    db_entries = get_screenshot_affordances(image_uuid)
    row_ctr = 0
    if len(db_entries) % 9 != 0:
        print('NOT MOD 9 TAG ENTRIES FOR IMAGE: {}'.format(image_uuid))
    print('NUM ROWS OF AFFORDANCES {} FOR IMAGE {}'.format(
        len(db_entries), image_uuid))
    while row_ctr < len(db_entries):
        label_to_convert = []
        tagger_id = db_entries[row_ctr]['tagger_id']
        for affordance in range(9):
            db_entry = db_entries[row_ctr]
            if db_entry['affordance'] != affordance:
                print('AFFORDANCES IN WRONG ORDER')
            tag_cv, encoded = P.from_data_to_cv(db_entry['tags'])
            label_to_convert.append(tag_cv)
            row_ctr += 1
        stacked_array = P.images_to_numpy(label_to_convert)
        pth = os.path.join(image_folder, f'{tagger_id}.npy')
        print(f'NUMPY SAVE: saving file: {pth}')
        np.save(os.path.join(image_folder, f'{tagger_id}.npy'), stacked_array)
def export_to_filesystem(dest='/out_dataset'):
    game_names = get_game_names()
    print(f'exporting data for games: {game_names}')
    total_exported = {}
    for game in game_names:
        screenshot_ctr = 0

        game_path = os.path.join(dest, game['game'])
        os.makedirs(os.path.join(game_path, 'screenshots'), exist_ok=True)
        os.makedirs(os.path.join(game_path, 'tiles'), exist_ok=True)
        os.makedirs(os.path.join(game_path, 'sprites'), exist_ok=True)

        print(f'Made Directories for game: {game}, {game_path}')
        screenshots = get_screenshots_by_game(game['game'])
        print(f'Exporting {len(screenshots)} screenshots for {game}')
        for screenshot in screenshots:
            image_id = screenshot['image_id']
            image_folder = os.path.join(game_path, 'screenshots',
                                        str(image_id))
            os.makedirs(image_folder, exist_ok=True)

            image_file = os.path.join(image_folder, f'{image_id}.png')
            orig_cv, encoded_img = P.from_data_to_cv(screenshot['data'])
            print(
                f'saving file: {image_file}  -- {orig_cv.shape} {type(orig_cv)}'
            )
            cv2.imwrite(image_file, orig_cv)
            save_labels(image_id, image_folder)
            meta = {
                'y_offset': screenshot['y_offset'],
                'x_offset': screenshot['x_offset']
            }
            with open(os.path.join(image_folder, f'{str(image_id)}.json'),
                      'w') as file:
                json.dump(meta, file)

        tiles = get_tiles_by_game(game['game'])
        print(f'Exporting {len(tiles)} screenshots for {game}')
        tiles_folder = os.path.join(game_path, 'tiles')
        os.makedirs(tiles_folder, exist_ok=True)
        to_csv = []
        for tile in tiles:
            tile_id = tile['tile_id']

            tile_file = os.path.join(tiles_folder, f'{tile_id}.png')
            orig_cv, encoded_img = P.from_data_to_cv(tile['data'])
            print(
                f'saving file: {tile_file}  -- {orig_cv.shape} {type(orig_cv)}'
            )
            cv2.imwrite(tile_file, orig_cv)

            tile_tag_entries = get_tile_affordances(tile_id)

            for db_entry in tile_tag_entries:
                db_entry['file_name'] = db_entry.pop('tile_id')
                to_csv.append(db_entry)
        with open(os.path.join(tiles_folder, 'tile_affordances.csv'),
                  mode='w') as csv_file:
            fieldnames = [
                'file_name', "solid", "movable", "destroyable", "dangerous",
                "gettable", "portal", "usable", "changeable", "ui", "tagger_id"
            ]
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            writer.writeheader()
            for row in to_csv:
                writer.writerow(row)
    return 1