-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlive.py
49 lines (39 loc) · 1.39 KB
/
live.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
import cv2
import os
import argparse
import uuid
"""parsing and configuration"""
def parse_args():
desc = "Frame Extractor"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--frame', type=int, default=1800, help='We Will Extract Frame Every X Frame')
parser.add_argument('--src', type=str, default='', help='What Is a Video Path?')
parser.add_argument('--dir', type=str, default='', help='Save Frames Folder Name')
return parser.parse_args()
def main():
args = parse_args()
if not args.src:
exit()
# Opens the Video file
target_directory = 'data/frames'
cap= cv2.VideoCapture(args.src)
frame_folder = args.dir or str(uuid.uuid4())
count = 0
file_number = 0
if not os.path.exists(f'{target_directory}/{frame_folder}'):
os.makedirs(f'{target_directory}/{frame_folder}')
print('Frames Directory Created Successfully!')
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
cv2.imwrite(f'{target_directory}/{frame_folder}/frame{file_number}.jpg', frame)
file_number += 1
count += args.frame # i.e. at 30 fps, this advances one second
cap.set(1, count)
else:
cap.release()
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()