Пример #1
0
def gen_mesh(res,
             net,
             cuda,
             data,
             save_path,
             thresh=0.5,
             use_octree=True,
             components=False):
    image_tensor_global = data['img_512'].to(device=cuda)
    image_tensor = data['img'].to(device=cuda)
    calib_tensor = data['calib'].to(device=cuda)

    net.filter_global(image_tensor_global)
    net.filter_local(image_tensor[:, None])

    try:
        if net.netG.netF is not None:
            image_tensor_global = torch.cat(
                [image_tensor_global, net.netG.nmlF], 0)
        if net.netG.netB is not None:
            image_tensor_global = torch.cat(
                [image_tensor_global, net.netG.nmlB], 0)
    except:
        pass

    b_min = data['b_min']
    b_max = data['b_max']
    try:
        save_img_path = save_path[:-4] + '.png'
        save_img_list = []
        for v in range(image_tensor_global.shape[0]):
            save_img = (
                np.transpose(image_tensor_global[v].detach().cpu().numpy(),
                             (1, 2, 0)) * 0.5 + 0.5)[:, :, ::-1] * 255.0
            save_img_list.append(save_img)
        save_img = np.concatenate(save_img_list, axis=1)
        cv2.imwrite(save_img_path, save_img)

        verts, faces, _, _ = reconstruction(net,
                                            cuda,
                                            calib_tensor,
                                            res,
                                            b_min,
                                            b_max,
                                            thresh,
                                            use_octree=use_octree,
                                            num_samples=50000)
        verts_tensor = torch.from_numpy(
            verts.T).unsqueeze(0).to(device=cuda).float()
        # if 'calib_world' in data:
        #     calib_world = data['calib_world'].numpy()[0]
        #     verts = np.matmul(np.concatenate([verts, np.ones_like(verts[:,:1])],1), inv(calib_world).T)[:,:3]

        #print(calib_tensor)
        color = np.zeros(verts.shape)
        interval = 50000
        for i in range(len(color) // interval + 1):
            left = i * interval
            if i == len(color) // interval:
                right = -1
            else:
                right = (i + 1) * interval
            net.calc_normal(verts_tensor[:, None, :, left:right],
                            calib_tensor[:, None], calib_tensor)
            nml = net.nmls.detach().cpu().numpy()[0] * 0.5 + 0.5
            color[left:right] = nml.T

        if 'calib_world' in data:
            calib_world = data['calib_world'].numpy()[0]
            verts = np.matmul(
                np.concatenate([verts, np.ones_like(verts[:, :1])], 1),
                inv(calib_world).T)[:, :3]

        save_obj_mesh_with_color(save_path, verts, faces, color)
    except Exception as e:
        print(e)
Пример #2
0
def gen_mesh_imgColor(res,
                      net,
                      cuda,
                      data,
                      save_path,
                      thresh=0.5,
                      use_octree=True,
                      components=False):
    image_tensor_global = data['img_512'].to(device=cuda)
    image_tensor = data['img'].to(device=cuda)
    calib_tensor = data['calib'].to(device=cuda)

    net.filter_global(image_tensor_global)
    net.filter_local(image_tensor[:, None])

    try:
        if net.netG.netF is not None:
            image_tensor_global = torch.cat(
                [image_tensor_global, net.netG.nmlF], 0)
        if net.netG.netB is not None:
            image_tensor_global = torch.cat(
                [image_tensor_global, net.netG.nmlB], 0)
    except:
        pass

    b_min = data['b_min']
    b_max = data['b_max']
    try:
        save_img_path = save_path[:-4] + '.png'
        save_img_list = []
        for v in range(image_tensor_global.shape[0]):
            save_img = (
                np.transpose(image_tensor_global[v].detach().cpu().numpy(),
                             (1, 2, 0)) * 0.5 + 0.5)[:, :, ::-1] * 255.0
            save_img_list.append(save_img)
        save_img = np.concatenate(save_img_list, axis=1)
        cv2.imwrite(save_img_path, save_img)

        verts, faces, _, _ = reconstruction(net,
                                            cuda,
                                            calib_tensor,
                                            res,
                                            b_min,
                                            b_max,
                                            thresh,
                                            use_octree=use_octree,
                                            num_samples=100000)
        verts_tensor = torch.from_numpy(
            verts.T).unsqueeze(0).to(device=cuda).float()

        # if this returns error, projection must be defined somewhere else
        xyz_tensor = net.projection(verts_tensor, calib_tensor[:1])
        uv = xyz_tensor[:, :2, :]
        print('uv', uv)
        print('xyz_tensor', xyz_tensor)
        print('verts', verts)

        color = index(image_tensor[:1], uv).detach().cpu().numpy()[0].T
        color = color * 0.5 + 0.5

        if 'calib_world' in data:
            calib_world = data['calib_world'].numpy()[0]
            verts = np.matmul(
                np.concatenate([verts, np.ones_like(verts[:, :1])], 1),
                inv(calib_world).T)[:, :3]

        save_obj_mesh_with_color(save_path, verts, faces, color)

    except Exception as e:
        print(e)