-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathextract.py
77 lines (61 loc) · 2.15 KB
/
extract.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
import os
import cv2
import numpy as np
from datetime import datetime
import classify
import os
import glob
files = glob.glob('./images/*')
for f in files:
os.remove(f)
print("Extracting frames from the video.....")
print("Please Wait...\n\n")
def video2frames(path, output=None, skip=1, mirror=False):
video_object = cv2.VideoCapture(path)
# setup the output folder
if output is None:
output = path[:-4]
else:
if not output.endswith('/') and not output.endswith('\\'):
output += '/'
output += 'py_image'
index = 0
last_mirrored = True
while True:
success, frame = video_object.read()
if success:
if index % skip == 0:
if mirror and last_mirrored:
frame = _mirror_image(frame)
last_mirrored = not last_mirrored
cv2.imwrite(output + "_" + str(datetime.now()) + ".jpg", frame) # assumes that the extension is three letters long
else:
break
index += 1
def _mirror_image(image):
return np.fliplr(image)
def main():
import argparse
parser = argparse.ArgumentParser("Enter the filename of a video")
parser.add_argument('filename')
parser.add_argument('-o', '--output')
parser.add_argument('--skip', help="Only save every nth frame")
parser.add_argument('--mirror', action='store_true', help="Flip every other image")
args = parser.parse_args()
if args.skip is None:
args.skip = 1
if args.mirror is None:
args.mirror = False
# In case the filename points to a directory
if os.path.isdir(args.filename):
files = [os.path.join(args.filename, f) for f in os.listdir(args.filename) if os.path.isfile(os.path.join(args.filename, f))]
for video in files:
video2frames(video, output=args.output, skip=int(args.skip), mirror=bool(args.mirror))
else:
video2frames(args.filename, output=args.output, skip=int(args.skip), mirror=bool(args.mirror))
if __name__ == "__main__":
main()
print("100% Extracted")
print("Begin Analysis.....")
print("Please Wait....\n\n")
classify.run_classifier()