Skip to content

Commit 482516a

Browse files
committed
ver 0.15.8
Added video load / save
1 parent 5175f94 commit 482516a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

npy/files/io.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import yaml
44
import os
55
import functools
6+
import cv2
7+
import numpy as np
68

79
from .path import home_rsc_path, makedirpath
810

@@ -12,6 +14,7 @@ def ensure_dir_exist(f):
1214
def wrapper(content, fpath, *args, **kwargs):
1315
makedirpath(fpath)
1416
return f(content, fpath, *args, **kwargs)
17+
1518
return wrapper
1619

1720

@@ -35,6 +38,37 @@ def load_txt(fpath):
3538
return f.read()
3639

3740

41+
def load_video(fpath):
42+
vidcap = cv2.VideoCapture(fpath)
43+
44+
ret = []
45+
success = True
46+
while success:
47+
success, image = vidcap.read()
48+
if success:
49+
ret.append(image)
50+
51+
ret = np.stack(ret)
52+
if ret.shape[-1] == 3:
53+
ret = ret[..., [2, 1, 0]]
54+
return ret
55+
56+
57+
@ensure_dir_exist
58+
def save_video(arr, fpath, fourcc='DIVX', fps=24, **kwargs):
59+
fourcc = cv2.VideoWriter_fourcc(*fourcc)
60+
N, H, W = arr.shape[:3]
61+
62+
if arr.shape[-1] == 3: # BGR to RGB
63+
arr = arr[..., [2, 1, 0]]
64+
65+
vid = cv2.VideoWriter(fpath, fourcc, fps, (W, H), **kwargs)
66+
67+
for frame in arr:
68+
vid.write(frame)
69+
vid.release()
70+
71+
3872
@ensure_dir_exist
3973
def save_binary(d, fpath):
4074
with open(fpath, 'wb') as f:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(name='nuclear-python',
1212

13-
version='0.15.7.13',
13+
version='0.15.8',
1414

1515
url='https://github.com/nuclearboy95/nuclear-python',
1616

0 commit comments

Comments
 (0)