Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,16 @@ import logging
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import time
|
|
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
import rembg
|
8 |
import torch
|
9 |
from PIL import Image
|
10 |
from functools import partial
|
|
|
11 |
from tsr.system import TSR
|
12 |
-
from tsr.utils
|
13 |
|
14 |
# HF_TOKEN = os.getenv("HF_TOKEN")
|
15 |
|
@@ -22,77 +24,86 @@ else:
|
|
22 |
device = "cpu"
|
23 |
|
24 |
d = os.environ.get("DEVICE", None)
|
25 |
-
if d
|
26 |
-
device = d
|
27 |
|
28 |
model = TSR.from_pretrained(
|
29 |
"stabilityai/TripoSR",
|
30 |
-
config_name="config",
|
31 |
weight_name="model.ckpt",
|
32 |
# token=HF_TOKEN
|
33 |
)
|
34 |
model.renderer.set_chunk_size(131072)
|
35 |
-
|
36 |
model.to(device)
|
37 |
|
38 |
-
rembg_session = rembg.
|
|
|
39 |
|
40 |
def check_input_image(input_image):
|
41 |
if input_image is None:
|
42 |
-
raise
|
|
|
43 |
|
44 |
def preprocess(input_image, do_remove_background, foreground_ratio):
|
45 |
def fill_background(image):
|
46 |
image = np.array(image).astype(np.float32) / 255.0
|
47 |
-
image = image[:, :, :3] * image[:, :, 3:4] +
|
48 |
-
image = Image.fromarray((image * 255.0).astype(np.uint8)
|
|
|
49 |
|
50 |
if do_remove_background:
|
51 |
image = input_image.convert("RGB")
|
52 |
image = remove_background(image, rembg_session)
|
53 |
image = resize_foreground(image, foreground_ratio)
|
54 |
image = fill_background(image)
|
55 |
-
|
|
|
|
|
|
|
56 |
return image
|
57 |
|
|
|
58 |
def generate(image):
|
59 |
scene_codes = model(image, device=device)
|
60 |
mesh = model.extract_mesh(scene_codes)[0]
|
61 |
mesh = to_gradio_3d_orientation(mesh)
|
62 |
-
mesh_path = tempfile.NamedTemporaryFile(suffix=".obj", delete)
|
63 |
mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
|
64 |
-
|
65 |
-
|
66 |
return mesh_path.name, mesh_path2.name
|
67 |
|
68 |
-
def run_example(image_pил):
|
69 |
-
preprocessed = preprocess(image_pил, False, 0.9)
|
70 |
-
mesh_name, mes_path2.name = generate(image_pил)
|
71 |
|
72 |
-
|
|
|
|
|
|
|
73 |
|
74 |
-
|
|
|
75 |
def __init__(self):
|
76 |
-
|
|
|
77 |
self.background_fill_primary = "#191a1e"
|
78 |
self.background_fill_secondary = "#191a1e"
|
79 |
self.background_fill_tertiary = "#191a1e"
|
80 |
self.text_color_primary = "#FFFFFF"
|
81 |
self.text_color_secondary = "#FFFFFF"
|
82 |
-
self.
|
83 |
self.input_background_fill = "#191a1e"
|
84 |
self.input_text_color = "#FFFFFF"
|
85 |
|
|
|
86 |
css = """
|
87 |
-
/* Скрываем
|
88 |
footer {
|
89 |
-
|
90 |
height: 0;
|
91 |
margin: 0;
|
92 |
padding: 0;
|
93 |
overflow: hidden;
|
94 |
}
|
95 |
-
@import url('https://fonts.googleapis.com/css2?
|
96 |
/* Применяем шрифты */
|
97 |
body, input, button, textarea, select,.gr-button {
|
98 |
font-family: 'Poppins', sans-serif;
|
@@ -105,13 +116,12 @@ h1, h2, h3, h4, h5, h6 {
|
|
105 |
font-weight: 700;
|
106 |
color: #FFFFFF;
|
107 |
}
|
108 |
-
/* Стиль для текстовых
|
109 |
input[type="text"], textarea {
|
110 |
-
background-color: #
|
111 |
color: #FFFFFF;
|
112 |
border: 1px solid #FFFFFF;
|
113 |
}
|
114 |
-
|
115 |
/* Цвет кнопки Generate */
|
116 |
.generate-button {
|
117 |
background-color: #5271FF!important;
|
@@ -121,8 +131,7 @@ input[type="text"], textarea {
|
|
121 |
}
|
122 |
.generate-button:hover {
|
123 |
background-color: #405BBF!important; /* Цвет при наведении */
|
124 |
-
|
125 |
-
|
126 |
.drop-image-container {
|
127 |
display: flex;
|
128 |
flex-direction: column;
|
@@ -140,75 +149,80 @@ input[type="text"], textarea {
|
|
140 |
margin-left: auto;
|
141 |
margin-right: auto;
|
142 |
}
|
|
|
143 |
|
144 |
-
with
|
145 |
-
with gr.Column(elem_id="
|
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 |
demo.queue(max_size=10)
|
214 |
-
demo.launch()
|
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import time
|
5 |
+
|
6 |
import gradio as gr
|
7 |
import numpy as np
|
8 |
import rembg
|
9 |
import torch
|
10 |
from PIL import Image
|
11 |
from functools import partial
|
12 |
+
|
13 |
from tsr.system import TSR
|
14 |
+
from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
|
15 |
|
16 |
# HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
|
|
|
24 |
device = "cpu"
|
25 |
|
26 |
d = os.environ.get("DEVICE", None)
|
27 |
+
if d!= None:
|
28 |
+
device = d
|
29 |
|
30 |
model = TSR.from_pretrained(
|
31 |
"stabilityai/TripoSR",
|
32 |
+
config_name="config.yaml",
|
33 |
weight_name="model.ckpt",
|
34 |
# token=HF_TOKEN
|
35 |
)
|
36 |
model.renderer.set_chunk_size(131072)
|
|
|
37 |
model.to(device)
|
38 |
|
39 |
+
rembg_session = rembg.new_session()
|
40 |
+
|
41 |
|
42 |
def check_input_image(input_image):
|
43 |
if input_image is None:
|
44 |
+
raise gr.Error("No image uploaded!")
|
45 |
+
|
46 |
|
47 |
def preprocess(input_image, do_remove_background, foreground_ratio):
|
48 |
def fill_background(image):
|
49 |
image = np.array(image).astype(np.float32) / 255.0
|
50 |
+
image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
|
51 |
+
image = Image.fromarray((image * 255.0).astype(np.uint8))
|
52 |
+
return image
|
53 |
|
54 |
if do_remove_background:
|
55 |
image = input_image.convert("RGB")
|
56 |
image = remove_background(image, rembg_session)
|
57 |
image = resize_foreground(image, foreground_ratio)
|
58 |
image = fill_background(image)
|
59 |
+
else:
|
60 |
+
image = input_image
|
61 |
+
if image.mode == "RGBA":
|
62 |
+
image = fill_background(image)
|
63 |
return image
|
64 |
|
65 |
+
|
66 |
def generate(image):
|
67 |
scene_codes = model(image, device=device)
|
68 |
mesh = model.extract_mesh(scene_codes)[0]
|
69 |
mesh = to_gradio_3d_orientation(mesh)
|
70 |
+
mesh_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False)
|
71 |
mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
|
72 |
+
mesh.export(mesh_path.name)
|
73 |
+
mesh.export(mesh_path2.name)
|
74 |
return mesh_path.name, mesh_path2.name
|
75 |
|
|
|
|
|
|
|
76 |
|
77 |
+
def run_example(image_pil):
|
78 |
+
preprocessed = preprocess(image_pil, False, 0.9)
|
79 |
+
mesh_name, mesn_name2 = generate(preprocessed)
|
80 |
+
return preprocessed, mesh_name, mesn_name2
|
81 |
|
82 |
+
|
83 |
+
class CustomTheme(gr.themes.Base):
|
84 |
def __init__(self):
|
85 |
+
super().__init__()
|
86 |
+
self.primary_hue = "#191a1e"
|
87 |
self.background_fill_primary = "#191a1e"
|
88 |
self.background_fill_secondary = "#191a1e"
|
89 |
self.background_fill_tertiary = "#191a1e"
|
90 |
self.text_color_primary = "#FFFFFF"
|
91 |
self.text_color_secondary = "#FFFFFF"
|
92 |
+
self.text_color_tertiary = "#FFFFFF"
|
93 |
self.input_background_fill = "#191a1e"
|
94 |
self.input_text_color = "#FFFFFF"
|
95 |
|
96 |
+
|
97 |
css = """
|
98 |
+
/* Скрываем нижний колонтитул */
|
99 |
footer {
|
100 |
+
visibility: hidden;
|
101 |
height: 0;
|
102 |
margin: 0;
|
103 |
padding: 0;
|
104 |
overflow: hidden;
|
105 |
}
|
106 |
+
@import url('https://fonts.googleapis.com/css2?familyPoppins:wght@400;500;700&display=swap');
|
107 |
/* Применяем шрифты */
|
108 |
body, input, button, textarea, select,.gr-button {
|
109 |
font-family: 'Poppins', sans-serif;
|
|
|
116 |
font-weight: 700;
|
117 |
color: #FFFFFF;
|
118 |
}
|
119 |
+
/* Стиль для текстовых полей и кнопок */
|
120 |
input[type="text"], textarea {
|
121 |
+
background-color: #191a1e!important;
|
122 |
color: #FFFFFF;
|
123 |
border: 1px solid #FFFFFF;
|
124 |
}
|
|
|
125 |
/* Цвет кнопки Generate */
|
126 |
.generate-button {
|
127 |
background-color: #5271FF!important;
|
|
|
131 |
}
|
132 |
.generate-button:hover {
|
133 |
background-color: #405BBF!important; /* Цвет при наведении */
|
134 |
+
/* Выравнивание элементов */
|
|
|
135 |
.drop-image-container {
|
136 |
display: flex;
|
137 |
flex-direction: column;
|
|
|
149 |
margin-left: auto;
|
150 |
margin-right: auto;
|
151 |
}
|
152 |
+
"""
|
153 |
|
154 |
+
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
155 |
+
with gr.Column(elem_id="main-container"):
|
156 |
+
# Заголовок
|
157 |
+
gr.Markdown("**Upload and Process Your Image**", elem_classes="prompt-text")
|
158 |
+
|
159 |
+
# Блок загрузки изображения и настроек
|
160 |
+
with gr.Row(elem_id="upload-and-settings"):
|
161 |
+
# Левый блок с загрузкой изображения
|
162 |
+
with gr.Column(scale=3, elem_id="image-upload-container"):
|
163 |
+
input_image = gr.Image(
|
164 |
+
label="Upload Image",
|
165 |
+
image_mode="RGBA",
|
166 |
+
sources="upload",
|
167 |
+
type="pil",
|
168 |
+
elem_id="content_image",
|
169 |
+
width=500, # Увеличена ширина входного изображения
|
170 |
+
)
|
171 |
+
# Правый блок с настройками
|
172 |
+
with gr.Column(scale=2, elem_id="settings-container"):
|
173 |
+
do_remove_background = gr.Checkbox(
|
174 |
+
label="Remove Background",
|
175 |
+
value=True,
|
176 |
+
)
|
177 |
+
foreground_ratio = gr.Slider(
|
178 |
+
label="Foreground Ratio",
|
179 |
+
minimum=0.5,
|
180 |
+
maximum=1.0,
|
181 |
+
value=0.85,
|
182 |
+
step=0.05,
|
183 |
+
)
|
184 |
+
|
185 |
+
# Блок с обработанным изображением
|
186 |
+
with gr.Row(elem_id="processed-image-container"):
|
187 |
+
processed_image = gr.Image(
|
188 |
+
label="Processed Image",
|
189 |
+
interactive=False,
|
190 |
+
elem_id="processed_image",
|
191 |
+
)
|
192 |
+
|
193 |
+
# Кнопка генерации
|
194 |
+
with gr.Row(elem_id="generate-button-container"):
|
195 |
+
submit = gr.Button(
|
196 |
+
"Generate",
|
197 |
+
scale=0,
|
198 |
+
variant="primary",
|
199 |
+
elem_classes="generate-button",
|
200 |
+
)
|
201 |
+
|
202 |
+
# Результаты генерации
|
203 |
+
with gr.Row(elem_id="output-model-container"):
|
204 |
+
with gr.Tab("OBJ Model"):
|
205 |
+
output_model = gr.Model3D(
|
206 |
+
label="Generated OBJ Model",
|
207 |
+
interactive=False,
|
208 |
+
)
|
209 |
+
with gr.Tab("GLB Model"):
|
210 |
+
output_model2 = gr.Model3D(
|
211 |
+
label="Generated GLB Model",
|
212 |
+
interactive=False,
|
213 |
+
)
|
214 |
+
|
215 |
+
# Привязка событий
|
216 |
+
submit.click(fn=check_input_image, inputs=[input_image]).success(
|
217 |
+
fn=preprocess,
|
218 |
+
inputs=[input_image, do_remove_background, foreground_ratio],
|
219 |
+
outputs=[processed_image],
|
220 |
+
).success(
|
221 |
+
fn=generate,
|
222 |
+
inputs=[processed_image],
|
223 |
+
outputs=[output_model, output_model2],
|
224 |
+
)
|
225 |
|
226 |
+
# Запуск приложения
|
227 |
demo.queue(max_size=10)
|
228 |
+
demo.launch()
|