-
Notifications
You must be signed in to change notification settings - Fork 0
/
step4_1_modify_texture.py
executable file
·60 lines (50 loc) · 2.78 KB
/
step4_1_modify_texture.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
import os
from scipy.io import loadmat
from utils.construct_triangles import remove_small_area, filter_tri, find_boundary_ind, construct_triangle
from utils.loader import load_boundary_ind
import numpy as np
def save_obj(data_path, save_path, stitch=True):
print(f'[INFO] [Step4] Save data from {data_path}')
names = [i for i in os.listdir(data_path) if i.endswith('.mat')]
border_index = load_boundary_ind()
for i, name in enumerate(names):
print(i, name.split('.')[0])
data = loadmat(os.path.join(data_path, name))
hair_xyz = data['hairear_shape']
hair_texture = data['hairear_texture']
hair_tri = data['hairear_tri']
face_xyz = data['face_shape']
face_texture = data['face_texture']
face_tri = data['face_tri']
mask = data['facemask_withouthair']
# define triangles between face and other parts of head
if stitch:
hb_ind, fb_ind = find_boundary_ind(
hair_xyz, face_xyz, data['hairear_index'], border_index, mask
)
border_xyz, border_texture, border_tri = construct_triangle(
hair_xyz, hair_texture, face_xyz, face_texture, hb_ind, fb_ind
)
with open(os.path.join(save_path, name.split('.')[0] + '.obj'), 'w') as f:
# face_texture = np.clip(face_texture,0,255)/255
for i in range(face_xyz.shape[0]):
f.write('v %f %f %f %f %f %f\n' %(*face_xyz[i, :], *face_texture[i, ::-1]/256))
for i in range(hair_xyz.shape[0]):
f.write('v %f %f %f %f %f %f\n' %(*hair_xyz[i, :], *hair_texture[i, ::-1]/256))
if stitch:
for i in range(border_xyz.shape[0]):
f.write('v %f %f %f %f %f %f\n' %(*border_xyz[i, :], *border_texture[i, ::-1]/256))
for i in range(face_tri.shape[0]):
f.write('f {} {} {}\n'.format(int(face_tri[i,0]), int(face_tri[i,1]), int(face_tri[i,2])))
for i in range(hair_tri.shape[0]):
f.write('f {} {} {}\n'.format(int(hair_tri[i,0]+face_xyz.shape[0]), int(hair_tri[i,1]+face_xyz.shape[0]), int(hair_tri[i,2]+face_xyz.shape[0])))
if stitch:
for i in range(border_tri.shape[0]):
f.write('f {} {} {}\n'.format(int(border_tri[i,0]+face_xyz.shape[0] + hair_xyz.shape[0]), int(border_tri[i,1]+face_xyz.shape[0] + hair_xyz.shape[0]), int(border_tri[i,2]+face_xyz.shape[0] + hair_xyz.shape[0])))
if __name__ == '__main__':
data_path = 'output/step3'
save_path = 'output/step4'
if not os.path.isdir(save_path):
os.makedirs(save_path)
# save the recovered geometry as obj files
save_obj(data_path, save_path, True)