File size: 5,982 Bytes
b807ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import trimesh
import numpy as np
import skimage.io as io
from PIL import Image
from smplx import SMPL
from matplotlib import cm as mpl_cm, colors as mpl_colors
from trimesh.visual.color import face_to_vertex_color, vertex_to_face_color, to_rgba

from common import constants
from .colorwheel import make_color_wheel_image


def get_smpl_uv():
    uv_obj = 'data/body_models/smpl_uv_20200910/smpl_uv.obj'

    uv_map = []
    with open(uv_obj) as f:
        for line in f.readlines():
            if line.startswith('vt'):
                coords = [float(x) for x in line.split(' ')[1:]]
                uv_map.append(coords)

    uv_map = np.array(uv_map)

    return uv_map


def show_uv_texture():
    # image = io.imread('data/body_models/smpl_uv_20200910/smpl_uv_20200910.png')
    image = make_color_wheel_image(1024, 1024)
    image = Image.fromarray(image)

    uv = np.load('data/body_models/smpl_uv_20200910/uv_table.npy') # get_smpl_uv()
    material = trimesh.visual.texture.SimpleMaterial(image=image)
    tex_visuals = trimesh.visual.TextureVisuals(uv=uv, image=image, material=material)

    smpl = SMPL(constants.SMPL_MODEL_DIR)

    faces = smpl.faces
    verts = smpl().vertices[0].detach().numpy()

    # assert(len(uv) == len(verts))
    print(uv.shape)
    vc = tex_visuals.to_color().vertex_colors
    fc = trimesh.visual.color.vertex_to_face_color(vc, faces)
    face_colors = fc.copy()
    fc = fc.astype(float)
    vc = vc.astype(float)
    fc[:,:3] = fc[:,:3] / 255.
    vc[:,:3] = vc[:,:3] / 255.
    print(fc[:,:3].max(), fc[:,:3].min(), fc[:,:3].mean())
    print(vc[:, :3].max(), vc[:, :3].min(), vc[:, :3].mean())
    np.save('data/body_models/smpl/color_wheel_face_colors.npy', fc)
    np.save('data/body_models/smpl/color_wheel_vertex_colors.npy', vc)
    print(fc.shape)
    mesh = trimesh.Trimesh(verts, faces, validate=True, process=False, face_colors=face_colors)
    # mesh = trimesh.load('data/body_models/smpl_uv_20200910/smpl_uv.obj', process=False)
    # mesh.visual = tex_visuals

    # import ipdb; ipdb.set_trace()
    # print(vc.shape)
    mesh.show()


def show_colored_mesh():
    cm = mpl_cm.get_cmap('jet')
    norm_gt = mpl_colors.Normalize()

    smpl = SMPL(constants.SMPL_MODEL_DIR)

    faces = smpl.faces
    verts = smpl().vertices[0].detach().numpy()

    m = trimesh.Trimesh(verts, faces, process=False)

    mode = 1
    if mode == 0:
        # mano_segm_labels = m.triangles_center
        face_labels = m.triangles_center
        face_colors = (face_labels - face_labels.min()) / np.ptp(face_labels)

    elif mode == 1:
        # print(face_labels.shape)
        face_labels = m.triangles_center
        face_labels = np.argsort(np.linalg.norm(face_labels, axis=-1))
        face_colors = np.ones((13776, 4))
        face_colors[:, 3] = 1.0
        face_colors[:, :3] = cm(norm_gt(face_labels))[:, :3]
    elif mode == 2:
        # breakpoint()
        fc = np.load('data/body_models/smpl_uv_20200910/data/vertex_texture.npy')[0, :, 0, 0, 0, :]
        face_colors = np.ones((13776, 4))
        face_colors[:, :3] = fc
    mesh = trimesh.Trimesh(verts, faces, process=False, face_colors=face_colors)
    mesh.show()


def get_tenet_texture(mode='smplpix'):
    # mode = 'smplpix', 'decomr'

    smpl = SMPL(constants.SMPL_MODEL_DIR)

    faces = smpl.faces
    verts = smpl().vertices[0].detach().numpy()

    m = trimesh.Trimesh(verts, faces, process=False)
    if mode == 'smplpix':
        # mano_segm_labels = m.triangles_center
        face_labels = m.triangles_center
        face_colors = (face_labels - face_labels.min()) / np.ptp(face_labels)
        texture = np.zeros((1, faces.shape[0], 1, 1, 1, 3), dtype=np.float32)
        texture[0, :, 0, 0, 0, :] = face_colors[:, :3]
        texture = torch.from_numpy(texture).float()
    elif mode == 'decomr':
        texture = np.load('data/body_models/smpl_uv_20200910/data/vertex_texture.npy')
        texture = torch.from_numpy(texture).float()
    elif mode == 'colorwheel':
        face_colors = np.load('data/body_models/smpl/color_wheel_face_colors.npy')
        texture = np.zeros((1, faces.shape[0], 1, 1, 1, 3), dtype=np.float32)
        texture[0, :, 0, 0, 0, :] = face_colors[:, :3]
        texture = torch.from_numpy(texture).float()
    else:
        raise ValueError(f'{mode} is not defined!')

    return texture


def save_tenet_textures(mode='smplpix'):
    # mode = 'smplpix', 'decomr'

    smpl = SMPL(constants.SMPL_MODEL_DIR)

    faces = smpl.faces
    verts = smpl().vertices[0].detach().numpy()

    m = trimesh.Trimesh(verts, faces, process=False)

    if mode == 'smplpix':
        # mano_segm_labels = m.triangles_center
        face_labels = m.triangles_center
        face_colors = (face_labels - face_labels.min()) / np.ptp(face_labels)
        texture = np.zeros((1, faces.shape[0], 1, 1, 1, 3), dtype=np.float32)
        texture[0, :, 0, 0, 0, :] = face_colors[:, :3]
        texture = torch.from_numpy(texture).float()

        vert_colors = face_to_vertex_color(m, face_colors).astype(float) / 255.0

    elif mode == 'decomr':
        texture = np.load('data/body_models/smpl_uv_20200910/data/vertex_texture.npy')
        texture = torch.from_numpy(texture).float()
        face_colors = texture[0, :, 0, 0, 0, :]
        vert_colors = face_to_vertex_color(m, face_colors).astype(float) / 255.0

    elif mode == 'colorwheel':
        face_colors = np.load('data/body_models/smpl/color_wheel_face_colors.npy')
        texture = np.zeros((1, faces.shape[0], 1, 1, 1, 3), dtype=np.float32)
        texture[0, :, 0, 0, 0, :] = face_colors[:, :3]
        texture = torch.from_numpy(texture).float()
        face_colors[:, :3] *= 255
        vert_colors = face_to_vertex_color(m, face_colors).astype(float) / 255.0
    else:
        raise ValueError(f'{mode} is not defined!')

    print(vert_colors.shape, vert_colors.max())
    np.save(f'data/body_models/smpl/{mode}_vertex_colors.npy', vert_colors)
    return texture