-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_from_images.py
256 lines (185 loc) · 7.81 KB
/
data_from_images.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from __future__ import annotations
import atexit
import bisect
import math
import os
import pathlib
import pickle
import shutil
import sys
from dataclasses import dataclass
from typing import Optional
import hydra
import omegaconf
import PIL.Image
import simple_term_menu
import tqdm
import utils
@dataclass()
class FrameExtractor:
input_dir: pathlib.Path
output_dir: pathlib.Path
quality: int = 90
resolution: int = 256
min_frames: int = 30
max_frames: int = 3000
num_jobs: int = -1
def run(self):
clip_paths = list(self.relative_frame_paths.keys())
with utils.ParallelProgressBar(n_jobs=self.num_jobs) as parallel:
parallel.tqdm(
desc="Extracting frames from frame folders", unit=" clips"
)
frame_paths = parallel(self._clip_worker, clip_paths)
frame_paths = [paths for paths in frame_paths if paths is not None]
frame_paths.sort()
frame_paths = dict(frame_paths)
print(
f"Extracted valid frames from {len(frame_paths)} of "
f"{len(self.relative_frame_paths)} video clips."
)
with open(self.frame_paths_file, "wb") as open_file:
pickle.dump(frame_paths, open_file)
print("Saved sorted dictionary of frame paths.")
def __post_init__(self):
self.frame_paths_file = os.path.join(self.output_dir, "frame_paths.pkl")
self._make_output_dir()
self.relative_frame_paths = self._get_relative_clip_paths()
self.temp_dir = self._make_temp_dir()
def _clip_worker(
self, index: int, relative_clip_path: str
) -> Optional[tuple[str, list[str]]]:
clip_name = f"clip_{index:07d}"
if len(self.relative_frame_paths) > 1000:
subset_name = f"{(index // 1000):04d}"
subset_dir = os.path.join(self.output_dir, subset_name)
relative_frame_dir = os.path.join(subset_name, clip_name)
else:
subset_dir = self.output_dir
relative_frame_dir = clip_name
frame_dir = os.path.join(self.output_dir, relative_frame_dir)
if os.path.exists(frame_dir):
frame_names = os.listdir(frame_dir)
frame_names.sort()
else:
temp_frame_dir = os.path.join(self.temp_dir, relative_frame_dir)
os.makedirs(temp_frame_dir)
try:
frame_names = self._extract_frames_single_clip(
relative_clip_path, temp_frame_dir
)
except Exception as e:
print(e)
return
else:
os.makedirs(subset_dir, exist_ok=True)
os.rename(temp_frame_dir, frame_dir)
return relative_frame_dir, frame_names
def _extract_frames_single_clip(
self, relative_clip_path: str, frame_dir: str
) -> list[str]:
frame_names = []
size = None
clip_path = self.input_dir.joinpath(relative_clip_path)
names = self.relative_frame_paths[relative_clip_path]
for index, name in enumerate(names):
frame_path = str(clip_path.joinpath(name))
frame = PIL.Image.open(frame_path)
if index == 0:
short_edge = min(frame.size)
if short_edge < self.resolution:
raise AssertionError(
f"Frame folder resolution too low: {relative_clip_path}"
)
if short_edge > self.resolution:
scale = self.resolution / short_edge
width = int(math.ceil(scale * frame.width))
height = int(math.ceil(scale * frame.height))
size = (width, height)
if size is not None:
frame = frame.resize(size, resample=PIL.Image.LANCZOS)
frame_name = f"frame_{index:05d}.jpg"
frame_names.append(frame_name)
frame_path = os.path.join(frame_dir, frame_name)
frame.save(frame_path, quality=self.quality)
if index == self.max_frames - 1:
break
if len(frame_names) < self.min_frames:
raise AssertionError(
f"Frame folder has too few frames: {relative_clip_path}"
)
return frame_names
def _make_output_dir(self):
if os.path.exists(self.output_dir):
print(f"Output directory already exists: {self.output_dir}")
line = "Please select how to procede:"
print(line)
options = ("Resume", "Overwrite", "Exit")
index = simple_term_menu.TerminalMenu(options).show()
assert isinstance(index, int)
# Appends the selected option to the end of the previous line.
print(f"\033[F\033[{len(line)}C", options[index])
if options[index].lower() == "exit":
sys.exit(1)
if options[index].lower() == "overwrite":
print("Removing existing output directory...")
shutil.rmtree(self.output_dir)
elif os.path.exists(self.frame_paths_file):
raise AssertionError(
"Cannot resume since frame paths file already exists: "
f"{self.frame_paths_file}"
)
os.makedirs(self.output_dir, exist_ok=True)
def _get_relative_clip_paths(self) -> dict[str, str]:
if not self.input_dir.is_dir():
raise ValueError(f"Directory not found: {self.input_dir}")
list_path = self.input_dir.joinpath("frame_paths.pkl")
if list_path.exists():
with open(list_path, "rb") as open_file:
frame_paths = pickle.load(open_file)
else:
frame_paths = {}
directories = [str(self.input_dir)]
progress_bar = tqdm.tqdm(desc="Listing all frame paths")
while directories:
directory = directories.pop()
for entry in os.scandir(directory):
if entry.is_dir(follow_symlinks=False):
directories.append(entry.path)
elif entry.name.lower().endswith((".jpg", ".jpeg", ".png")):
path = pathlib.Path(entry).parent
relative_path = path.relative_to(self.input_dir)
if relative_path in frame_paths:
bisect.insort(
frame_paths[relative_path], entry.name
)
else:
frame_paths[relative_path] = [entry.name]
progress_bar.update(1)
progress_bar.close()
for folder_path, names in frame_paths.items():
frame_paths[folder_path] = sorted(names)
with open(list_path, "wb") as open_file:
pickle.dump(frame_paths, open_file)
print("Saved list of frame paths.")
return frame_paths
def _make_temp_dir(self) -> str:
temp_dir = os.path.join(self.output_dir, "tmp")
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
os.mkdir(temp_dir)
def _remove_temp_dir():
print("Removing temporary directry...")
shutil.rmtree(temp_dir)
atexit.register(_remove_temp_dir)
return temp_dir
@hydra.main(config_path="configs/data", config_name="from_images")
def extract_frames(config: omegaconf.DictConfig):
if config.input_dir is None or config.output_dir is None:
raise AssertionError("Must specify input and output directories.")
input_dir = pathlib.Path(config.input_dir)
output_dir = pathlib.Path(config.output_dir)
frame_extractor = FrameExtractor(input_dir, output_dir, **config.extractor)
frame_extractor.run()
if __name__ == "__main__":
extract_frames()