-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathsingle_convert.py
82 lines (71 loc) · 1.93 KB
/
single_convert.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import argparse
import os
import cv2
# Necessary Arguments
ap = argparse.ArgumentParser()
ap.add_argument(
"-i", "--input", required=True, type=str, help="Input for the video files"
)
ap.add_argument(
"-o",
"--output",
required=True,
type=str,
help="Output directory for the images extracted",
)
ap.add_argument(
"-fps",
"--framesps",
default=int(cv2.CAP_PROP_FPS),
help="Extract at the desired Frame rate",
)
ap.add_argument(
"-frate",
"--frate",
default=0.1,
type=float,
help="To determine the amount of frames to be extracted per second",
)
args = vars(ap.parse_args())
video_path = args["input"]
save_path = args["output"]
skip_frames = args["framesps"]
frameRate = args["frate"]
def length_of_video(video_p):
"""
Gets the input length of video and returns it.
"""
cap = cv2.VideoCapture(video_p)
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
return length
def extracting_frames(video_pa, op_path, skip_frame):
"""
Extracting function for the images
"""
length = length_of_video(video_pa)
if length == 0:
print("Length is 0,Video not Available")
return 0
cap = cv2.VideoCapture(video_pa)
count = 0
ret, frame = cap.read()
test_file_path = op_path + "image" + str(count) + ".png"
cv2.imwrite(test_file_path, frame)
if os.path.isfile(test_file_path):
print("Saving Test Frame successful")
count = 1
while ret:
ret, frame = cap.read()
if ret and count % int(skip_frame / (frameRate)) == 0:
# naming
name = op_path + "image" + str(count) + ".png"
cv2.imwrite(name, frame)
print(name)
count += 1
else:
count += 1
else:
print("Problem saving the Test Frame.")
return 0
cap.release()
extracting_frames(video_path, save_path, skip_frames)