This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
84 lines (65 loc) · 2.61 KB
/
extract.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
import argparse
import cv2
from pathlib import Path
import numpy as np
import json
ROOT = Path(__file__).parent
def extract(
n_frames: int = 1000,
overwrite: bool = False,
) -> None:
"""Extract a certain number from frames from the train video without duplication.
Args:
n_frames (int, optional): the number of frames to extract. Defaults to 1000.
overwrite (bool, optional): overwrite existing files. Defaults to False.
"""
# Load settings.json
with open(ROOT / "settings.json") as f:
settings = json.load(f)
# Create variables
train_video = ROOT / settings["videos"] / settings["vidoes_names"][0]
frames = ROOT / settings["frames"]
# Remove all files in the directory if overwrite is true
# Note: from distutils.dir_util import remove_tree cause directory creation problem due to race condition.
if overwrite and frames.exists():
for f in frames.glob("*.png"):
f.unlink()
# Create the directory if it doesn't exist
frames.mkdir(exist_ok=True)
# Create video capture object
cap = cv2.VideoCapture(str(train_video))
# Find the number of frames in the video
nums_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Find all frames that have been extracted already
existed_frames = [int(f.stem) for f in frames.glob("*.png")]
# Find all frames that haven't been extracted yet
new_frames = [i for i in range(nums_frame) if i not in existed_frames]
# Randomly pick frames
picked_frames = np.random.choice(
new_frames, n_frames - len(existed_frames), replace=False
)
# Write frames to the directory
for frame in picked_frames:
cap.set(cv2.CAP_PROP_POS_FRAMES, frame)
_, frame_val = cap.read()
cv2.imwrite(str(frames / f"{frame}.png"), frame_val)
def parse_opt(known: bool = False) -> argparse.Namespace:
"""Set up command line arguments
Args:
known (bool, optional): if arguments are known, throw an error if an unknown argument are passed in. Defaults to False.
Returns:
argparse.Namespace: parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--n_frames", "-n", default=1000, type=int, help="number of frames to extact"
)
parser.add_argument(
"-o", "--overwrite", action="store_true", help="overwrite the directory"
)
opt = parser.parse_known_args()[0] if known else parser.parse_args()
return opt
# Run this code if this script is called from a command line
if __name__ == "__main__":
opt = parse_opt()
extract(n_frames=opt.n_frames, overwrite=opt.overwrite)