Zaiiida commited on
Commit
1aaf1b1
1 Parent(s): fb59e95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -172
app.py CHANGED
@@ -13,87 +13,75 @@ from functools import partial
13
  from tsr.system import TSR
14
  from tsr.utils import remove_background, resize_foreground, to_gradio_3d_orientation
15
 
16
- #...
17
 
18
- css = """
19
- /*... */
20
- """
 
21
 
22
- with gr.Blocks(theme=CustomTheme(), css=css) as demo:
23
- #...
 
24
 
25
- with gr.Column():
26
- gr.Markdown("**Input Image**")
27
- input_image = gr.Image(
28
- label="",
29
- image_mode="RGBA",
30
- sources="upload",
31
- type="pil",
32
- elem_id="content_image",
33
- width=500, # Увеличена ширина входных изображений
34
- )
35
- with gr.Column():
36
- do_remove_background = gr.Checkbox(
37
- label="Remove Background",
38
- value=True,
39
- elem_id="remove_background",
40
- )
41
- foreground_ratio = gr.Slider(
42
- label="Foreground Ratio",
43
- minimum=0.5,
44
- maximum=1.0,
45
- value=0.85,
46
- step=0.05,
47
- elem_id="foreground_ratio",
48
- )
49
- with gr.Row():
50
- submit = gr.Button(
51
- label="Generate",
52
- scale=0,
53
- variant="primary",
54
- elem_classes="generate-button",
55
- )
56
- output_model = gr.Model3D(
57
- label="Output Model",
58
- interactive=False,
59
- )
60
- output_model2 = gr.Model3D(
61
- label="Output Model",
62
- interactive=False,
63
- )
64
- with gr.Tab("obj"):
65
- output_model = gr.Model3D(
66
- label="Output Model",
67
- interactive=False,
68
- )
69
- with gr.Tab("glb"):
70
- output_model2 = gr.Model3D(
71
- label="Output Model",
72
- interactive=False,
73
- )
74
- #...
75
 
76
- with gr.Row():
77
- output_model = gr.Model3D(
78
- label="Output Model",
79
- interactive=False,
80
- )
81
- output_model2 = gr.Model3D(
82
- label="Output Model",
83
- interactive=False,
84
- )
85
-
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  with gr.Column():
88
- gr.Markdown("**Input Image**")
89
  input_image = gr.Image(
90
  label="",
91
  image_mode="RGBA",
92
  sources="upload",
93
  type="pil",
94
  elem_id="content_image",
95
- width=500, # Увеличена ширина входных изображений
96
- )
97
  with gr.Column():
98
  do_remove_background = gr.Checkbox(
99
  label="Remove Background",
@@ -107,113 +95,42 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
107
  value=0.85,
108
  step=0.05,
109
  elem_id="foreground_ratio",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  )
111
- with gr.Row():
112
- submit = gr.Button(
113
- label="Generate",
114
- scale=0,
115
- variant="primary",
116
- elem_classes="generate-button",
117
- )
118
- output_model = gr.Model3D(
119
- label="Output Model",
120
- interactive=False,
121
- )
122
- output_model2 = gr.Model3D(
123
- label="Output Model",
124
- interactive=False,
125
- )
126
- with gr.Tab("obj"):
127
- output_model = gr.Model3D(
128
- label="Output Model",
129
- interactive=False,
130
- )
131
- output_model2 = gr.Model3D(
132
- label="Output Model",
133
- interactive=False,
134
- )
135
- with gr.Tab("glb"):
136
- output_model2 = gr.Model3D(
137
- label="Output Model",
138
- interactive=False,
139
- )
140
-
141
- with gr.Column():
142
- gr.Markdown("**Output Models**")
143
- output_model = gr.Model3D(label="Output Model", interactive=False)
144
- output_model2 = gr.Model3D(label="Output Model", interactive=False)
145
-
146
- with gr.Row():
147
- #...
148
-
149
- with gr.Button("Download Example", elem_id="download_example", fn=partial(run_example, inputs=[input_image], outputs=[input_image, output_model]):
150
- #...
151
 
152
  with gr.Column():
153
  gr.Examples(
154
- examples=[
155
- "examples/1.png",
156
- "examples/2.png",
157
- ],
158
  inputs=[input_image],
159
- outputs=[output_model, output_model2],
160
- fn=partial(run_example),
161
- label="Examples",
162
- examples_per_page=20,
163
- )
164
- submit.click(
165
- fn=partial(run_example),
166
- inputs=[input_image],
167
- outputs=[output_model, output_model2],
168
- )
169
-
170
- with gr.Button("Generate", elem_id="generate", variant="primary", fn=generate, inputs=[output_model, output_model2]):
171
- #...
172
-
173
- with gr.Row():
174
- output_model = gr.Model3D(
175
- label="Output Model",
176
- interactive=False,
177
- )
178
- output_model2 = gr.Model3D(
179
- label="Output Model",
180
- interactive=False,
181
  )
182
- )
183
 
