-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA01.py
More file actions
109 lines (84 loc) · 3.22 KB
/
Copy pathA01.py
File metadata and controls
109 lines (84 loc) · 3.22 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import os
from pathlib import Path
import numpy as np
import shutil
import cv2
def load_video_as_frames(video_filepath):
# Load video
print("\n\nPROCESS VIDEO: ",video_filepath)
#Use cv2.VideoCapture to load a video from video_filepath
capture = cv2.VideoCapture(video_filepath)
# Check if data is invalid
if not capture.isOpened():
# If the video is not opened, print an error and return None
print("ERROR: Could not open or find the video!")
return None
# Loop through the video ONCE and add each frame to a list
my_frames = []
frame_cnt = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
print("video frame count is: ",frame_cnt)
cv2.namedWindow(video_filepath)
for current_frame in range(frame_cnt):
ret, frame = capture.read()
#print("on frame: ",current_frame+1)
#print("shape: ",frame.shape)
my_frames.append(frame)
#close capture and destroy all windows
capture.release()
cv2.destroyAllWindows()
#Return list of frames as numpy array
return np.array(my_frames)
def compute_wait(fps):
#Compute the wait in milliseconds as int(1000.0/fps)
return int(1000.0/fps)
def display_frames(all_frames, title, fps=30):
#Call compute_wait(fps) to get the wait time.
wait_time = compute_wait(fps)
#show all the frames
for frame in all_frames:
cv2.imshow(title, frame)
cv2.waitKey(wait_time)
#destroy all windows
cv2.destroyAllWindows()
def save_frames(all_frames, output_dir, basename, fps=30):
#Make the video folder name as basename + “_” + str(fps)
video_folder = basename + "_" + str(fps)
output_path = os.path.join(output_dir, video_folder)
# If the path already exists (os.path.exists()), remove it with shutil.rmtree()
if os.path.exists(output_path) is True:
shutil.rmtree(output_path)
#make the directory
print("Creating directory",output_path)
os.makedirs(output_path)
#For each frame...
for frame in range(len(all_frames)):
print("frame:",frame)
#get zero-padded filename
filename = "image_%07d.png" % frame
#get full path
filename = os.path.join(output_path, filename)
print("filename: ",filename)
#save frame
cv2.imwrite(filename,all_frames[frame])
def main():
if len(sys.argv) < 3:
print("ERROR: Not enough args.")
exit(1)
else:
video_filepath = sys.argv[1]
output_directory = sys.argv[2]
core_file_name = Path(video_filepath).stem
#Load all frames; if the function returns None, print an error and exit(1)
all_frames = load_video_as_frames(video_filepath)
if all_frames is None:
print("ERROR: Could not get frames.")
exit(1)
#Display all frames with title “Input Video” and fps of 30
display_frames(all_frames, "Input Video", fps=30)
# Save the (output) frames to the output folder
save_frames(all_frames, output_directory, core_file_name, fps=30)
# Close down...
print("Closing application...")
if __name__ == "__main__":
main()