如何使用 3D 模型组件

相关空间: https://huggingface.co/spaces/dawood/Model3D,https : //huggingface.co/spaces/radames/PIFu-Clothed-Human-Digitization,https : //huggingface.co/spaces/radames/dpt -depth-estimation-3d-obj标签:VISION, IMAGE

介绍

3D 模型在机器学习中越来越受欢迎,并制作了一些最有趣的演示来进行实验。 使用 gradio ,你可以轻松构建 3D 图像模型的演示并与任何人共享。 Gradio 3D 模型组件接受 3 种文件类型,包括: .obj.glb和 & .gltf

3D models are becoming more popular in machine learning and make for some of the most fun demos to experiment with. Using gradio, you can easily build a demo of your 3D image model and share it with anyone. The Gradio 3D Model component accepts 3 file types including: .obj, .glb, & .gltf.

本指南将向你展示如何通过几行代码为你的 3D 图像模型构建演示; 就像下面的那个。 通过点击、拖动和缩放来玩转 3D 对象:

This guide will show you how to build a demo for your 3D image model in a few lines of code; like the one below. Play around with 3D object by clicking around, dragging and zooming:

先决条件

Prerequisites

确保你已经安装了gradio Python 包。

Make sure you have the gradio Python package already installed.

查看代码

让我们看看如何创建上面的最小界面。 本例中的预测函数将只返回原始 3D 模型网格,但你可以更改此函数以在你的机器学习模型上运行推理。 我们将在下面查看更复杂的示例。

Let's take a look at how to create the minimal interface above. The prediction function in this case will just return the original 3D model mesh, but you can change this function to run inference on your machine learning model. We'll take a look at more complex examples below.

import gradio as gr

def load_mesh(mesh_file_name):
    return mesh_file_name

demo = gr.Interface(
    fn=load_mesh,
    inputs=gr.Model3D(),
    outputs=gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0],  label="3D Model"),
    examples=[
        ["files/Bunny.obj"],
        ["files/Duck.glb"],
        ["files/Fox.gltf"],
        ["files/face.obj"],
    ],
    cache_examples=True,
)

demo.launch()

让我们分解上面的代码:

Let's break down the code above:

load_mesh :这是我们的“预测”函数,为简单起见,此函数将接收 3D 模型网格并将其返回。

load_mesh: This is our 'prediction' function and for simplicity, this function will take in the 3D model mesh and return it.

创建界面:

Creating the Interface:

  • fn :用户点击提交时使用的预测函数。 在我们的例子中,这是 load_mesh 函数。

    fn: the prediction function that is used when the user clicks submit. In our case this is the load_mesh function.

  • inputs : 创建一个 model3D 输入组件。 输入需要上传的文件作为 {str} 文件路径。

    inputs: create a model3D input component. The input expects an uploaded file as a {str} filepath.

  • outputs :创建一个 model3D 输出组件。 输出组件还需要一个文件作为 {str} 文件路径。

    outputs: create a model3D output component. The output component also expects a file as a {str} filepath.

    • clear_color: this is the background color of the 3D model canvas. Expects RGBa values.

    clear_color :这是 3D 模型画布的背景色。 期望 RGBa 值。

    • label: the label that appears on the top left of the component.

    label :显示在组件左上角的标签。

  • examples :3D 模型文件列表。 3D 模型组件可以接受.obj.glb.gltf文件类型。

    examples: list of 3D model files. The 3D model component can accept .obj, .glb, & .gltf file types.

  • cache_examples :保存示例的预测输出,以节省推理时间。

    cache_examples: saves the predicted output for the examples, to save time on inference.

探索模式复杂的 Model3D 演示:

下面是一个使用 DPT 模型预测图像深度然后使用 3D 点云创建 3D 对象的演示。 查看app.py文件以了解代码和模型预测函数。

Below is a demo that uses the DPT model to predict the depth of an image and then uses 3D Point Cloud to create a 3D object. Take a look at the app.py file for a peek into the code and the model prediction function.

下面是一个使用 PIFu 模型将穿着衣服的人的图像转换为 3D 数字化模型的演示。 查看spaces.py文件以了解代码和模型预测函数。

Below is a demo that uses the PIFu model to convert an image of a clothed human into a 3D digitized model. Take a look at the spaces.py file for a peek into the code and the model prediction function.


你完成了! 这就是为 Model3D 模型构建界面所需的所有代码。 以下是你可能会觉得有用的一些参考资料:

And you're done! That's all the code you need to build an interface for your Model3D model. Here are some references that you may find useful: