Exemplo n.º 1
0
    def __init__(self):

        self.beta_gt = BetaGroundTruth().load(conf_path('beta_gt'))
        self.cloth = Mesh(self.beta_gt.template)
        self.beta = np.zeros(10)
        self.pose = np.zeros((24, 3))
        self.trans = np.zeros(3)
        self.last_beta = np.zeros(10)
        self.shaped_cloth = None
        self.smpl = SMPLModel(conf_path('smpl'))
        vs, fs = self.smpl.set_params(self.pose, self.beta, self.trans)
        self.body = Mesh().from_vertices(vs, fs)
        # 暂存0状态normal
        self.cloth_base_normal = self.cloth.normal
        self.body_base_normal = self.body.normal
        self.canvas = [Canvas(), Canvas()]
        self.shape_regressor = ShapeRegressor(self.canvas[0], conf_path('mlp'))
        self.pose_regressor = PoseRegressor(self.canvas[1], conf_path('gru'),
                                            conf_value('gru_step'))
        self.reset_cloth()
        self.relation = ClosestVertex().load(conf_path('vert_rela'))
        self.cloth_weights = np.zeros((len(self.cloth.vertices), 24))
        rela = self.relation.get_rela()
        for i in range(len(self.cloth.vertices)):
            self.cloth_weights[i] = self.smpl.weights[rela[i]]
Exemplo n.º 2
0
def gen_body(beta, pose):
    m = Mesh()
    smpl.set_params(beta=beta, pose=pose)
    m.vertices = smpl.verts
    m.faces = smpl.faces
    m.update()
    m.save("tst.obj")
Exemplo n.º 3
0
def smooth_hcl(mesh, times):
    origin = Mesh(mesh)
    for i in range(times):
        taubin(mesh, 1, 1)
        push_back_vertex(origin, mesh, 0.2)
        taubin(mesh, -0.2, 1)
    return mesh
Exemplo n.º 4
0
 def reset_cloth(self, body_flag=True):
     if self.shaped_cloth is None or (self.last_beta != self.beta).any():
         self.shaped_cloth = Mesh(self.beta_gt.template)
         self.shaped_cloth.vertices += self.shape_regressor.gen(self.beta)
         self.last_beta = self.beta
     self.cloth.set_vertices(self.shaped_cloth.vertices)
     self.cloth.normal = np.copy(self.cloth_base_normal)
     if body_flag:
         self.body.normal = np.copy(self.body_base_normal)
Exemplo n.º 5
0
def show_cloth_truth(disps, poses=None):
    global frame
    frame = 0

    if poses is not None and len(poses) != len(disps):
        print('length of inputs are different!')
        return

    display_mesh = Mesh(cloth_template)
    display_body = Mesh().from_vertices(smpl.verts, smpl.faces)

    def idle():
        global frame
        frame += 1
        frame %= len(disps)

        display_mesh.vertices = np.copy(cloth_template.vertices)
        display_mesh.vertices += disps[frame]
        display_mesh.update_normal_only()

        if poses is not None:
            smpl.set_params(poses[frame])
            display_body.from_vertices(smpl.verts, smpl.faces)

        glutPostRedisplay()

    def draw():
        msr.render()
        if poses is not None:
            bdy.render()

    msr = MeshRenderer(display_mesh, [0.6, 0.6, 0.6])
    bdy = MeshRenderer(display_body, [0.7, 0.7, 0.7])

    set_callbacks(idle)
    set_display(draw)
    run_glut()
Exemplo n.º 6
0
from lbac.train.pose_gt import PoseGroundTruth
from lbac.train.shape_gt import BetaGroundTruth
from com.mesh.mesh import Mesh
from com.mesh.array_renderer import *
from com.mesh.simple_display import *
from com.posture.smpl import SMPLModel
from com.path_helper import *

