File size: 4,039 Bytes
e5e4b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
import json
import os
import shutil
import subprocess
from io import BytesIO
from pathlib import Path

import cv2
import matplotlib.pyplot as plt
import numpy as np
import yaml
from PIL import Image
from pycocotools import mask as mask_utils
from tqdm import tqdm


annotation_files = {
    "droid": [
        "silver_droid_merged_test.json",
    ],
    "sav": [
        "silver_sav_merged_test.json",
    ],
    "yt1b": [
        "silver_yt1b_merged_test.json",
    ],
    "ego4d": [
        "silver_ego4d_merged_test.json",
    ],
}


def load_yaml(filename):
    with open(filename, "r") as f:
        return yaml.safe_load(f)


def load_json(filename):
    with open(filename, "r") as f:
        return json.load(f)


def save_json(content, filename):
    with open(filename, "w") as f:
        json.dump(content, f)


def run_command(cmd):
    """Run a shell command and raise if it fails."""
    result = subprocess.run(cmd, shell=True)
    if result.returncode != 0:
        raise RuntimeError(f"Command failed: {cmd}")


config = load_yaml("CONFIG_FRAMES.yaml")


def is_valid_image(img_path):
    try:
        img = Image.open(img_path).convert("RGB")
        return True
    except Exception:
        return False


def get_frame_from_video(video_path, frame_id):
    cap = cv2.VideoCapture(video_path)
    cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
    ret, frame = cap.read()
    cap.release()
    if not ret:
        # Some videos cannot be open with OpenCV
        import av

        container = av.open(video_path)
        stream = container.streams.video[0]
        for i, frame in tqdm(
            enumerate(container.decode(stream)),
            desc="Decoding with AV",
            total=frame_id + 1,
        ):
            if i == frame_id:
                img = frame.to_ndarray(format="rgb24")
                return img
        raise ValueError(
            f"Could not read frame {frame_id} from video {video_path} (out of frame)"
        )
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    return frame_rgb


def update_annotations(dataset_name, file_names_keep, key="original_video"):
    for annotation_file in annotation_files[dataset_name]:
        path_ann = os.path.join(config["path_annotations"], annotation_file)
        path_original_ann = os.path.join(
            config["path_annotations"],
            annotation_file.replace(".json", "_original.json"),
        )
        ann = load_json(path_ann)
        shutil.copy(path_ann, path_original_ann)
        new_images = []
        image_ids_keep = set()
        for image in ann["images"]:
            if image[key].replace(".mp4", "") in file_names_keep:
                new_images.append(image)
                image_ids_keep.add(image["id"])
        new_annotations = []
        for annotation in ann["annotations"]:
            if annotation["image_id"] in image_ids_keep:
                new_annotations.append(annotation)
        ann["images"] = new_images
        ann["annotations"] = new_annotations
        save_json(ann, path_ann)


def get_filename_size_map(annotation_path):
    with open(annotation_path) as f:
        annotations = json.load(f)
    filename_size_map = {}
    for each in annotations["images"]:
        filename_size_map[each["file_name"]] = (each["width"], each["height"])
    return filename_size_map


def get_filenames(annotation_path):
    with open(annotation_path) as f:
        annotations = json.load(f)
    filenames = {Path(each["file_name"]) for each in annotations["images"]}
    return filenames


def get_image_ids(annotation_path):
    filenames = get_filenames(annotation_path)
    filestems = {Path(each).stem for each in filenames}
    return filestems


def setup(folder):
    print("Making dir", folder)
    folder.mkdir(exist_ok=True)


def copy_file(paths):
    old_path, new_path = paths
    print("Copy from", old_path, "to", new_path)
    if not Path(new_path).exists():
        shutil.copy2(old_path, new_path)