10
10
import deep_3drecon
11
11
from moviepy .editor import VideoFileClip
12
12
import copy
13
+ import psutil
13
14
14
15
sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
15
16
@@ -39,31 +40,39 @@ def process_video(fname, out_name=None, skip_tmp=True):
39
40
# return
40
41
os .system (f"touch { tmp_name } " )
41
42
cap = cv2 .VideoCapture (fname )
42
- lm68_lst = []
43
- lm5_lst = []
44
- frames = []
45
- cnt = 0
46
43
print (f"loading video ..." )
44
+ # 获取视频相关参数
45
+ num_frames = int (cap .get (7 ))
46
+ h = int (cap .get (4 ))
47
+ w = int (cap .get (3 ))
48
+ # 检测系统资源是否充足
49
+ mem = psutil .virtual_memory ()
50
+ a_mem = mem .available
51
+ min_mem = num_frames * 68 * 2 + num_frames * 5 * 2 + num_frames * h * w * 3
52
+ if a_mem < min_mem :
53
+ print (f"WARNING: The physical memory is insufficient, which may result in memory swapping. Available Memory: { a_mem / 1000000 :.3f} M, the minimum memory required is:{ min_mem / 1000000 :.3f} M." )
54
+ # 初始化矩阵
55
+ lm68_arr = np .empty ((num_frames , 68 , 2 ),dtype = np .float32 )
56
+ lm5_arr = np .empty ((num_frames , 5 , 2 ),dtype = np .float32 )
57
+ video_rgb = np .empty ((num_frames , h , w , 3 ),dtype = np .uint8 )
58
+ cnt = 0
47
59
while cap .isOpened ():
48
60
ret , frame_bgr = cap .read ()
49
61
if frame_bgr is None :
50
62
break
51
63
frame_rgb = cv2 .cvtColor (frame_bgr , cv2 .COLOR_BGR2RGB )
52
- frames . append ( frame_rgb )
64
+ video_rgb [ cnt ] = frame_rgb
53
65
cnt += 1
54
- for i in trange (cnt , desc = "extracting 2D facial landmarks ..." ):
66
+ for i in trange (num_frames , desc = "extracting 2D facial landmarks ..." ):
55
67
try :
56
- lm68 = fa .get_landmarks (frames [i ])[0 ] # 识别图片中的人脸,获得角点, shape=[68,2]
68
+ lm68 = fa .get_landmarks (video_rgb [i ])[0 ] # 识别图片中的人脸,获得角点, shape=[68,2]
57
69
except :
58
- print (f"WARNING: Caught errors when fa.get_landmarks, maybe No face detected at frame { cnt } in { fname } !" )
70
+ print (f"WARNING: Caught errors when fa.get_landmarks, maybe No face detected at frame { i } in { fname } !" )
59
71
raise ValueError ("" )
60
72
lm5 = lm68_2_lm5 (lm68 )
61
- lm68_lst .append (lm68 )
62
- lm5_lst .append (lm5 )
63
- video_rgb = np .stack (frames ) # [t, 224,224, 3]
64
- lm68_arr = np .stack (lm68_lst ).reshape ([cnt , 68 , 2 ])
65
- lm5_arr = np .stack (lm5_lst ).reshape ([cnt , 5 , 2 ])
66
- num_frames = cnt
73
+ lm68_arr [i ]= lm68
74
+ lm5_arr [i ]= lm5
75
+ # num_frames = cnt
67
76
batch_size = 32
68
77
iter_times = num_frames // batch_size
69
78
last_bs = num_frames % batch_size
@@ -82,8 +91,8 @@ def process_video(fname, out_name=None, skip_tmp=True):
82
91
coeff_arr = np .concatenate (coeff_lst ,axis = 0 )
83
92
result_dict = {
84
93
'coeff' : coeff_arr .reshape ([cnt , - 1 ]),
85
- 'lm68' : lm68_arr . reshape ([ cnt , 68 , 2 ]) ,
86
- 'lm5' : lm5_arr . reshape ([ cnt , 5 , 2 ]) ,
94
+ 'lm68' : lm68_arr ,
95
+ 'lm5' : lm5_arr ,
87
96
}
88
97
np .save (out_name , result_dict )
89
98
os .system (f"rm { tmp_name } " )
0 commit comments