-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpkpts_extract_keypoints.py
61 lines (44 loc) · 2.17 KB
/
mpkpts_extract_keypoints.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
"""
This file is used to extract the keypoints from the videos in the dataset by leveraging the KeypointsExtractor class.
"""
from pathlib import Path
import argparse
import numpy as np
from src.keypointsextractor import KeypointsExtractor
from src import constants
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Subsample videos.')
parser.add_argument('--input', type=str, required=True, help='Path to input directory.')
parser.add_argument('--output', type=str, required=True, help='Path to output directory.')
args = parser.parse_args()
# Define paths
path_input = Path(args.input)
path_output = Path(args.output)
# Create output folder if it doesn't exist
path_output.mkdir(parents=True, exist_ok=True)
# Loop through each label folder
for target in ["0", "1"]:
# Create the target directory if it doesn't exist
(path_output / target).mkdir(parents=True, exist_ok=True)
# Loop through each video in the input folder
for input_file in sorted((path_input / target).glob("*")):
# Skip if the input is not a file or if it is not a video
if not (input_file.is_file() and input_file.suffix.lower() in constants.VIDEO_EXTENSIONS):
continue
# Define the output file
output_file = (path_output / input_file.relative_to(path_input)).with_suffix(".npy")
# If the keypoints file already exists, skip it
if output_file.is_file():
print(f"Skipping {input_file}")
continue
# Print the current file
print(f"Processing {input_file}")
# Extract keypoints from the video
feature_extractor = KeypointsExtractor(str(input_file), show_image=True)
keypoints = feature_extractor.extract_keypoints_from_video()
# Save keypoints as npy file if keypoints is not None
if keypoints is not None:
np.save(output_file, keypoints)
else:
print("No hand detected for the whole video, or multiple hands detected.")
print("Done extracting keypoints.")