forked from abdallahdib/NextFace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
124 lines (110 loc) · 4.09 KB
/
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
import numpy as np
import torch
import cv2
import os
def saveObj(filename, materialName, vertices, faces, normals = None, tcoords = None, textureFileName = 'texture.png'):
'''
write mesh to an obj file
:param filename: path to where to save the obj file
:param materialFileName: material name
:param vertices: float tensor [n, 3]
:param faces: tensor [#triangles, 3]
:param normals: float tensor [n, 3]
:param tcoords: float tensor [n, 2]
:param textureFileName: name of the texture to use with material
:return:
'''
assert(vertices.dim() == 2 and vertices.shape[-1] == 3)
assert (faces.dim() == 2 and faces.shape[-1] == 3)
if normals is not None:
assert (normals.dim() == 2 and normals.shape[-1] == 3)
if tcoords is not None:
assert (tcoords.dim() == 2 and tcoords.shape[-1] == 2)
if torch.is_tensor(vertices):
vertices = vertices.detach().cpu().numpy()
if torch.is_tensor(faces):
faces = faces.detach().cpu().numpy()
if torch.is_tensor(normals):
normals = normals.detach().cpu().numpy()
if torch.is_tensor(tcoords):
tcoords = tcoords.detach().cpu().numpy()
assert(isinstance(vertices, np.ndarray))
assert (isinstance(faces, np.ndarray))
assert (isinstance(normals, np.ndarray))
assert (isinstance(tcoords, np.ndarray))
#write material
f = open(os.path.dirname(filename) + '/' + materialName, 'w')
f.write('newmtl material0\n')
f.write('map_Kd ' + textureFileName + '\n')
f.close()
f = open(filename, 'w')
f.write('###########################################################\n')
f.write('# OBJ file generated by faceYard 2021\n')
f.write('#\n')
f.write('# Num Vertices: %d\n' % (vertices.shape[0]))
f.write('# Num Triangles: %d\n' % (faces.shape[0]))
f.write('#\n')
f.write('###########################################################\n')
f.write('\n')
f.write('mtllib ' + materialName + '\n')
#write vertices
for v in vertices:
f.write('v %f %f %f\n' % (v[0], v[1], v[2]))
# write the tcoords
if tcoords is not None and tcoords.shape[0] > 0:
for uv in tcoords:
f.write('vt %f %f\n' % (uv[0], uv[1]))
#write the normals
if normals is not None and normals.shape[0] > 0:
for n in normals:
f.write('vn %f %f %f\n' % (n[0], n[1], n[2]))
f.write('usemtl material0\n')
#write face indices list
for t in faces:
f.write('f %d/%d/%d %d/%d/%d %d/%d/%d\n' % (t[0] + 1, t[0] + 1,t[0] + 1,
t[1] + 1, t[1] + 1,t[1] + 1,
t[2] + 1, t[2] + 1, t[2] + 1))
f.close()
def saveLandmarksVerticesProjections(imageTensor, projPoints, landmarks):
'''
for debug, render the projected vertices and landmakrs on image
:param images: [w, h, 3]
:param projPoints: [n, 3]
:param landmarks: [n, 2]
:return: tensor [w, h, 3
'''
assert(imageTensor.dim() == 3 and imageTensor.shape[-1] == 3 )
assert(projPoints.dim() == 2 and projPoints.shape[-1] == 2)
assert(projPoints.shape == landmarks.shape)
image = imageTensor.clone().detach().cpu().numpy() * 255.
landmarkCount = landmarks.shape[0]
for i in range(landmarkCount):
x = landmarks[i, 0]
y = landmarks[i, 1]
cv2.circle(image, (int(x), int(y)), 2, (0, 255, 0), -1)
x = projPoints[i, 0]
y = projPoints[i, 1]
cv2.circle(image, (int(x), int(y)), 2, (0, 0, 255), -1)
return image
def mkdir_p(path):
import errno
import os
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def loadDictionaryFromPickle(picklePath):
import pickle
handle = open(picklePath, 'rb')
assert handle is not None
dic = pickle.load(handle)
handle.close()
return dic
def writeDictionaryToPickle(dict, picklePath):
import pickle
handle = open(picklePath, 'wb')
pickle.dump(dict, handle, pickle.HIGHEST_PROTOCOL)
handle.close()