3
3
import yaml
4
4
import os
5
5
import functools
6
+ import cv2
7
+ import numpy as np
6
8
7
9
from .path import home_rsc_path , makedirpath
8
10
@@ -12,6 +14,7 @@ def ensure_dir_exist(f):
12
14
def wrapper (content , fpath , * args , ** kwargs ):
13
15
makedirpath (fpath )
14
16
return f (content , fpath , * args , ** kwargs )
17
+
15
18
return wrapper
16
19
17
20
@@ -35,6 +38,37 @@ def load_txt(fpath):
35
38
return f .read ()
36
39
37
40
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
+
38
72
@ensure_dir_exist
39
73
def save_binary (d , fpath ):
40
74
with open (fpath , 'wb' ) as f :
0 commit comments