-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy path_3dmm_utils.py
130 lines (89 loc) · 3.23 KB
/
_3dmm_utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Notes: Many of .dat files are written using Matlab.
Hence, there are "-1" subtraction to Python 0-based indexing
"""
from __future__ import division
import math
import numpy as np
from config import _3DMM_DEFINITION_DIR
VERTEX_NUM = 53215
TRI_NUM = 105840
N = VERTEX_NUM * 3
def load_3DMM_tri():
# Triangle definition (i.e. from Basel model)
print 'Loading 3DMM tri ...'
fd = open(_3DMM_DEFINITION_DIR + '3DMM_tri.dat')
tri = np.fromfile(file=fd, dtype=np.int32)
fd.close()
#print tri
tri = tri.reshape((3,-1)).astype(np.int32)
tri = tri - 1
tri = np.append(tri, [[ VERTEX_NUM], [VERTEX_NUM], [VERTEX_NUM]], axis = 1 )
print ' DONE'
return tri
def load_3DMM_vertex_tri():
# Vertex to triangle mapping (list of all trianlge containing the cureent vertex)
print 'Loading 3DMM vertex tri ...'
fd = open(_3DMM_DEFINITION_DIR + '3DMM_vertex_tri.dat')
vertex_tri = np.fromfile(file=fd, dtype=np.int32)
fd.close()
vertex_tri = vertex_tri.reshape((8,-1)).astype(np.int32)
#vertex_tri = np.append(vertex_tri, np.zeros([8,1]), 1)
vertex_tri[vertex_tri == 0] = TRI_NUM + 1
vertex_tri = vertex_tri - 1
print ' DONE'
return vertex_tri
def load_3DMM_vt2pixel():
# Mapping in UV space
fd = open(_3DMM_DEFINITION_DIR + 'vertices_2d_u.dat')
vt2pixel_u = np.fromfile(file=fd, dtype=np.float32)
vt2pixel_u = np.append(vt2pixel_u - 1, 0)
fd.close()
fd = open(_3DMM_DEFINITION_DIR + 'vertices_2d_v.dat')
vt2pixel_v = np.fromfile(file=fd, dtype=np.float32)
vt2pixel_v = np.append(vt2pixel_v - 1, 0)
fd.close()
return vt2pixel_u, vt2pixel_v
def load_3DMM_kpts():
# 68 keypoints indices
print 'Loading 3DMM keypoints ...'
fd = open(_3DMM_DEFINITION_DIR + '3DMM_keypoints.dat')
kpts = np.fromfile(file=fd, dtype=np.int32)
kpts = kpts.reshape((-1, 1))
fd.close()
return kpts - 1
def load_3DMM_tri_2d(with_mask = False):
fd = open(_3DMM_DEFINITION_DIR + '3DMM_tri_2d.dat')
tri_2d = np.fromfile(file=fd, dtype=np.int32)
fd.close()
tri_2d = tri_2d.reshape(192, 224)
tri_mask = tri_2d != 0
tri_2d[tri_2d == 0] = TRI_NUM+1 #VERTEX_NUM + 1
tri_2d = tri_2d - 1
if with_mask:
return tri_2d, tri_mask
return tri_2d
def load_Basel_basic(element, is_reduce = False):
fn = _3DMM_DEFINITION_DIR + '3DMM_'+element+'_basis.dat'
print 'Loading ' + fn + ' ...'
fd = open(fn)
all_paras = np.fromfile(file=fd, dtype=np.float32)
fd.close()
all_paras = np.transpose(all_paras.reshape((-1,N)).astype(np.float32))
mu = all_paras[:,0]
w = all_paras[:,1:]
print ' DONE'
return mu, w
def load_const_alb_mask():
fd = open(_3DMM_DEFINITION_DIR + '3DMM_const_alb_mask.dat')
const_alb_mask = np.fromfile(file=fd, dtype=np.uint8)
fd.close()
const_alb_mask = const_alb_mask - 1
const_alb_mask = const_alb_mask.reshape((-1,2)).astype(np.uint8)
return const_alb_mask
def load_3DMM_tri_2d_barycoord():
fd = open(_3DMM_DEFINITION_DIR + '3DMM_tri_2d_barycoord.dat')
tri_2d_barycoord = np.fromfile(file=fd, dtype=np.float32)
fd.close()
tri_2d_barycoord = tri_2d_barycoord.reshape(192, 224, 3)
return tri_2d_barycoord