-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractImages.py
67 lines (51 loc) · 1.65 KB
/
extractImages.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
import cv2
import os
import argparse
#
# Exports every frame of the video as a jpg.
#
# USAGE:
#
# --v parameter specifies path to the video
# --o parameter specifies path (directory) to export the frames.
#
# python3 extractImages.py --v MiniVanMadness.mov --o frames_output
#
def extractImages(pathToVideo, pathToOutput):
pathToVideo = "MiniVanMadness.mov" if pathToVideo is None else pathToVideo
pathToOutput = "frames_output" if pathToOutput is None else pathToOutput
# Read the video from specified path
cam = cv2.VideoCapture(pathToVideo)
try:
# creating a folder named data
if not os.path.exists(pathToOutput):
os.makedirs(pathToOutput)
# if not created then raise error
except OSError:
print('Error: Creating directory of data')
# frame
currentframe = 0
while (True):
# reading from frame
ret, frame = cam.read()
if ret:
# if video is still left continue creating images
name = './' + pathToOutput + '/frame' + str(currentframe) + '.jpg'
print('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
a = argparse.ArgumentParser()
a.add_argument("--v", help="path to video")
a.add_argument("--o", help="path to images")
args = a.parse_args()
print(args)
extractImages(args.v, args.o)