pose_gt = PoseGroundTruth(5, 0).load(conf_path('pose_gt'))
beta_gt = BetaGroundTruth().load(conf_path('beta_gt'))
smpl = SMPLModel(conf_path('smpl'))

cloth_template = Mesh(beta_gt.template)


def show_pose_gts(pose_gt=pose_gt):

    for si in pose_gt.data:
        disps, poses, beta = get_pose_gt(si)

        print(si, beta, len(poses))


def get_pose_gt(si):
    if type(si) == int:
        si = str(si)
    x = pose_gt.data[si]
    return x['disps'], x['poses'], x['beta']


def show_cloth_truth(disps, poses=None):
Exemplo n.º 7
0
class VirtualFitting:
    def __init__(self):

        self.beta_gt = BetaGroundTruth().load(conf_path('beta_gt'))
        self.cloth = Mesh(self.beta_gt.template)
        self.beta = np.zeros(10)
        self.pose = np.zeros((24, 3))
        self.trans = np.zeros(3)
        self.last_beta = np.zeros(10)
        self.shaped_cloth = None
        self.smpl = SMPLModel(conf_path('smpl'))
        vs, fs = self.smpl.set_params(self.pose, self.beta, self.trans)
        self.body = Mesh().from_vertices(vs, fs)
        # 暂存0状态normal
        self.cloth_base_normal = self.cloth.normal
        self.body_base_normal = self.body.normal
        self.canvas = [Canvas(), Canvas()]
        self.shape_regressor = ShapeRegressor(self.canvas[0], conf_path('mlp'))
        self.pose_regressor = PoseRegressor(self.canvas[1], conf_path('gru'),
                                            conf_value('gru_step'))
        self.reset_cloth()
        self.relation = ClosestVertex().load(conf_path('vert_rela'))
        self.cloth_weights = np.zeros((len(self.cloth.vertices), 24))
        rela = self.relation.get_rela()
        for i in range(len(self.cloth.vertices)):
            self.cloth_weights[i] = self.smpl.weights[rela[i]]

    def reset_cloth(self, body_flag=True):
        if self.shaped_cloth is None or (self.last_beta != self.beta).any():
            self.shaped_cloth = Mesh(self.beta_gt.template)
            self.shaped_cloth.vertices += self.shape_regressor.gen(self.beta)
            self.last_beta = self.beta
        self.cloth.set_vertices(self.shaped_cloth.vertices)
        self.cloth.normal = np.copy(self.cloth_base_normal)
        if body_flag:
            self.body.normal = np.copy(self.body_base_normal)

    def apply_pose(self):
        self.cloth.vertices = apply_pose(self.smpl, self.cloth_weights,
                                         self.cloth.vertices)

    def apply_normal(self, body_flag=True):
        self.cloth.normal = apply_pose(self.smpl, self.cloth_weights,
                                       self.cloth.normal)
        if body_flag:
            self.body.normal = apply_pose(self.smpl, self.smpl.weights,
                                          self.body.normal)

    def post_processing(self):
        cloth = self.cloth
        body = self.body
        rela = self.relation.calc_rela_once(cloth, body)

        def spread(ci, vec, levels=40):
            past = {}
            bias = 0.002

            # def get_vec(i, level):
            #     return vec * bias / (np.linalg.norm(cloth.vertices[i] - cloth.vertices[ci]) + bias)
            #     # return vec * level / tot_level

            def forward(i, level):
                if level == 0 or i in past:
                    return
                past[i] = 1
                cloth.vertices[i] += vec * bias / (
                    np.linalg.norm(cloth.vertices[i] - cloth.vertices[ci]) +
                    bias)
                for ni in cloth.edges[i]:
                    forward(ni, level - 1)

            forward(ci, levels)

        # to the close skin
        for i in range(len(cloth.vertices)):
            vc = cloth.vertices[i]
            vb = body.vertices[rela[i]]
            bn = body.normal[rela[i]]
            if np.dot(vb - vc, bn) > 0:
                spread(i, bn * np.dot(vb - vc, bn) * 0.5, 20)

        for r in range(2000):
            flag = True
            for i in range(len(cloth.vertices)):
                vc = cloth.vertices[i]
                vb = body.vertices[rela[i]]
                bn = body.normal[rela[i]]
                if np.dot(vb - vc, bn) > 0.004:
                    spread(i, bn * np.dot(vb - vc, bn) * 1, 10)
                    flag = False
            if flag:
                break

        cloth.update_normal_only()
        for i in range(len(cloth.vertices)):
            cn = cloth.normal[i]
            bn = body.normal[rela[i]]
            cloth.vertices[i] += cn * 0.004 + bn * 0.004
        self.cloth = cloth

    def post_processing_2(self):
        cloth = self.cloth
        body = self.body
        rela = self.relation.calc_rela_once(cloth, body)

        # to the close skin
        for i in range(len(cloth.vertices)):
            vc = cloth.vertices[i]
            vb = body.vertices[rela[i]]
            bn = body.normal[rela[i]]
            cloth.vertices[i] += bn * (np.dot(vb - vc, bn) + 0.01)

        # cloth.update_normal_only()
        # for i in range(len(cloth.vertices)):
        #     cn = cloth.normal[i]
        #     bn = body.normal[rela[i]]
        #     cloth.vertices[i] += cn * 0.004 + bn * 0.004

        self.cloth = cloth

    def update(self):
        timer = Timer(False)
        self.smpl.set_params(self.pose, self.beta, self.trans, True)
        self.body.set_vertices(self.smpl.verts)
        # self.body.update_normal_only()
        timer.tick('gen smpl body')
        self.reset_cloth()
        timer.tick('copy template cloth and apply beta displacement')
        self.cloth.vertices += self.pose_regressor.gen(self.beta, self.pose)
        timer.tick('apply pose displacement')
        # self.relation.reset()
        self.apply_pose()
        # self.apply_normal()
        timer.tick('apply pose')
        self.relation.update(self.cloth, self.body)
        self.post_processing()
        self.cloth.update_normal_only()
        timer.tick('postprocessing')

    def update_cloth_only(self):
        timer = Timer()
        self.smpl.set_params(self.pose, self.beta, self.trans, True, True)
        timer.tick('smpl')
        self.reset_cloth(False)
        timer.tick('reset')
        self.cloth.vertices += self.pose_regressor.gen(self.beta, self.pose)
        timer.tick('diff')
        self.apply_pose()
        timer.tick('pose')
        # self.apply_normal(False)
        timer.tick('normal')

    def recalculate_pose_displacement(self):
        self.smpl.set_params(self.pose, self.beta, self.trans, True, True)
        self.reset_cloth(False)
        self.cloth.vertices += self.pose_regressor.gen(self.beta, self.pose)

    def close(self):
        for canvas in self.canvas:
            canvas.close()
Exemplo n.º 8
0
        w = 1 - mu
        mesh.vertices[v] = mesh.vertices[v] * w + origin.vertices[v] * (1 - w)


def smooth_hcl(mesh, times):
    origin = Mesh(mesh)
    for i in range(times):
        taubin(mesh, 1, 1)
        push_back_vertex(origin, mesh, 0.2)
        taubin(mesh, -0.2, 1)
    return mesh


if __name__ == '__main__':
    # m = Mesh().load('../data/beta_simulation/result/1.obj')
    m = Mesh().load('../data/pose_simulation/tst/shape_y_final/5.obj')
    smooth_hcl(m, 50)
    # expand(m, 1.03)
    m.save('../tst/save_mesh.obj')


def smooth_bounds(mesh, times):
    def taubin(mesh, mu, level=1):
        new_vertices = []
        new_vertices.extend(mesh.vertices)
        for v in mesh.bounds:
            neighbors = [v]
            find_neighbors(mesh.edges, v, level, neighbors)
            s = 0
            ws = 0
            for n in neighbors: