3D头模加载

发布于:2024-05-05 ⋅ 阅读:(33) ⋅ 点赞:(0)

目录

mesh参数

psbody加载

psbody示例

trimesh加载

加载动画:

openmesh


mesh参数

5023个顶点

9976个面。

from psbody.mesh import Mesh

选择相机参数:
python
Copy code
if template_type == "flame":
    camera_params = {'c': np.array([400, 400]),
                     'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]),
                     'f': np.array([4754.97941935 / 2, 4754.97941935 / 2])}
elif template_type == "biwi":
    camera_params = {'c': np.array([400, 400]),
                     'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]),
                     'f': np.array([4754.97941935 / 8, 4754.97941935 / 8])}
这里根据 template_type 变量的值来设置相机参数:
camera_params: 是一个包含相机参数的字典。
'c': 表示相机的光心坐标(以像素为单位)。
'k': 表示镜头的畸变系数数组,其中可能包含径向和切向畸变参数。
'f': 表示焦距,以像素为单位。
设置视锥体:
python
Copy code
frustum = {'near': 0.01, 'far': 3.0, 'height': 800, 'width': 800}
frustum 是一个字典,用于定义相机的视锥体参数:
'near': 表示相机的近平面距离。
'far': 表示相机的远平面距离。
'height' 和 'width': 表示视锥体的高度和宽度。
Mesh 对象创建:
python
Copy code
mesh_copy = Mesh(mesh.v, mesh.f)
这行代码创建了一个 Mesh 对象,它是 mesh_copy 的副本:
Mesh: 是一个类,表示一个 3D 网格模型。
mesh.v: 传递给 Mesh 构造函数的第一个参数,表示网格的顶点。
mesh.f: 传递给 Mesh 构造函数的第二个参数,表示网格的面。
mesh_copy 是 mesh 对象的副本,可以在后续操作中对其进行修改或渲染。

psbody加载

codetalker

from psbody.mesh import Mesh

    if cfg.dataset == "BIWI":
        template_file = os.path.join(cfg.data_root, "BIWI.ply")
    elif cfg.dataset == "vocaset":
        template_file = os.path.join(cfg.data_root, "FLAME_sample.ply")
         
    print("rendering: ", test_name)
                 
    template = Mesh(filename=template_file)

psbody示例

from psbody.mesh import Mesh
from psbody.mesh.meshviewer import MeshViewer

template_file = r'BIWI/BIWI.ply'
template_file = r'E:\project\audio\audio2face\CodeTalker-main\vocaset\FLAME_sample.ply'
# template_file = r'E:\project\audio\audio2face\CodeTalker-main\BIWI\BIWI.ply'

target_mesh = Mesh(filename=template_file)
target_mesh.set_vertex_colors('white')
viewer = MeshViewer()
# viewer.set_background_color((1.0, 1.0, 1.0),blocking=False)

viewer.set_static_meshes([target_mesh], blocking=True)
# viewer.set_dynamic_meshes(target_mesh)
viewer.set_background_color((1.0, 1.0, 1.0))
viewer.show()

这个显示是黑屏,不能显示物体

trimesh加载

FaceDiffuser

import trimesh

if __name__ == '__main__':
    template_file=f"data/BIWI/templates/face_template.obj"
    ref_mesh = trimesh.load_mesh(template_file, process=False)

    scene = trimesh.scene.scene.Scene([ref_mesh])

    # 显示场景
    scene.show()

报错:

ImportError: `trimesh.viewer.windowed` requires `pip install "pyglet<2"`

解决方法:

pip install "pyglet<2"

加载动画:


import trimesh

import numpy as np

def load_mesh(file_path):
    """加载PLY文件并返回网格对象。"""
    return trimesh.load(file_path, process=False)

def apply_smile(mesh, intensity=0.5):
    """模拟微笑通过向上移动模型嘴角附近的顶点。"""
    # 假设嘴角的顶点索引已知
    mouth_corner_indices = [1500, 2300]  # 示例索引,需要根据实际模型调整

    movement = np.array([0, intensity, 0])  # 向上移动
    for idx in mouth_corner_indices:
        print(f"Vertex_o {idx}: {mesh.vertices[idx]}")  # 打印原始顶点位置
    # 更新嘴角顶点位置
    for idx in range(mouth_corner_indices[0],mouth_corner_indices[1]):
        mesh.vertices[idx] += movement
    for idx in mouth_corner_indices:
        print(f"Vertex {idx}: {mesh.vertices[idx]}")  # 打印更新后的顶点位置
def main():
    # 路径替换为你的PLY文件路径
    mesh = load_mesh(r'E:\BIWI.ply')

    # 应用微笑表情变换
    apply_smile(mesh, intensity=0.1)  # 强度根据模型尺度调整

    # 使用trimesh提供的简单pyglet窗口显示结果
    mesh.show()

if __name__ == "__main__":
    main()

openmesh

pip install openmesh

win11直接安装报错

从巴塞尔面模型 (BFM) 转换为 FLAME 头部模型

https://github.com/TimoBolkart/BFM_to_FLAME


网站公告

今日签到

点亮在社区的每一天
去签到