File size: 7,197 Bytes
ce4c1d3
 
e283fdf
 
 
ce4c1d3
e283fdf
 
ce4c1d3
 
 
 
 
 
 
 
 
 
e283fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce4c1d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82f2717
ce4c1d3
 
 
 
 
 
 
 
 
e283fdf
 
 
 
 
 
 
 
ce4c1d3
 
 
 
 
 
 
 
 
 
 
e283fdf
 
ce4c1d3
 
 
 
 
 
 
 
 
e283fdf
ce4c1d3
 
 
e283fdf
 
 
 
 
 
 
9d36776
 
 
 
e283fdf
9d36776
82f2717
e283fdf
82f2717
 
 
 
 
 
 
 
 
e283fdf
 
 
 
 
 
 
 
 
 
 
9d36776
 
ce4c1d3
 
 
 
 
 
9d36776
 
ce4c1d3
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python

import os
import shutil
import tempfile
import gradio as gr
from PIL import Image
import numpy as np

from settings import (
    DEFAULT_IMAGE_RESOLUTION,
    DEFAULT_NUM_IMAGES,
    MAX_IMAGE_RESOLUTION,
    MAX_NUM_IMAGES,
    MAX_SEED,
)
from utils import randomize_seed_fn

# ---- helper to build a quick textured copy of the mesh ---------------
def apply_texture(src_mesh:str, texture:str, tag:str)->str:
    """
    Writes a copy of `src_mesh` and tiny .mtl that points to `texture`.
    Returns the new OBJ/GLB path for viewing.
    """
    tmp_dir = tempfile.mkdtemp()
    mesh_copy = os.path.join(tmp_dir, f"{tag}.obj")
    mtl_name  = f"{tag}.mtl"

    # copy geometry
    shutil.copy(src_mesh, mesh_copy)

    # write minimal MTL
    with open(os.path.join(tmp_dir, mtl_name), "w") as f:
        f.write(f"newmtl material_0\nmap_Kd {os.path.basename(texture)}\n")

    # ensure texture lives next to OBJ
    shutil.copy(texture, os.path.join(tmp_dir, os.path.basename(texture)))

    # patch OBJ to reference our new MTL
    with open(mesh_copy, "r+") as f:
        lines = f.readlines()
        if not lines[0].startswith("mtllib"):
            lines.insert(0, f"mtllib {mtl_name}\n")
            f.seek(0); f.writelines(lines)

    return mesh_copy

def image_to_temp_path(img_like, tag):
    """
    Convert various image-like objects (str, PIL.Image, list, tuple) to temp PNG path.
    Returns the path to the saved image file.
    """
    # Handle tuple or list input
    if isinstance(img_like, (list, tuple)):
        if len(img_like) == 0:
            raise ValueError("Empty image list/tuple.")
        img_like = img_like[0]

    # If it's already a file path
    if isinstance(img_like, str):
        return img_like

    # If it's a PIL Image
    if isinstance(img_like, Image.Image):
        temp_path = os.path.join(tempfile.mkdtemp(), f"{tag}.png")
        img_like.save(temp_path)
        return temp_path

    # if it's numpy array
    if isinstance(img_like, np.ndarray):
        temp_path = os.path.join(tempfile.mkdtemp(), f"{tag}.png")
        img_like = Image.fromarray(img_like)
        img_like.save(temp_path)
        return temp_path

    raise ValueError(f"Expected PIL.Image, str, list, or tuple — got {type(img_like)}")

def show_mesh(which, mesh, inp, coarse, fine):
    """Switch the displayed texture based on dropdown change."""
    print()
    tex_map = {
        "Input": image_to_temp_path(inp, "input"),
        "Coarse": coarse[0] if isinstance(coarse, tuple) else coarse,
        "Fine": fine[0] if isinstance(fine, tuple) else fine,
    }
    texture_path = tex_map[which]
    return apply_texture(mesh, texture_path, which.lower())
# ----------------------------------------------------------------------


def create_demo(process):
    with gr.Blocks() as demo:
        with gr.Row():
            with gr.Column():
                image = gr.Image()
                prompt = gr.Textbox(label="Prompt", submit_btn=True)
                with gr.Accordion("Advanced options", open=False):
                    num_samples = gr.Slider(
                        label="Number of images", minimum=1, maximum=MAX_NUM_IMAGES, value=DEFAULT_NUM_IMAGES, step=1
                    )
                    image_resolution = gr.Slider(
                        label="Image resolution",
                        minimum=256,
                        maximum=MAX_IMAGE_RESOLUTION,
                        value=DEFAULT_IMAGE_RESOLUTION,
                        step=256,
                    )
                    num_steps = gr.Slider(label="Number of steps", minimum=1, maximum=100, value=10, step=1)
                    guidance_scale = gr.Slider(label="Guidance scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
                    seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
                    randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
                    a_prompt = gr.Textbox(label="Additional prompt", value="best quality, extremely detailed")
                    n_prompt = gr.Textbox(
                        label="Negative prompt",
                        value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
                    )
            with gr.Column():
                result_coarse = gr.Gallery(label="Output Coarse", show_label=True, columns=2, object_fit="scale-down")
                result_fine = gr.Gallery(label="Output Fine", show_label=True, columns=2, object_fit="scale-down")
                # mesh_viewer = gr.Model3D(label="Textured Mesh", clear_color=[0, 0, 0, 0], value="examples/monkey/mesh.obj")

        # radio buttons let the user toggle which texture to view
        # texture_choice = gr.Radio(["Input", "Coarse", "Fine"], label="Preview texture", value="Input")
        # mesh_path_state = gr.State("examples/bunny/mesh.obj")

        inputs = [
            image,
            prompt,
            a_prompt,
            n_prompt,
            num_samples,
            image_resolution,
            num_steps,
            guidance_scale,
            seed,
        ]
        
        # first call → run diffusion / texture network
        prompt.submit(
            fn=randomize_seed_fn,
            inputs=[seed, randomize_seed],
            outputs=seed,
            queue=False,
            api_name=False,
        ).then(
            fn=process,
            inputs=inputs,
            outputs=[result_coarse, result_fine],
            api_name="canny",
            concurrency_id="main",
        )
        # .then(
        #     fn=show_mesh,
        #     inputs=[texture_choice, mesh_path_state, image, result_coarse, result_fine],
        #     outputs=mesh_viewer,
        #     queue=False,
        #     api_name=False,
        # )

        gr.Examples(
            fn=process,
            inputs=inputs,
            outputs=[result_coarse, result_fine],
            examples=[
                [
                    "examples/bunny/uv_normal.png", # /dgxusers/Users/jyang/project/ObjectReal/data/control/preprocess/bunny/uv_normal/fused.png
                    "feather", 
                    a_prompt.value, 
                    n_prompt.value,
                    num_samples.value,
                    image_resolution.value,
                    num_steps.value,
                    guidance_scale.value,
                    seed.value,
                ],
                [
                    "examples/monkey/uv_normal.png", # /dgxusers/Users/jyang/project/ObjectReal/data/control/preprocess/monkey/uv_normal/fused.png
                    "wood", 
                    a_prompt.value, 
                    n_prompt.value,
                    num_samples.value,
                    image_resolution.value,
                    num_steps.value,
                    guidance_scale.value,
                    seed.value,
                ],
            ],
        )
    return demo


if __name__ == "__main__":
    from model import Model

    model = Model(task_name="Texnet")
    demo = create_demo(model.process_texnet)
    demo.queue().launch()