<jupyter_start><jupyter_text>---  Show First Frames of Random Rounds & Games<jupyter_code>from IPython.core.display import HTML, Markdown
from PIL import ImageDraw


def transform_box(x1, y1, x2, y2, target_width, target_height, curr_dim=640):
    """
    Transform bbox coordinates from (curr_dim, curr_dim) pixel space to size=(width, height) pixel
    space. assumes width is greater than height. This is used because the detector bbox coordinates
    are in a square pixel space (config.dataset_config.img_size)**2, and we need to convert the bbox
    coordinates back to the original image pixel space (e.g., 1280*720).

    Args:
        xmin, ymin, xmax, ymax

    Returns:
        Tuple[[xmin, ymin, xmax, ymax], area]
    """
    # Back to width*width:
    new_x1 = x1 * (target_width / curr_dim)
    new_y1 = y1 * (target_width / curr_dim)
    new_x2 = x2 * (target_width / curr_dim)
    new_y2 = y2 * (target_width / curr_dim)
    # Remove vertical padding
    y_pad = (target_width - target_height) / 2
    new_y1 -= y_pad
    new_y2 -= y_pad
    new_area = (new_x2 - new_x1 + 1) * (new_y2 - new_y1 + 1)
    return (new_x1, new_y1, new_x2, new_y2), new_area


def get_dets(video_id: str, model: str, df_meta: pd.DataFrame):
    split = df_meta.loc[video_id].split
    dets_path = Path(
        f"/shared/gbiamby/geo/segment/detections/{model}/{split}/df_frame_dets-video_id_{video_id}.pkl"
    )
    df_dets = pickle.load(open(dets_path, "rb"))
    if "frame_id" in df_dets.columns:
        df_dets.drop(columns=["frame_id"], inplace=True)
    df_dets.set_index("frame_idx", inplace=True)

    # df_dets.bbox.apply(lambda x: transform_box(*x.values(),
    return df_dets


def show_random_frames_masked(
    video_id: str, model: str, df: pd.DataFrame, df_meta: pd.DataFrame, n_samples: int = 5
):
    df_random = df.sample(n=n_samples)
    df_dets = get_dets(video_id, model, df_meta)
    for idx, img_row in df_random.iterrows():
        print("-" * 180)
        print(
            f"video_id: {img_row.video_id}, frame_idx: {img_row.frame_idx}, seconds: {img_row.sec}",
        )
        img = pil_img.open(img_row["file_path"])
        # img.thumbnail((1080, 640), pil_img.NEAREST)
        img_width, img_height = img.size
        display(img)
        dets = df_dets.loc[img_row.frame_idx]
        # display(dets)
        dets_lookup = {
            l: (l, transform_box(*bb.values(), img_width, img_height), s)
            for l, bb, s in zip(dets.labels, dets.bboxes, dets.scores)
        }
        # print(dets_lookup)
        masked_area = sum([d[1][1] for d in dets_lookup.values()])
        print(
            f"masked_area: {masked_area:,}",
            f"img_area: {float(img_width*img_height):,}",
            f"pct_masked: {100.0 * masked_area / (img_width*img_height):.2f}%",
        )

        img_masked = img
        draw = ImageDraw.Draw(img_masked)
        for label, bbox, score in dets_lookup.values():
            draw.rectangle(bbox[0], fill=0)

        # Mask out minimum rectangular region that encloses the geoguessr logo and/or the status bar:
        top_ui = [dets_lookup[l] for l in ["game_title", "status_bar"] if l in dets_lookup]
        if top_ui:
            y_max = max(d[1][0][3] for d in top_ui)
            # xmin, ymax = reverse_point(640, y_max, img_width, img_height, 640)
            draw.rectangle((0, 0, img_width, y_max), fill=0)
        display(img_masked)
        print("")


if "df_meta" not in locals():
    df_meta = pd.DataFrame(get_all_geoguessr_split_metadata().values()).set_index("id")
if "frame_paths" not in locals():
    frame_paths = pickle.load(open(EXTRACTED_FRAMES_PATH / "frames_list.pkl", "rb"))


video_id = "--0Kbpo9DtE"
video_id = "zOoUR17xnL0"
model = LATEST_DETECTION_MODEL_NAME
# frames = subsample_frames(video_id, df_frames_meta, frame_paths)
# df_all_frames = pd.DataFrame(frames)
# in_game_frames = filter_to_in_game(video_id, frames, df_meta)
# df_ingame = pd.DataFrame(in_game_frames).sort_values(["round_num", "frame_idx"])

show_random_frames_masked(video_id, model, df_ingame.loc[video_id], df_meta, 10)<jupyter_output><empty_output><jupyter_text>--- Use Initial Frame(s) To Identify Rounds<jupyter_code>def get_first_name_frames(
    video_id: str, df_ingame: pd.DataFrame, n_samples: int = 5, round_num: int = None
):
    """
    Get first n_samples frames from given round of the video
    """
    video_rows = df_ingame.loc[video_id].sort_index()
    results = []
    rounds = set(video_rows.round_num.unique().tolist())
    if round_num:
        rounds = rounds.intersection(set([round_num]))
    for round_num in rounds:
        start, end = -1, -1
        total_sampled = 0
        round_rows = video_rows[video_rows.round_num == round_num]
        for i, (idx, row) in enumerate(round_rows.iterrows()):
            # if i<=6:
            #     continue
            if start < 0:
                if True or "in_game_mini_map" in row.labels:
                    start = row.frame_idx
                else:
                    continue
            total_sampled += 1
            end = row.frame_idx
            if total_sampled > n_samples:
                break
            print(row)
            img = display(pil_img.open(row.file_path))

        print(f"start: {start}, end: {end}")
        print(len(round_rows.loc[start:end]))
        results.append(round_rows.loc[start:end])
    return results


video_id = "zOoUR17xnL0"
video_id = "--0Kbpo9DtE"
results = get_first_name_frames(video_id, df_ingame, n_samples=5, round_num=8)
video_rows = df_ingame.loc[video_id].sort_index()
video_rows.shape
video_rows.round_num.unique().tolist()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1200141193081539, 'fraction_numerical': 0.01570773032121426, 'mean_word_length': 2.9768421052631577, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '543174', 'license': 'MIT', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2046, 'n_words': 435, 'path': 'notebooks/game_locations/initial_frames.ipynb', 'repo_name': 'GiscardBiamby/geo', 'size': '5666'}