forked from stephen-w-bailey/fast-n-deep-faces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizeApproximation.py
434 lines (380 loc) · 16.1 KB
/
visualizeApproximation.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import argparse
import functools
import matplotlib.pyplot as plt
import numpy as np
import os
import pickle
from skimage.transform import resize
import sys
import tensorflow as tf
import time
import trimesh
import yaml
from pathlib import Path
basePath = (Path(__file__).parent).resolve()
sys.path.append(os.path.join(basePath, 'cnnModel'))
from PySide2 import QtCore,QtWidgets
import cnnModel
import cnnOpt
import render
import UVGenerator
import rigidDeformer
class Viewer(QtWidgets.QWidget):
def __init__(self,rigData,config,animFile,checkpoint,sess):
super(Viewer,self).__init__()
self.loadCache(rigData,config)
if 'base_config' in config['model_params']:
print('Loading refinement')
with tf.variable_scope('refine'):
self.buildModel(config,animFile,applyNeutral=False)
self.refineModel = self.model
print('Loading base model')
with open(os.path.join(basePath,config['model_params']['base_config'])) as file:
baseConfig = yaml.load(file)
print('Loading cache file '+baseConfig['data_params']['cache_file'])
with open(os.path.join(basePath,baseConfig['data_params']['cache_file']),'rb') as file:
baseCache = pickle.load(file)
self.loadCache(baseCache,baseConfig)
self.buildModel(baseConfig,animFile)
self.model['refine_output'] = self.model['output'] + self.refineModel['output']
else:
self.buildModel(config,animFile)
baseConfig = config
#self.images = self.model['images']
# Load the mesh viewer
obj = render.mesh.RenderObject(self.vertices,self.normals,self.texture,dynamic=True)
scene = render.Scene()
scene.addMesh(obj)
self.view = render.Viewer(scene)
self.view.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding,QtWidgets.QSizePolicy.MinimumExpanding)
self.mesh = obj
# Load the control list
controlFile = config['data_params']['control_file']
controls = []
controlDefault = []
controlRange = []
with open(controlFile) as file:
for line in file:
if ',' in line:
parts = line.strip().split(',')
else:
parts = line.strip().split(' ')
parts = [parts[0]+'-'+parts[1]]+parts[2:]
controls.append(parts[0]+'-'+parts[1])
controlRange.append((float(parts[2]),float(parts[3])))
if len(parts) > 4:
controlDefault.append(float(parts[4]))
else:
controlDefault.append(0.0)
self.controlRange = controlRange
# Load the timeline
if self.anim is not None:
timeline = QtWidgets.QSlider(QtCore.Qt.Horizontal)
timeline.setRange(0,len(self.anim)-1)
timeline.valueChanged.connect(self.onTimelineMove)
self.timelineText = QtWidgets.QLabel('1/'+str(len(self.anim)))
self.timeline = timeline
self.playButton = QtWidgets.QPushButton('Play')
self.playTimer = QtCore.QTimer()
#self.playTimer.setInterval(33)
self.playButton.clicked.connect(self.onPlayPress)
self.playTimer.timeout.connect(self.onNextFrame)
# Create the control GUI
controlWidget = QtWidgets.QWidget()
controlLayout = QtWidgets.QGridLayout(controlWidget)
controlWidget.setSizePolicy(QtWidgets.QSizePolicy.Maximum,QtWidgets.QSizePolicy.Minimum)
controlList = QtWidgets.QListWidget()
scrollArea = QtWidgets.QScrollArea()
scrollArea.setWidgetResizable(True)
scrollArea.setWidget(controlWidget)
scrollArea.setSizePolicy(QtWidgets.QSizePolicy.Maximum,QtWidgets.QSizePolicy.MinimumExpanding)
self.refineCheck = QtWidgets.QCheckBox('Apply refinement')
self.refineCheck.setChecked(True)
self.refineCheck.stateChanged.connect(self.onRefineClick)
# Create the animation selection widget
if animFile is not None:
self.animSelect = QtWidgets.QListWidget()
for k in self.animNames:
length = len(self.fullAnim[k])
label = k+' ('+str(length)+')'
self.animSelect.addItem(label)
self.animSelect.show()
self.animSelect.currentRowChanged.connect(self.onAnimChanged)
self.sliders = []
self.sliderText = []
for i in range(len(controls)):
label = QtWidgets.QLabel(controls[i])
value = QtWidgets.QLabel('{0:.3f}'.format(controlDefault[i]))
slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
slider.setRange(0,1000)
slider.setMinimumWidth(300)
controlLayout.addWidget(label,i,0,1,1)
controlLayout.addWidget(value,i,1,1,1)
controlLayout.addWidget(slider,i,2,1,1)
sliderFunc = functools.partial(self.onSliderChange,index=i)
slider.valueChanged.connect(sliderFunc)
slider.sliderMoved.connect(self.onSliderMoved)
self.sliders.append(slider)
self.sliderText.append(value)
# Put the gui together
gridLayout = QtWidgets.QGridLayout(self)
gridLayout.addWidget(self.view,0,0,1,1)
gridLayout.addWidget(scrollArea,0,1,1,1)
if animFile is not None:
gridLayout.addWidget(timeline,1,0,1,2)
gridLayout.addWidget(self.timelineText,2,0,1,1)
gridLayout.addWidget(self.playButton,2,1,1,1)
gridLayout.addWidget(self.refineCheck,3,1,1,1)
self.status = QtWidgets.QStatusBar()
gridLayout.addWidget(self.status,3,0,1,2)
self.sess=sess
if os.path.isdir(checkpoint):
checkpoint = tf.train.latest_checkpoint(checkpoint)
else:
checkpoint = checkpoint
saver = tf.train.Saver()
saver.restore(sess,checkpoint)
# Compute rigid parts and normals
with tf.device('/CPU:0'):
self.rigidDeformer = None
if 'rigid_files' in baseConfig['data_params']:
mask = np.arange(len(self.neutral))[self.parts>-1]
self.rigidDeformer = rigidDeformer.RigidDeformer(self.neutral,baseConfig['data_params']['rigid_files'],mask)
self.model['output'] = self.rigidDeformer.deformTF(self.model['output'][0])[np.newaxis]
v = self.model['output'][0]
normals = cnnOpt.faceNormals(v,self.faces)
normals = cnnOpt.vertexNormals(v,normals,self.faces)
normals = tf.gather(normals,self.faces.reshape(-1))
normals = tf.reshape(normals,(-1,3,3))
#normals = tf.tile(normals[:,np.newaxis],(1,3,1))
self.model['normals'] = normals
if 'refine_output' in self.model:
if 'rigid_files' in baseConfig['data_params']:
self.model['refine_output'] = self.rigidDeformer.deformTF(self.model['refine_output'][0])[np.newaxis]
v = self.model['refine_output'][0]
normals = cnnOpt.faceNormals(v,self.faces)
normals = cnnOpt.vertexNormals(v,normals,self.faces)
normals = tf.gather(normals,self.faces.reshape(-1))
normals = tf.reshape(normals,(-1,3,3))
#normals = tf.tile(normals[:,np.newaxis],(1,3,1))
self.model['refine_normals'] = normals
if animFile is not None:
self.setFrame(0)
else:
self.setPose(controlDefault)
def onSliderChange(self,value,index):
r = self.controlRange[index]
text = self.sliderText[index]
v = value/1000
v = v*(r[1]-r[0])+r[0]
text.setText('{0:.3f}'.format(v))
def onSliderMoved(self,value):
pose = self.getSliderPose()
self.setPose(pose)
def onAnimChanged(self,row):
k = self.animNames[row]
self.anim = self.fullAnim[k]
self.setFrame(0)
self.timeline.setSliderPosition(0)
self.timeline.setRange(0,len(self.anim)-1)
self.status.showMessage('Showing '+k)
def loadCache(self,rigData,config):
# Load the mesh data
self.data = rigData
neutral = self.data['neutral']/1.5
active = self.data['active']
neutral = neutral[active].astype('float32')
meshMean = np.mean(neutral,0)
neutral = neutral - meshMean
faces = self.data['faces']
parts = self.data['vCharts']
uv = self.data['uv']
mesh = trimesh.Trimesh(neutral,faces,process=False)
vertices = neutral[faces.reshape(-1)].reshape((-1,3,3))
normals = np.repeat(mesh.face_normals[:,np.newaxis],3,1)
texture = 0.75 * np.ones(normals.shape)
if 'hide_unused' in config['model_params'] and config['model_params']['hide_unused']:
unusedVerts = parts==-1
faceCount = unusedVerts[faces.reshape(-1)].reshape((-1,3))
faceCount = np.sum(faceCount,-1)
faces = faces[faceCount==0]
self.faces = faces
self.parts = parts
self.neutral = neutral
self.uv = uv
if 'parameter_mask' in self.data:
self.mask = self.data['parameter_mask']
else:
self.mask = None
self.vertices = vertices
self.normals = normals
self.texture = texture
def buildModel(self,config,animFile,applyNeutral=True):
# Load the animation sequence
if animFile is not None:
with open(animFile,'rb') as file:
anim = pickle.load(file,encoding='latin1')
self.fullAnim = anim
self.animNames = sorted([k for k in anim])
self.anim = anim[self.animNames[0]]
controls = self.anim.shape[1]
else:
self.anim = None
controls = 0
with open(config['data_params']['control_file']) as file:
for _ in file:
controls += 1
# Create the model
if not hasattr(self,'posePH'):
self.posePH = tf.placeholder(tf.float32,shape=(1,controls))
partCount = np.max(self.parts)+1
data = {'pose':self.posePH}
self.usedVerts = []
self.usedUVs = []
for i in range(partCount):
if np.sum(self.parts==i) > 0:
data['image-'+str(i)] = tf.ones(1)
else:
data['image-'+str(i)] = None
ref = self.faces.reshape(-1)
idx = np.arange(len(self.neutral))[self.parts==i]
if len(idx) == 0:
continue
uv = self.uv[idx]
self.usedUVs.append(uv)
self.usedVerts.append(idx)
idx = np.concatenate(self.usedVerts)
linear = np.zeros(self.neutral.shape,dtype='float32')
if applyNeutral:
linear[idx] = self.neutral[idx]
neutral = self.neutral
else:
neutral = linear
data['linear'] = linear
self.model = cnnModel.buildModel(data,self,neutral,config)
def setSelection(self,index):
texture = 0.75 * np.ones(self.neutral.shape,dtype='float32')
texture[self.parts==index] = [1,0,0]
texture = texture[self.faces.reshape(-1)].reshape((-1,3,3))
self.mesh.reloadMesh(t=texture)
def getUV(self,index):
usedV = np.zeros(len(self.neutral),dtype='bool')
usedV[self.parts == index] = True
usedT = usedV[self.faces.reshape(-1)].reshape((-1,3))
usedT = np.sum(usedT,-1)==3
usedVIdx = np.arange(len(self.neutral))[usedV]
usedTIdx = np.asarray(list(set(list(self.faces[usedT].reshape(-1)))))
diff = len(usedVIdx)-len(usedTIdx)
print('Vertices missing in triangles: '+str(diff))
plt.triplot(self.uv[:,0],self.uv[:,1],self.faces[usedT])
plt.axis('equal')
plt.show()
def onNextFrame(self):
frame = (self.timeline.value()+1)
if frame < len(self.anim):
self.timeline.setSliderPosition(frame)
else:
self.stop()
def onTimelineMove(self,value):
self.timelineText.setText(str(value+1)+'/'+str(len(self.anim)))
self.setFrame(value)
def setFrame(self,index):
pose = self.anim[index]
self.setPose(pose)
def onPlayPress(self):
if self.playTimer.isActive():
self.stop()
else:
self.play()
def stop(self):
self.playButton.setText('Play')
self.playTimer.stop()
def play(self):
self.playButton.setText('Stop')
self.playTimer.start(33)
def getSliderPose(self):
pose = []
for r,slider in zip(self.controlRange,self.sliders):
v = slider.sliderPosition()/1000
v = v*(r[1]-r[0])+r[0]
pose.append(v)
pose = np.asarray(pose).astype('float32')
return pose
def onRefineClick(self):
pose = self.getSliderPose()
self.setPose(pose)
def setPose(self,pose):
for i in range(len(pose)):
v = pose[i]
r = self.controlRange[i]
v = int((v-r[0])/(max(r[1]-r[0],1e-6))*1000)
self.sliders[i].setSliderPosition(v)
pose = self.getSliderPose()
if self.refineCheck.isChecked() and 'refine_output' in self.model:
output = 'refine_output'
normals = 'refine_normals'
else:
output = 'output'
normals = 'normals'
feed_dict = {self.posePH:pose[np.newaxis]}
start = time.time()
verts,normals = self.sess.run((self.model[output],self.model[normals]),feed_dict=feed_dict)
verts = verts[0]
elapsed = time.time()-start
#if self.rigidDeformer is not None:
# verts = self.rigidDeformer.deform(verts)
#mesh = trimesh.Trimesh(verts,self.faces,process=False)
vertices = verts[self.faces.reshape(-1)].reshape((-1,3,3))
#normals = np.repeat(mesh.face_normals[:,np.newaxis],3,1)
self.mesh.reloadMesh(vertices,normals)
self.view.update()
self.status.showMessage('Evaluation time: {:.0f}ms'.format(elapsed*1000))
def main():
parser = argparse.ArgumentParser(description='Visualize a mesh for parameterization')
parser.add_argument('--configFile', type=str, required=True)
parser.add_argument('--checkpoint', type=str, required=True)
parser.add_argument('--animFile', type=str)
args = parser.parse_args()
with open(args.configFile) as file:
config = yaml.load(file)
with open(os.path.join(basePath,config['data_params']['cache_file']),'rb') as file:
data = pickle.load(file)
neutral = data['neutral']
active = data['active']
neutral = neutral[active].astype('float32')
meshMean = np.mean(neutral,0)
neutral = neutral - meshMean
faces = data['faces']
parts = data['vCharts']
#parts = 15*np.ones(len(faces),dtype='int32')
mesh = trimesh.Trimesh(neutral,faces,process=False)
vertices = neutral[faces.reshape(-1)].reshape((-1,3,3))
normals = np.repeat(mesh.face_normals[:,np.newaxis],3,1)
texture = 0.75 * np.ones(normals.shape)
with tf.Session() as sess:
app = QtWidgets.QApplication(sys.argv)
v = Viewer(data,config,args.animFile,args.checkpoint,sess)
v.show()
partIndex = -1
maxParts = np.max(parts)+1
visibleParts = np.zeros(maxParts,dtype=bool)
def nextSample():
nonlocal partIndex
partIndex = (partIndex+1)%maxParts
print('Showing part '+str(partIndex))
texture = 0.75 * np.ones(normals.shape,dtype='float32')
texture[parts==partIndex] = [1,0,0]
mesh.reloadMesh(t=texture)
v.update()
def onKeyboard(key):
print('Pressed '+str(key))
if key == render.viewer.pygame.K_v:
visibleParts[partIndex] = True
nextSample()
if key == render.viewer.pygame.K_SPACE:
nextSample()
#v.addKeyboardCallback(onKeyboard)
#nextSample()
sys.exit(app.exec_())
if __name__=='__main__':
main()