184
- with gr.Column():
185
- gr.Markdown("**Input Image**")
186
- input_image = gr.Image(
187
- label="",
188
- image_mode="RGBA",
189
- sources="upload",
190
- type="pil",
191
- elem_id="content_image",
192
- width=500, # Увеличена ширина входных изображений
193
- )
194
- with gr.Column():
195
- do_remove_background = gr.Checkbox(
196
- label="Remove Background",
197
- value=True,
198
- elem_id="remove_background",
199
- )
200
- foreground_ratio = gr.Slider(
201
- label="Foreground Ratio",
202
- minimum=0.5,
203
- maximum=1.0,
204
- value=0.85,
205
- step=0.05,
206
- elem_id="foreground_ratio",
207
- )
208
- with gr.Row():
209
- submit = gr.Button(
210
- label="Generate",
211
- scale=0,
212
- variant="primary",
213
- elem_classes="generate-button",
214
- )
215
- output_model = gr.Model3D(
216
- label="Output Model",
217
- interactive=False,
218
- )
219
- output_model2
 
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
 
18
+ if torch.cuda.is_available():
19
+ device = "cuda:0"
20
+ else:
21
+ device = "cpu"
22
 
23
+ d = os.environ.get("DEVICE", None)
24
+ if d!= None:
25
+ device = d
26
 
27
+ model = TSR.from_pretrained(
28
+ "stabilityai/TripoSR",
29
+ config_name="config.yaml",
30
+ weight_name="model.ckpt",
31
+ # token=HF_TOKEN
32
+ )
33
+ model.renderer.set_chunk_size(131072)
34
+ # Увеличение разрешения входного изображения
35
+ model.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ rembg_session = rembg.new_session()
 
 
 
 
 
 
 
 
 
38
 
39
+ def preprocess(input_image, do_remove_background, foreground_ratio):
40
+ def fill_background(image):
41
+ image = np.array(image).astype(np.float32) / 255.0
42
+ image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
43
+ image = Image.fromarray((image * 255.0).astype(np.uint8))
44
+ return image
45
+
46
+ if do_remove_background:
47
+ image = image.convert("RGB")
48
+ image = remove_background(image, rembg_session)
49
+ image = resize_foreground(image, foreground_ratio)
50
+ image = fill_background(image)
51
+ else:
52
+ if image.mode == "RGBA":
53
+ image = fill_background(image)
54
+ return image
55
+
56
+ def generate(image):
57
+ scene_codes = model(image, device=device)
58
+ # Увеличение разрешения входного изображения
59
+ mesh = model.extract_mesh(scene_codes)[0]
60
+ mesh = to_gradio_3d_orientation(mesh)
61
+ mesh_path = tempfile.NamedTemporaryFile(suffix=".obj", delete=False)
62
+ mesh.export(mesh_path.name)
63
+ # Увеличение разрешения входного изображения
64
+ mesh_path2 = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
65
+ mesh.export(mesh_path2.name)
66
+ # Увеличение разрешения входного изображения
67
+ return mesh_path.name, mesh_path2.name
68
+
69
+ def run_example(image_pil):
70
+ # Увеличение разрешения входного изображения
71
+ preprocessed = preprocess(image_pil, False, 0.9)
72
+ mesh_name, mesh_name2 = generate(preprocessed) # Увеличение разрешения входного изображения
73
+ return image_pil, mesh_name, mesh_name2
74
+
75
+ with gr.Blocks() as demo:
76
+ gr.Markdown("# **Input Image**")
77
  with gr.Column():
 
78
  input_image = gr.Image(
79
  label="",
80
  image_mode="RGBA",
81
  sources="upload",
82
  type="pil",
83
  elem_id="content_image",
84
+ ) # Увеличение разрешения входного изображения
 
85
  with gr.Column():
86
  do_remove_background = gr.Checkbox(
87
  label="Remove Background",
 
95
  value=0.85,
96
  step=0.05,
97
  elem_id="foreground_ratio",
98
+ ) # Увеличение разрешения входного изображения
99
+ with gr.Row():
100
+ submit = gr.Button("Generate")
101
+ submit.click(
102
+ fn=check_input_image,
103
+ inputs=[input_image],
104
+ outputs=[],
105
+ ).click(
106
+ fn=preprocess,
107
+ inputs=[input_image, do_remove_background, foreground_ratio],
108
+ outputs=[input_image],
109
+ ).click(
110
+ fn=generate,
111
+ inputs=[input_image],
112
+ outputs=["mesh_name", "mesh_name2"],
113
  )
114
+ with gr.Tab("obj"):
115
+ output_model = gr.Model3D(
116
+ label="Output Model",
117
+ interactive=False,
118
+ )
119
+ with gr.Tab("glb"):
120
+ output_model2 = gr.Model3D(
121
+ label="Output Model",
122
+ interactive=False,
123
+ )
124
+ #...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  with gr.Column():
127
  gr.Examples(
128
+ examples=["examples/1.png"],
 
 
 
129
  inputs=[input_image],
130
+ outputs=[input_image, output_model, output_model2],
131
+ fn=run_example,
132
+ cache_examples=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  )
 
134
 
135
+ demo.queue(max_size=10)
136
+ demo.launch()