-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_flattener.py
executable file
·65 lines (51 loc) · 1.81 KB
/
image_flattener.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
#! /Users/reidm/anaconda3/envs/openCV/bin/python
"""
Take Video input and flatten it to individual frames/images
"""
import cv2
import os
import argparse
import pathlib
def flatten_video(vid_path, out_path, base_file):
os.makedirs(out_path, exist_ok=True)
current_frame = 0
video = cv2.VideoCapture(vid_path)
while True:
ret, frame = video.read()
if ret:
name = os.path.join(out_path, f'{base_file}-frame-{current_frame:010d}.jpg')
cv2.imwrite(name, frame)
current_frame += 1
else:
break
video.release()
def process_video_files(input_path, output_path):
for root, _, files in os.walk(input_path):
for file in files:
file_path = os.path.join(root, file)
if pathlib.Path(file_path).suffix.lower() == '.mp4':
base_file = os.path.splitext(file)[0]
out_path = os.path.join(output_path, base_file)
print(f"Converting: {file}")
flatten_video(file_path, out_path, base_file)
else:
print(f"Skipping: {file_path}")
def main():
parser = argparse.ArgumentParser(
prog='flatten_to_images.py',
description='Flatten Video to Images'
)
parser.add_argument('-i', '--input', required=True, help='Input video directory')
parser.add_argument('-o', '--output', required=True, help='Output image directory')
args = parser.parse_args()
if not os.path.exists(args.input):
print("Input directory does not exist.")
return
if not os.path.exists(args.output):
os.makedirs(args.output)
print(f"Input: {args.input}")
print(f"Output: {args.output}")
print("======================")
process_video_files(args.input, args.output)
if __name__ == "__main__":
main()