File size: 7,428 Bytes
fc2d6a1
c38ae1d
3158a8e
fc2d6a1
 
 
 
0eb82e2
fc2d6a1
c38ae1d
3158a8e
fc2d6a1
c38ae1d
fc2d6a1
d92e0b1
 
 
fc2d6a1
 
 
 
c38ae1d
 
94c8594
 
0eb82e2
 
c38ae1d
 
94c8594
fc2d6a1
 
0eb82e2
0bb48ab
d92e0b1
fc2d6a1
1aaf1b1
94c8594
d92e0b1
1aaf1b1
d92e0b1
0eb82e2
 
 
d92e0b1
1aaf1b1
 
c38ae1d
 
1aaf1b1
c38ae1d
fc2d6a1
1aaf1b1
 
d92e0b1
0eb82e2
b3b3c92
708d74e
1aaf1b1
c38ae1d
 
 
 
 
77706c8
 
c38ae1d
 
 
 
 
 
 
 
 
 
 
 
 
77706c8
59ffa13
9b38fee
c38ae1d
 
 
 
 
 
 
4817a50
c38ae1d
 
 
 
 
 
77706c8
c38ae1d
 
 
 
 
77706c8
c38ae1d
 
 
 
 
fdafb07
c38ae1d
 
 
 
708d74e
c38ae1d
 
 
 
 
 
 
 
 
 
 
 
 
708d74e
 
 
 
 
fdafb07
c38ae1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0eb82e2
4817a50
c38ae1d
 
3158a8e
c38ae1d
 
c0af06f
 
 
 
 
 
 
 
 
 
 
 
 
 
c38ae1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
708d74e
 
c38ae1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c191ddb
 
 
 
c38ae1d
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import gradio as gr
import time  # Для эмуляции времени загрузки
import tempfile
import numpy as np
import torch
from PIL import Image
from tsr.system import TSR
from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation

# Проверяем наличие GPU
device = "cuda:0" if torch.cuda.is_available() else "cpu"

# Загружаем модель
model = TSR.from_pretrained(
    "stabilityai/TripoSR",
    config_name="config.yaml",
    weight_name="model.ckpt",
)
model.renderer.set_chunk_size(131072)
model.to(device)


# Функция для проверки изображения
def check_input_image(input_image):
    if input_image is None:
        raise gr.Error("No image uploaded!")


# Функция обработки изображения
def preprocess(input_image, do_remove_background, foreground_ratio):
    def fill_background(image):
        image = np.array(image).astype(np.float32) / 255.0
        image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
        image = Image.fromarray((image * 255.0).astype(np.uint8))
        return image

    if do_remove_background:
        image = input_image.convert("RGB")
        image = remove_background(image)
        image = resize_foreground(image, foreground_ratio)
        image = fill_background(image)
    else:
        image = input_image
        if image.mode == "RGBA":
            image = fill_background(image)
    return image


# Функция генерации 3D модели
def generate(image):
    time.sleep(3)  # Эмуляция времени обработки
    scene_codes = model(image, device=device)
    mesh = model.extract_mesh(scene_codes)[0]
    mesh = to_gradio_3d_orientation(mesh)
    mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
    mesh.export(mesh_path2.name)
    return mesh_path2.name


def start_loading():
    return gr.HTML.update(visible=True)

def stop_loading():
    return gr.HTML.update(visible=False)


# Настройка темы и CSS
class CustomTheme(gr.themes.Base):
    def __init__(self):
        super().__init__()
        self.primary_hue = "#191a1e"
        self.background_fill_primary = "#191a1e"
        self.background_fill_secondary = "#191a1e"
        self.background_fill_tertiary = "#191a1e"
        self.text_color_primary = "#FFFFFF"
        self.text_color_secondary = "#FFFFFF"
        self.text_color_tertiary = "#FFFFFF"
        self.input_background_fill = "#191a1e"
        self.input_text_color = "#FFFFFF"


