Skip to content

Commit 02c22ff

Browse files
committed
Reduce memory footprint,Change prompt statement
1 parent f855272 commit 02c22ff

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

data_gen/nerf/extract_3dmm.py

+25-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import deep_3drecon
1111
from moviepy.editor import VideoFileClip
1212
import copy
13+
import psutil
1314

1415
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
1516

@@ -39,31 +40,39 @@ def process_video(fname, out_name=None, skip_tmp=True):
3940
# return
4041
os.system(f"touch {tmp_name}")
4142
cap = cv2.VideoCapture(fname)
42-
lm68_lst = []
43-
lm5_lst = []
44-
frames = []
45-
cnt = 0
4643
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
4759
while cap.isOpened():
4860
ret, frame_bgr = cap.read()
4961
if frame_bgr is None:
5062
break
5163
frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
52-
frames.append(frame_rgb)
64+
video_rgb[cnt]=frame_rgb
5365
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 ..."):
5567
try:
56-
lm68 = fa.get_landmarks(frames[i])[0] # 识别图片中的人脸,获得角点, shape=[68,2]
68+
lm68 = fa.get_landmarks(video_rgb[i])[0] # 识别图片中的人脸,获得角点, shape=[68,2]
5769
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}!")
5971
raise ValueError("")
6072
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
6776
batch_size = 32
6877
iter_times = num_frames // batch_size
6978
last_bs = num_frames % batch_size
@@ -82,8 +91,8 @@ def process_video(fname, out_name=None, skip_tmp=True):
8291
coeff_arr = np.concatenate(coeff_lst,axis=0)
8392
result_dict = {
8493
'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,
8796
}
8897
np.save(out_name, result_dict)
8998
os.system(f"rm {tmp_name}")

docs/prepare_env/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ moviepy
2525
dearpygui
2626
ninja
2727
pyaudio # for extract esperanto
28-
mediapipe==0.8.11
28+
mediapipe==0.8.11
29+
psutil

0 commit comments

Comments
 (0)