-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.py
87 lines (66 loc) · 2.41 KB
/
fps.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
83
84
85
86
87
# -*- coding: utf-8 -*-
"""fps.ipynb
Author: Amanda Horacio
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kLpbsHkaFLFAgB_snrPxUb-5euGBoQBp
"""
import cv2
import os
import shutil
def new_folder(dir):
if os.path.exists(dir):
shutil.rmtree(dir)
os.mkdir(dir)
def name(x):
return 'vf_frames/vf_set_{}'.format(str(x))
def split_fps(clip, fps=15, fpf=15, update_res=False, res_x=720, res_y=1280):
"""Break video down in frames
@param: clip The file name of the video
@param: fps The number of frames per second (default 15.0)
@param: fpf The number of frames per folder (default 15.0)
@param: updates_res, Option to update resolution of video (default False)
@param: res_x, The updated resolution width value (default 720.0)
@param: res_y, The updated resolution height value (default 1280.0)
"""
assert(fps > 0)
assert(fps < 30)
assert(fpf * 2 < 124)
vf = cv2.VideoCapture(clip)
frames = []
while(vf.isOpened()):
ret, frame = vf.read()
if ret == False:
break
if update_res:
frame = cv2.resize(frame,(res_x,res_y),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
frames.append(frame)
frames_fps = vf.get(cv2.CAP_PROP_FPS)
step = round(frames_fps / fps)
num_frames, split = 1, 1
path = name(split)
new_folder(path)
for f in range(0, len(frames), step):
if (num_frames > fpf):
num_frames, split = 1, split + 1
path = name(split)
new_folder(path)
dir = '{}/{}.jpg'.format(path, str(f))
cv2.imwrite(dir, frames[f])
num_frames += 1
!mkdir vf_frames
# upload reference video into directory (folder icon) on the left
video_ref = 'IMG_0393.MOV'
split_fps(video_ref)
# SKIP TO NEXT BLOCK IF YOU WANT TO DOWNLOAD DATA
## THIS BLOCK DELETES GENERATED VIDEO FRAMES IF YOU WANT TO RERUN ALGORITHM W DIFFERENT PARAMETERS / REFRESH
## EXAMPLE: split_fps(video_ref, fps=10, fpf=20, update_res=True)
## --> splits video into 10 frames per second,
## --> splits frames into folders of 20 frames (2 seconds of the video),
## --> updates resolution w default values
!rm -rf vf_frames
# DOWNLOAD Video Reference Frames
from google.colab import files
!zip -r vf_frames.zip vf_frames/
# Creates a zip file in folder icon (left of page). Right click or click 3 dots on 'vf_frames.zip' and select download.
files.download('vf_frames.zip')