-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideo_to_frames.py
62 lines (48 loc) · 1.86 KB
/
video_to_frames.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
'''
Within this script, focus to get the frames of the input video file.
Requirements
----
You require OpenCV 3.2 to be installed.
Run
----
If need to run this script seperately, then can edit the releavant input file path and output file path.
If need to use this script within another code then can import the scirpt and call the functions with relevant arguments.
'''
import cv2
import os
def get_frames(video_input_path, frame_output_path):
if (os.path.isfile(video_input_path)):
# Playing the input video from file
video_capture = cv2.VideoCapture(video_input_path)
try:
if not os.path.exists(frame_output_path):
os.makedirs(frame_output_path)
except OSError:
print('Error: Creating directory of data')
# Capture the very first frame
return_status, frame = video_capture.read()
current_frame = 0
counter = 0
while(return_status):
if(counter==4):
# Saving the current frame's image as a jpg file
frame_location = frame_output_path+"frame" + str(current_frame) + ".jpg"
print ("Creating..." + frame_location)
cv2.imwrite(frame_location, frame)
# Increasing the current frame value for the next frame
current_frame += 1
counter = 0
# Capture frame-by-frame
return_status, frame = video_capture.read()
counter +=1
# Release the capture
video_capture.release()
cv2.destroyAllWindows()
else:
print("Invalid input video to capture. Location or the video not exist.")
def run():
video_input_path = "./data/input_video/input_video.mp4"
frame_output_path = "./data/generated_frames/"
get_frames(video_input_path, frame_output_path)
if __name__ == "__main__":
run()