Struggle-Dataset / tools /build_frames.py
Shijia2025's picture
Update tools/build_frames.py
97f97d7 verified
import os
import glob
import sys
import argparse
from tqdm import tqdm
def extract_frames(root_path, out_path, dataset_name):
print("Extracting Frames")
epic_pipes_path = os.path.join(root_path, dataset_name)
epic_pipes_vidlist = os.listdir(epic_pipes_path)
out_path = os.path.join(out_path, dataset_name)
with tqdm(total=len(epic_pipes_vidlist)) as pbar:
for vid in epic_pipes_vidlist:
vid_name = vid.split('.')[0]
out_path_sub = os.path.join(out_path, vid_name)
if not os.path.isdir(out_path_sub):
# print("creating folder: " + out_path_sub)
os.mkdir(out_path_sub)
# extract frames first
import cv2
video = cv2.VideoCapture(os.path.join(epic_pipes_path, vid))
count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
for i in range(count):
ret, frame = video.read()
assert ret
cv2.imwrite('{}/img_{:03d}.jpg'.format(out_path_sub, i), frame)
pbar.update()
print("Finished!")
def main(root_path):
out_path = os.path.join(root_path, 'extracted_frames')
# prepare for the Pipes-Struggle dataset
print("Extracting frames from Pipes-Struggle Dataset")
extract_frames(root_path, out_path, 'Pipes-Struggle')
# prepare for the Tent-Struggle dataset
print("Extracting frames from Tent-Struggle Dataset")
extract_frames(root_path, out_path, 'Tent-Struggle')
# prepare for the Tower-Struggle dataset
print("Extracting frames from Tower-Struggle Dataset")
extract_frames(root_path, out_path, 'Tower-Struggle')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Extract frames from Struggle-Dataset videos.',
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'--root_path', '-r',
type=str,
required=True,
help='Root path to the Struggle-Dataset directory'
)
args = parser.parse_args()
if not os.path.isdir(args.root_path):
parser.error(f"The path '{args.root_path}' does not exist or is not a directory.")
main(args.root_path)