Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,13 @@ from PIL import Image
|
|
| 14 |
import sf3d.utils as sf3d_utils
|
| 15 |
from sf3d.system import SF3D
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
rembg_session = rembg.new_session()
|
| 18 |
|
| 19 |
COND_WIDTH = 512
|
|
@@ -167,189 +174,27 @@ def show_mask_img(input_image: Image) -> Image:
|
|
| 167 |
return Image.fromarray(new_img.astype(np.uint8), mode="RGB")
|
| 168 |
|
| 169 |
|
| 170 |
-
def run_button(run_btn, input_image, background_state, foreground_ratio):
|
| 171 |
-
if run_btn == "Run":
|
| 172 |
-
glb_file: str = run_model(background_state)
|
| 173 |
-
|
| 174 |
-
return (
|
| 175 |
-
gr.update(),
|
| 176 |
-
gr.update(),
|
| 177 |
-
gr.update(),
|
| 178 |
-
gr.update(),
|
| 179 |
-
gr.update(value=glb_file, visible=True),
|
| 180 |
-
gr.update(visible=True),
|
| 181 |
-
)
|
| 182 |
-
elif run_btn == "Remove Background":
|
| 183 |
-
rem_removed = remove_background(input_image)
|
| 184 |
-
|
| 185 |
-
sqr_crop = square_crop(rem_removed)
|
| 186 |
-
fr_res = resize_foreground(sqr_crop, foreground_ratio)
|
| 187 |
-
|
| 188 |
-
return (
|
| 189 |
-
gr.update(value="Run", visible=True),
|
| 190 |
-
sqr_crop,
|
| 191 |
-
fr_res,
|
| 192 |
-
gr.update(value=show_mask_img(fr_res), visible=True),
|
| 193 |
-
gr.update(value=None, visible=False),
|
| 194 |
-
gr.update(visible=False),
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
def requires_bg_remove(image, fr):
|
| 199 |
-
if image is None:
|
| 200 |
-
return (
|
| 201 |
-
gr.update(visible=False, value="Run"),
|
| 202 |
-
None,
|
| 203 |
-
None,
|
| 204 |
-
gr.update(value=None, visible=False),
|
| 205 |
-
gr.update(visible=False),
|
| 206 |
-
gr.update(visible=False),
|
| 207 |
-
)
|
| 208 |
-
alpha_channel = np.array(image.getchannel("A"))
|
| 209 |
-
min_alpha = alpha_channel.min()
|
| 210 |
-
|
| 211 |
-
if min_alpha == 0:
|
| 212 |
-
print("Already has alpha")
|
| 213 |
-
sqr_crop = square_crop(image)
|
| 214 |
-
fr_res = resize_foreground(sqr_crop, fr)
|
| 215 |
-
return (
|
| 216 |
-
gr.update(value="Run", visible=True),
|
| 217 |
-
sqr_crop,
|
| 218 |
-
fr_res,
|
| 219 |
-
gr.update(value=show_mask_img(fr_res), visible=True),
|
| 220 |
-
gr.update(visible=False),
|
| 221 |
-
gr.update(visible=False),
|
| 222 |
-
)
|
| 223 |
-
return (
|
| 224 |
-
gr.update(value="Remove Background", visible=True),
|
| 225 |
-
None,
|
| 226 |
-
None,
|
| 227 |
-
gr.update(value=None, visible=False),
|
| 228 |
-
gr.update(visible=False),
|
| 229 |
-
gr.update(visible=False),
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
|
| 233 |
-
def
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
foreground_res,
|
| 237 |
-
gr.update(value=show_mask_img(foreground_res)),
|
| 238 |
-
)
|
| 239 |
|
|
|
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
**Tips**
|
| 251 |
-
1. If the image already has an alpha channel, you can skip the background removal step.
|
| 252 |
-
2. You can adjust the foreground ratio to control the size of the foreground object. This can influence the shape
|
| 253 |
-
3. You can upload your own HDR environment map to light the 3D model.
|
| 254 |
-
""")
|
| 255 |
-
with gr.Row(variant="panel"):
|
| 256 |
-
with gr.Column():
|
| 257 |
-
with gr.Row():
|
| 258 |
-
input_img = gr.Image(
|
| 259 |
-
type="pil", label="Input Image", sources="upload", image_mode="RGBA"
|
| 260 |
-
)
|
| 261 |
-
preview_removal = gr.Image(
|
| 262 |
-
label="Preview Background Removal",
|
| 263 |
-
type="pil",
|
| 264 |
-
image_mode="RGB",
|
| 265 |
-
interactive=False,
|
| 266 |
-
visible=False,
|
| 267 |
-
)
|
| 268 |
-
|
| 269 |
-
foreground_ratio = gr.Slider(
|
| 270 |
-
label="Foreground Ratio",
|
| 271 |
-
minimum=0.5,
|
| 272 |
-
maximum=1.0,
|
| 273 |
-
value=0.85,
|
| 274 |
-
step=0.05,
|
| 275 |
-
)
|
| 276 |
-
|
| 277 |
-
foreground_ratio.change(
|
| 278 |
-
update_foreground_ratio,
|
| 279 |
-
inputs=[img_proc_state, foreground_ratio],
|
| 280 |
-
outputs=[background_remove_state, preview_removal],
|
| 281 |
-
)
|
| 282 |
-
|
| 283 |
-
run_btn = gr.Button("Run", variant="primary", visible=False)
|
| 284 |
-
|
| 285 |
-
with gr.Column():
|
| 286 |
-
output_3d = LitModel3D(
|
| 287 |
-
label="3D Model",
|
| 288 |
-
visible=False,
|
| 289 |
-
clear_color=[0.0, 0.0, 0.0, 0.0],
|
| 290 |
-
tonemapping="aces",
|
| 291 |
-
contrast=1.0,
|
| 292 |
-
scale=1.0,
|
| 293 |
-
)
|
| 294 |
-
with gr.Column(visible=False, scale=1.0) as hdr_row:
|
| 295 |
-
gr.Markdown("""## HDR Environment Map
|
| 296 |
-
|
| 297 |
-
Select an HDR environment map to light the 3D model. You can also upload your own HDR environment maps.
|
| 298 |
-
""")
|
| 299 |
-
|
| 300 |
-
with gr.Row():
|
| 301 |
-
hdr_illumination_file = gr.File(
|
| 302 |
-
label="HDR Env Map", file_types=[".hdr"], file_count="single"
|
| 303 |
-
)
|
| 304 |
-
example_hdris = [
|
| 305 |
-
os.path.join("demo_files/hdri", f)
|
| 306 |
-
for f in os.listdir("demo_files/hdri")
|
| 307 |
-
]
|
| 308 |
-
hdr_illumination_example = gr.Examples(
|
| 309 |
-
examples=example_hdris,
|
| 310 |
-
inputs=hdr_illumination_file,
|
| 311 |
-
)
|
| 312 |
-
|
| 313 |
-
hdr_illumination_file.change(
|
| 314 |
-
lambda x: gr.update(env_map=x.name if x is not None else None),
|
| 315 |
-
inputs=hdr_illumination_file,
|
| 316 |
-
outputs=[output_3d],
|
| 317 |
-
)
|
| 318 |
-
|
| 319 |
-
examples = gr.Examples(
|
| 320 |
-
examples=example_files,
|
| 321 |
-
inputs=input_img,
|
| 322 |
-
)
|
| 323 |
|
| 324 |
-
input_img.change(
|
| 325 |
-
requires_bg_remove,
|
| 326 |
-
inputs=[input_img, foreground_ratio],
|
| 327 |
-
outputs=[
|
| 328 |
-
run_btn,
|
| 329 |
-
img_proc_state,
|
| 330 |
-
background_remove_state,
|
| 331 |
-
preview_removal,
|
| 332 |
-
output_3d,
|
| 333 |
-
hdr_row,
|
| 334 |
-
],
|
| 335 |
-
)
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
inputs=[
|
| 340 |
-
run_btn,
|
| 341 |
-
input_img,
|
| 342 |
-
background_remove_state,
|
| 343 |
-
foreground_ratio,
|
| 344 |
-
],
|
| 345 |
-
outputs=[
|
| 346 |
-
run_btn,
|
| 347 |
-
img_proc_state,
|
| 348 |
-
background_remove_state,
|
| 349 |
-
preview_removal,
|
| 350 |
-
output_3d,
|
| 351 |
-
hdr_row,
|
| 352 |
-
],
|
| 353 |
-
)
|
| 354 |
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import sf3d.utils as sf3d_utils
|
| 15 |
from sf3d.system import SF3D
|
| 16 |
|
| 17 |
+
from fastapi import FastAPI, File, UploadFile
|
| 18 |
+
from fastapi.responses import FileResponse
|
| 19 |
+
|
| 20 |
+
import datetime
|
| 21 |
+
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
rembg_session = rembg.new_session()
|
| 25 |
|
| 26 |
COND_WIDTH = 512
|
|
|
|
| 174 |
return Image.fromarray(new_img.astype(np.uint8), mode="RGB")
|
| 175 |
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
def upload_file_to_s3(file_path, bucket_name, object_name=None):
|
| 179 |
+
|
| 180 |
+
s3_client.upload_file(file_path, bucket_name, object_name)
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
return True
|
| 183 |
|
| 184 |
+
|
| 185 |
+
@app.post("/process-image/")
|
| 186 |
+
async def process_image(file: UploadFile = File(...), foreground_ratio: float = 0.85):
|
| 187 |
+
input_image = Image.open(file.file).convert("RGBA")
|
| 188 |
+
rem_removed = remove_background(input_image)
|
| 189 |
+
sqr_crop = square_crop(rem_removed)
|
| 190 |
+
fr_res = resize_foreground(sqr_crop, foreground_ratio)
|
| 191 |
+
glb_file = run_model(fr_res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
|
| 195 |
+
object_name = f'object_{timestamp}.glb'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
if upload_file_to_s3(glb_file, 'framebucket3d',object_name):
|
| 198 |
+
return {
|
| 199 |
+
"glb_path": f"https://framebucket3d.s3.amazonaws.com/{object_name_2}"
|
| 200 |
+
}
|