css = """
/* Скрываем нижний колонтитул */
footer {
    visibility: hidden;
    height: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
}
/* Применяем шрифты */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap');
body, input, button, textarea, select, .gr-button {
    font-family: 'Poppins', sans-serif;
    background-color: #191a1e !important;
    color: #FFFFFF;
}
/* Настройки заголовков */
h1, h2, h3, h4, h5, h6 {
    font-family: 'Poppins', sans-serif;
    font-weight: 700;
    color: #FFFFFF;
}
/* Стиль для текстовых полей и кнопок */
input[type="text"], textarea {
    background-color: #191a1e !important;
    color: #FFFFFF;
    border: 1px solid #FFFFFF;
}
/* Слайдер */
.gr-slider {
    height: 40px !important; /* Увеличение высоты слайдера */
    transition: all 0.3s ease !important; /* Плавные переходы */
}
.gr-slider .slider-value {
    display: none !important; /* Прячем текст значения */
}
.gr-slider .slider::-webkit-slider-thumb {
    height: 15px !important;
    width: 15px !important;
    background-color: #5271FF !important; /* Цвет совпадает с кнопкой */
    border-radius: 50%;
}
.gr-slider .slider::-webkit-slider-runnable-track {
    background: #5271FF !important; /* Цвет заполнения слайдера */
}
/* Кнопка Generate */
.generate-button {
    background-color: #5271FF !important;
    color: #FFFFFF !important;
    border: none;
    font-weight: bold;
}
.generate-button:hover {
    background-color: #405BBF !important; /* Цвет при наведении */
}
.generate-button {
    position: relative; /* Кнопка становится родительским контейнером */
}
/* Выделяем текст для Prompt */
.prompt-text {
    font-weight: bold;
    color: #FFFFFF;
}
/* Лоадер (анимация) */
#loading-bar {
    display: none; /* Скрыт по умолчанию */
    position: relative; /* Изменено на relative для использования в кнопке */
    margin: 0 auto; /* Центрируем внутри родительского элемента */
    width: 30px; /* Уменьшен размер для кнопки */
    height: 30px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #5271FF; /* Цвет лоадера */
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
/* Анимация вращения */
@keyframes spin {
    0% { transform: translate(-50%, -50%) rotate(0deg); }
    100% { transform: translate(-50%, -50%) rotate(360deg); }
}
/* Текст CheckBox в белый */
.gr-checkbox label {
    color: #FFFFFF !important;
}
"""

# Интерфейс
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
    with gr.Column():
        gr.Markdown("<span style='color: white; font-weight: bold;'>Upload and Process Your Image</span>")
        with gr.Row():
            input_image = gr.Image(
                label="Upload Image",
                image_mode="RGBA",
                sources="upload",
                type="pil",
                width=400,
                height=300,
            )
            processed_image = gr.Image(
                label="Processed Image",
                interactive=False,
                width=400,
                height=300,
            )
        foreground_ratio = gr.Slider(
            label="Foreground Ratio",
            minimum=0.5,
            maximum=1.0,
            value=0.85,
            step=0.05,
        )
        do_remove_background = gr.Checkbox(
            label="Remove Background",  # Текст перекрашен в белый через CSS
            value=True,
        )
        submit = gr.Button("Generate", elem_classes="generate-button")
        loading_bar = gr.HTML("<div id='loading-bar'></div>")
        output_model = gr.Model3D(
            label="Generated GLB Model",
            interactive=False,
            elem_classes="gr-model3d-container",
        )

    submit.click(
    fn=start_loading,  # Включить прогресс-бар
    inputs=[], 
    outputs=[loading_bar]
).then(
    fn=check_input_image,
    inputs=[input_image],
    outputs=[]
).then(
    fn=preprocess,
    inputs=[input_image, do_remove_background, foreground_ratio],
    outputs=[processed_image]
).then(
    fn=generate,
    inputs=[processed_image],
    outputs=[output_model]
).then(
    fn=stop_loading,  # Отключаем прогресс-бар после завершения генерации
    inputs=[],
    outputs=[loading_bar]
)
# Запуск приложения
demo.launch(
    server_name="0.0.0.0",
    server_port=7860,
    share=True,
)