-
Notifications
You must be signed in to change notification settings - Fork 62
/
extract_measurements.py
139 lines (106 loc) · 3.74 KB
/
extract_measurements.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
import numpy as np
import sys
import os
import utils
DATA_DIR = "data"
# loading data: file_list, vertex, mean, std
#def obj2npy(label="male"):
#
# OBJ_DIR = os.path.join(DATA_DIR, "obj")
#
# obj_file_dir = os.path.join(OBJ_DIR, label)
## print("File directory = ",obj_file_dir)
# file_list = os.listdir(obj_file_dir)
#
# # load original data
# vertex = []
# for i, obj in enumerate(file_list):
# sys.stdout.write('\r>> Converting %s body %d\n'%(label, i))
# sys.stdout.flush()
# f = open(os.path.join(obj_file_dir, obj), 'r')
# j = 0
# for line in f:
# if line[0] == '#':
# continue
# elif "v " in line:
# line.replace('\n', ' ')
# tmp = list(map(float, line[1:].split()))
# vertex.append(tmp)
# j += 1
# else:
# break
#
# vertex = np.array(vertex).reshape(len(file_list), utils.V_NUM, 3)#utils.V_NUM
## print("Vertex are of type = ",type(vertex))
## print("vertex = ",vertex)
#
# return vertex
# read control points(CP) from text file
def convert_cp():
f = open(os.path.join(DATA_DIR, 'customBodyPoints.txt'), "r")
tmplist = []
cp = []
for line in f:
if '#' in line:
if len(tmplist) != 0:
cp.append(tmplist)
tmplist = []
elif len(line.split()) == 1:
continue
else:
tmplist.append(list(map(float, line.strip().split())))
cp.append(tmplist)
return cp
# calculate measure data from given vertex by control points
def calc_measure(cp, vertex,height):#, facet):
measure_list = []
for measure in cp:
# print("#########################",measure)
# print("@@@@@@@@@@@@")
length = 0.0
p2 = vertex[int(measure[0][1]), :]
for i in range(0, len(measure)):#1
p1 = p2
if measure[i][0] == 1:
p2 = vertex[int(measure[i][1]), :]
elif measure[i][0] == 2:
p2 = vertex[int(measure[i][1]), :] * measure[i][3] + \
vertex[int(measure[i][2]), :] * measure[i][4]
# print("if 2 Measurement",int(measure[i][1]))
else:
p2 = vertex[int(measure[i][1]), :] * measure[i][4] + \
vertex[int(measure[i][2]), :] * measure[i][5] + \
vertex[int(measure[i][3]), :] * measure[i][6]
length += np.sqrt(np.sum((p1 - p2)**2.0))
measure_list.append(length * 100)# * 1000
measure_list = float(height)*(measure_list/measure_list[0])
# print("measure list = ",float(height)*(measure_list/measure_list[0]))
measure_list[8] = measure_list[8] * 0.36#reducing the error in measurement added due to unarranged vertices
measure_list[3] = measure_list[3] * 0.6927
# print("measure list = ",float(height)*(measure_list/measure_list[0]))
# measure_list = float(height)*(measure_list/measure_list[0])
return np.array(measure_list).reshape(utils.M_NUM, 1)
##added code: extract body measurements given a .obj model in data.
def extract_measurements(height, vertices):
genders = ["male"]#, "male"]
measure = []
for gender in genders:
# generate and load control point from txt to npy file
cp = convert_cp()
# vertex = obj2npy(gender)[0]
#calculte + convert
measure = calc_measure(cp, vertices, height)
#give body measurements one by one
for i in range(0, utils.M_NUM):
print("%s: %f" % (utils.M_STR[i], measure[i]))
face_path = './src/tf_smpl/smpl_faces.npy'
faces = np.load(face_path)
obj_mesh_name = 'test.obj'
with open(obj_mesh_name, 'w') as fp:
for v in vertices:
fp.write( 'v %f %f %f\n' % ( v[0], v[1], v[2]) )
for f in faces: # Faces are 1-based, not 0-based in obj files
fp.write( 'f %d %d %d\n' % (f[0] + 1, f[1] + 1, f[2] + 1) )
print("Model Saved...")
#if __name__ == "__main__":
# extract_measurements()