File size: 2,262 Bytes
d79be43
 
 
97f97d7
d79be43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97f97d7
 
 
d79be43
97f97d7
 
 
d79be43
97f97d7
 
 
d79be43
 
 
97f97d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d79be43
 
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
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)