-
Notifications
You must be signed in to change notification settings - Fork 2
/
vid_split_img.py
51 lines (43 loc) · 1.48 KB
/
vid_split_img.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
import cv2
import numpy as np
import os
from tqdm import tqdm
def main():
# Playing video from file
# vid = cv2.VideoCapture(str(file_path))
vid = cv2.VideoCapture('./videos/walkman_silhh.mp4')
try:
if not os.path.exists('data'):
print("Creating '/data' directory..")
os.makedirs('data')
else:
# already exists, clear
print("'/data' directory found, clearing..")
for file in os.listdir('data'):
os.remove(os.path.join('data', file))
print("'/data' cleared")
except OSError:
print('Error: Creating directory of data')
interval = int(vid.get(cv2.CAP_PROP_FPS)) / 2
totalFrames = int(vid.get(cv2.CAP_PROP_FRAME_COUNT))
print("Total Frames: {}".format(totalFrames))
while(vid.isOpened()):
currentFrame = 0
success, frame = vid.read()
if success:
while currentFrame < totalFrames/interval:
# Capture frame-by-frame
success, frame1 = vid.read()
# Saves image of the current frame in jpg file
name = './data/frame' + str(currentFrame) + '.png'
print('Creating...' + name)
cv2.imwrite(name, frame1)
# To stop duplicate images
currentFrame += 1
vid.release()
break
cv2.destroyAllWindows()
if __name__ == '__main__':
# test1.py executed as script
# do something
main()