-
Notifications
You must be signed in to change notification settings - Fork 16
/
data.py
316 lines (281 loc) · 12.3 KB
/
data.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import os
import sys
import numpy as np
import scipy.io as sio
from skimage import io
import time
import math
import skimage
import faceutil
from faceutil import mesh
from faceutil.morphable_model import MorphabelModel
from matlabutil import NormDirection
from math import sin, cos, asin, acos, atan, atan2
from PIL import Image
import matplotlib.pyplot as plt
# global data
bfm = MorphabelModel('data/Out/BFM.mat')
default_init_image_shape = np.array([450, 450, 3])
default_cropped_image_shape = np.array([256, 256, 3])
default_uvmap_shape = np.array([256, 256, 3])
face_mask_np = io.imread('uv-data/uv_face_mask.png') / 255.
face_mask_mean_fix_rate = (256 * 256) / np.sum(face_mask_np)
def process_uv(uv_coordinates):
[uv_h, uv_w, uv_c] = default_uvmap_shape
uv_coordinates[:, 0] = uv_coordinates[:, 0] * (uv_w - 1)
uv_coordinates[:, 1] = uv_coordinates[:, 1] * (uv_h - 1)
uv_coordinates[:, 1] = uv_h - uv_coordinates[:, 1] - 1
uv_coordinates = np.hstack((uv_coordinates, np.zeros((uv_coordinates.shape[0], 1)))) # add z
return uv_coordinates
def readUVKpt(uv_kpt_path):
file = open(uv_kpt_path, 'r', encoding='utf-8')
lines = file.readlines()
# txt is inversed
x_line = lines[1]
y_line = lines[0]
uv_kpt = np.zeros((68, 2)).astype(int)
x_tokens = x_line.strip().split(' ')
y_tokens = y_line.strip().split(' ')
for i in range(68):
uv_kpt[i][0] = int(float(x_tokens[i]))
uv_kpt[i][1] = int(float(y_tokens[i]))
return uv_kpt
# global data
uv_coords = faceutil.morphable_model.load.load_uv_coords('data/Out/BFM_UV.mat')
uv_coords = process_uv(uv_coords)
uv_kpt = readUVKpt('uv-data/uv_kpt_ind.txt')
uvmap_place_holder = np.ones((256, 256, 1))
def getLandmark(ipt):
# from uv map
kpt = ipt[uv_kpt[:, 0], uv_kpt[:, 1]]
return kpt
def bfm2Mesh(bfm_info, image_shape=default_init_image_shape):
"""
generate mesh data from 3DMM (bfm2009) parameters
:param bfm_info:
:param image_shape:
:return: meshe data
"""
[image_h, image_w, channel] = image_shape
pose_para = bfm_info['Pose_Para'].T.astype(np.float32)
shape_para = bfm_info['Shape_Para'].astype(np.float32)
exp_para = bfm_info['Exp_Para'].astype(np.float32)
tex_para = bfm_info['Tex_Para'].astype(np.float32)
color_Para = bfm_info['Color_Para'].astype(np.float32)
illum_Para = bfm_info['Illum_Para'].astype(np.float32)
# 2. generate mesh_numpy
# shape & exp param
vertices = bfm.generate_vertices(shape_para, exp_para)
# texture param
tex = bfm.generate_colors(tex_para)
norm = NormDirection(vertices, bfm.model['tri'])
# color param
[Gain_r, Gain_g, Gain_b, Offset_r, Offset_g, Offset_b, c] = color_Para[0]
M = np.array([[0.3, 0.59, 0.11], [0.3, 0.59, 0.11], [0.3, 0.59, .11]])
g = np.diag([Gain_r, Gain_g, Gain_b])
o = [Offset_r, Offset_g, Offset_b]
o = np.tile(o, (vertices.shape[0], 1))
# illum param
[Amb_r, Amb_g, Amb_b, Dir_r, Dir_g, Dir_b, thetal, phil, ks, v] = illum_Para[0]
Amb = np.diag([Amb_r, Amb_g, Amb_b])
Dir = np.diag([Dir_r, Dir_g, Dir_b])
l = np.array([math.cos(thetal) * math.sin(phil), math.sin(thetal), math.cos(thetal) * math.cos(phil)]).T
h = l + np.array([0, 0, 1]).T
h = h / math.sqrt(h.T.dot(h))
# final color
n_l = l.T.dot(norm.T)
n_h = h.T.dot(norm.T)
n_l = np.array([max(x, 0) for x in n_l])
n_h = np.array([max(x, 0) for x in n_h])
n_l = np.tile(n_l, (3, 1))
n_h = np.tile(n_h, (3, 1))
L = Amb.dot(tex.T) + Dir.dot(n_l * tex.T) + (ks * Dir).dot((n_h ** v))
CT = g.dot(c * np.eye(3) + (1 - c) * M)
tex_color = CT.dot(L) + o.T
tex_color = np.minimum(np.maximum(tex_color, 0), 1).T
# transform mesh_numpy
s = pose_para[-1, 0]
angles = pose_para[:3, 0]
t = pose_para[3:6, 0]
# 3ddfa-R: radian || normal transform - R:degree
transformed_vertices = bfm.transform_3ddfa(vertices, s, angles, t)
projected_vertices = transformed_vertices.copy() # using stantard camera & orth projection as in 3DDFA
image_vertices = projected_vertices.copy()
# should not -1
image_vertices[:, 1] = image_h - image_vertices[:, 1]
mesh_info = {'vertices': image_vertices, 'triangles': bfm.full_triangles,
'full_triangles': bfm.full_triangles,
'colors': tex_color}
# 'landmarks': bfm_info['pt3d_68'].T
return mesh_info
def UVmap2Mesh(uv_position_map, uv_texture_map=None, only_foreface=True, is_extra_triangle=False):
"""
if no texture map is provided, translate the position map to a point cloud
:param uv_position_map:
:param uv_texture_map:
:param only_foreface:
:return: mesh data
"""
[uv_h, uv_w, uv_c] = default_uvmap_shape
vertices = []
colors = []
triangles = []
if uv_texture_map is not None:
for i in range(uv_h):
for j in range(uv_w):
if not only_foreface:
vertices.append(uv_position_map[i][j])
colors.append(uv_texture_map[i][j])
pa = i * uv_h + j
pb = i * uv_h + j + 1
pc = (i - 1) * uv_h + j
pd = (i + 1) * uv_h + j + 1
if (i > 0) & (i < uv_h - 1) & (j < uv_w - 1):
triangles.append([pa, pb, pc])
triangles.append([pa, pc, pb])
triangles.append([pa, pb, pd])
triangles.append([pa, pd, pb])
else:
if face_mask_np[i, j] == 0:
vertices.append(np.array([0, 0, 0]))
colors.append(np.array([0, 0, 0]))
continue
else:
vertices.append(uv_position_map[i][j])
colors.append(uv_texture_map[i][j])
pa = i * uv_h + j
pb = i * uv_h + j + 1
pc = (i - 1) * uv_h + j
pd = (i + 1) * uv_h + j + 1
if (i > 0) & (i < uv_h - 1) & (j < uv_w - 1):
if is_extra_triangle:
pe = (i - 1) * uv_h + j + 1
pf = (i + 1) * uv_h + j
if (face_mask_np[i, j + 1] > 0) and (face_mask_np[i + 1, j + 1] > 0) and (face_mask_np[i + 1, j] > 0) and (
face_mask_np[i - 1, j + 1] > 0 and face_mask_np[i - 1, j] > 0):
triangles.append([pa, pb, pc])
triangles.append([pa, pc, pb])
triangles.append([pa, pc, pe])
triangles.append([pa, pe, pc])
triangles.append([pa, pb, pe])
triangles.append([pa, pe, pb])
triangles.append([pb, pc, pe])
triangles.append([pb, pe, pc])
triangles.append([pa, pb, pd])
triangles.append([pa, pd, pb])
triangles.append([pa, pb, pf])
triangles.append([pa, pf, pb])
triangles.append([pa, pd, pf])
triangles.append([pa, pf, pd])
triangles.append([pb, pd, pf])
triangles.append([pb, pf, pd])
else:
if not face_mask_np[i, j + 1] == 0:
if not face_mask_np[i - 1, j] == 0:
triangles.append([pa, pb, pc])
triangles.append([pa, pc, pb])
if not face_mask_np[i + 1, j + 1] == 0:
triangles.append([pa, pb, pd])
triangles.append([pa, pd, pb])
else:
for i in range(uv_h):
for j in range(uv_w):
if not only_foreface:
vertices.append(uv_position_map[i][j])
colors.append(np.array([64, 64, 64]))
pa = i * uv_h + j
pb = i * uv_h + j + 1
pc = (i - 1) * uv_h + j
if (i > 0) & (i < uv_h - 1) & (j < uv_w - 1):
triangles.append([pa, pb, pc])
else:
if face_mask_np[i, j] == 0:
vertices.append(np.array([0, 0, 0]))
colors.append(np.array([0, 0, 0]))
continue
else:
vertices.append(uv_position_map[i][j])
colors.append(np.array([128, 0, 128]))
pa = i * uv_h + j
pb = i * uv_h + j + 1
pc = (i - 1) * uv_h + j
if (i > 0) & (i < uv_h - 1) & (j < uv_w - 1):
if not face_mask_np[i, j + 1] == 0:
if not face_mask_np[i - 1, j] == 0:
triangles.append([pa, pb, pc])
triangles.append([pa, pc, pb])
vertices = np.array(vertices)
colors = np.array(colors)
triangles = np.array(triangles)
# verify_face = mesh.render.render_colors(verify_vertices, verify_triangles, verify_colors, height, width,
# channel)
mesh_info = {'vertices': vertices, 'triangles': triangles,
'full_triangles': triangles,
'colors': colors}
return mesh_info
def mesh2UVmap(mesh_data):
"""
generate uv map from mesh data
:param mesh_data:
:return: uv position map and corresponding texture
"""
[uv_h, uv_w, uv_c] = default_uvmap_shape
vertices = mesh_data['vertices']
colors = mesh_data['colors']
triangles = mesh_data['full_triangles']
# colors = colors / np.max(colors)
# model_image = mesh.render.render_colors(vertices, bfm.triangles, colors, image_h, image_w) # only for show
uv_texture_map = mesh.render.render_colors(uv_coords, triangles, colors, uv_h, uv_w, uv_c)
position = vertices.copy()
position[:, 2] = position[:, 2] - np.min(position[:, 2]) # translate z
uv_position_map = mesh.render.render_colors(uv_coords, triangles, position, uv_h, uv_w, uv_c)
return uv_position_map, uv_texture_map
def renderMesh(mesh_info, image_shape=None):
if image_shape is None:
image_height = np.ceil(np.max(mesh_info['vertices'][:, 1])).astype(int)
image_width = np.ceil(np.max(mesh_info['vertices'][:, 0])).astype(int)
else:
[image_height, image_width, image_channel] = image_shape
mesh_image = mesh.render.render_colors(mesh_info['vertices'],
mesh_info['triangles'],
mesh_info['colors'], image_height, image_width)
mesh_image = np.clip(mesh_image, 0., 1.)
return mesh_image
def getTransformMatrix(s, angles, t, height):
x, y, z = angles[0], angles[1], angles[2]
Rx = np.array([[1, 0, 0],
[0, cos(x), sin(x)],
[0, -sin(x), cos(x)]])
Ry = np.array([[cos(y), 0, -sin(y)],
[0, 1, 0],
[sin(y), 0, cos(y)]])
Rz = np.array([[cos(z), sin(z), 0],
[-sin(z), cos(z), 0],
[0, 0, 1]])
# rotate
R = Rx.dot(Ry).dot(Rz)
R = R.astype(np.float32)
T = np.zeros((4, 4))
T[0:3, 0:3] = R
T[3, 3] = 1.
# scale
S = np.diagflat([s, s, s, 1.])
T = S.dot(T)
# offset move
M = np.diagflat([1., 1., 1., 1.])
M[0:3, 3] = t.astype(np.float32)
T = M.dot(T)
# revert height
# x[:,1]=height-x[:,1]
H = np.diagflat([1., 1., 1., 1.])
H[1, 1] = -1.0
H[1, 3] = height
T = H.dot(T)
return T.astype(np.float32)
def getColors(image, posmap):
[h, w, _] = image.shape
[uv_h, uv_w, uv_c] = posmap.shape
# tex = np.zeros((uv_h, uv_w, uv_c))
around_posmap = np.around(posmap).clip(0, h - 1).astype(np.int)
tex = image[around_posmap[:, :, 1], around_posmap[:, :, 0], :]
return tex