File size: 3,974 Bytes
e7415d3
 
 
 
 
 
 
 
a6ce4c7
e7415d3
c1190c1
 
e7415d3
7118d20
e7415d3
 
 
8fc5ab1
e7415d3
 
8fc5ab1
 
e7415d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b515eec
 
e7415d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b515eec
e7415d3
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
# Direct3D-S2 - SHAPE ONLY (Uniboards, 2026-11-20). One photo -> mesh (GLB + STL). Docker Space, L40S (48 GB).
# Code: DreamTechAI/Direct3D-S2 @ a1cf235 (MIT); weights wushuang98/Direct3D-S2 direct3d-s2-v-1-1 (MIT).
# Background: BiRefNet (MIT) inside the pipeline when the photo has no transparency. Nothing else is downloaded.
import os
import datetime
import numpy as np
import torch
import trimesh
import gradio as gr
from PIL import Image
import sys
sys.path.insert(0, "/opt/d3s2")  # editable install from root is not visible to user 1000
from direct3d_s2.pipeline import Direct3DS2Pipeline

MAX_SEED = np.iinfo(np.int32).max
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")
os.makedirs(TMP_DIR, exist_ok=True)

pipe = Direct3DS2Pipeline.from_pretrained("wushuang98/Direct3D-S2", subfolder="direct3d-s2-v-1-1")
pipe.to("cuda:0")


@torch.no_grad()
def generate(image, seed, resolution, dense_steps, sparse_steps, guidance, target_faces):
    if image is None:
        raise gr.Error("Upload a photo first.")
    seed = int(seed) if int(seed) >= 0 else int(np.random.randint(0, MAX_SEED))
    gen = torch.Generator(device="cuda").manual_seed(seed)
    g = float(guidance)
    mesh = pipe(image, sdf_resolution=int(resolution), generator=gen, remesh=False, mc_threshold=0.2,
                dense_sampler_params={"num_inference_steps": int(dense_steps), "guidance_scale": g},
                sparse_512_sampler_params={"num_inference_steps": int(sparse_steps), "guidance_scale": g},
                sparse_1024_sampler_params={"num_inference_steps": max(5, int(sparse_steps) // 2), "guidance_scale": g})["mesh"]
    if not isinstance(mesh, trimesh.Trimesh):
        mesh = trimesh.Trimesh(vertices=np.asarray(mesh.vertices), faces=np.asarray(mesh.faces))
    faces_before = len(mesh.faces)
    if target_faces and int(target_faces) > 0 and faces_before > int(target_faces):
        mesh = mesh.simplify_quadric_decimation(face_count=int(target_faces))
    stamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    folder = os.path.join(TMP_DIR, stamp)
    os.makedirs(folder, exist_ok=True)
    glb, stl = os.path.join(folder, f"direct3ds2_{stamp}.glb"), os.path.join(folder, f"direct3ds2_{stamp}.stl")
    mesh.export(glb)
    mesh.export(stl)
    torch.cuda.empty_cache()
    peak = torch.cuda.max_memory_allocated() / 2**30
    info = (f"seed {seed}; resolution {resolution}; faces {faces_before:,} -> {len(mesh.faces):,}; "
            f"watertight {mesh.is_watertight}; GPU peak {peak:.1f} GB")
    return glb, glb, stl, info


with gr.Blocks(title="Direct3D-S2 shape only") as demo:
    gr.Markdown("# Direct3D-S2 - shape only\nOne photo (one subject) -> 3D mesh. Download GLB or STL.")
    with gr.Row():
        with gr.Column():
            image_in = gr.Image(label="Photo", type="pil", image_mode="RGBA")
            with gr.Accordion("Settings", open=False):
                seed = gr.Number(label="Seed (-1 = random)", value=0, precision=0)
                resolution = gr.Radio(["1024", "512"], value="1024", label="Resolution (1024 = full detail)")
                dense_steps = gr.Slider(10, 100, value=50, step=1, label="Stage 1 steps")
                sparse_steps = gr.Slider(10, 60, value=30, step=1, label="Stage 2 steps (stage 3 = half)")
                guidance = gr.Slider(1.0, 12.0, value=7.0, step=0.1, label="Guidance")
                target_faces = gr.Slider(0, 3000000, value=1000000, step=50000, label="Target faces (0 = keep all)")
            go = gr.Button("Generate", variant="primary")
        with gr.Column():
            model_out = gr.Model3D(label="Mesh")
            glb_out = gr.File(label="Download GLB")
            stl_out = gr.File(label="Download STL")
            info_out = gr.Textbox(label="Info")
    go.click(generate, [image_in, seed, resolution, dense_steps, sparse_steps, guidance, target_faces],
             [model_out, glb_out, stl_out, info_out])

demo.queue(max_size=4).launch()