mrbui1990 commited on
Commit
f2ab5b4
·
verified ·
1 Parent(s): 50a6dd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -5
app.py CHANGED
@@ -3,9 +3,9 @@ import numpy as np
3
  import random
4
  import torch
5
  import spaces
6
-
7
-
8
-
9
  from PIL import Image
10
  from diffusers import FlowMatchEulerDiscreteScheduler,DiffusionPipeline
11
  from optimization import optimize_pipeline_
@@ -17,7 +17,7 @@ from huggingface_hub import InferenceClient
17
  import math
18
  from huggingface_hub import hf_hub_download
19
  from safetensors.torch import load_file
20
-
21
  from basicsr.archs.rrdbnet_arch import RRDBNet
22
  from basicsr.utils.download_util import load_file_from_url
23
  from realesrgan import RealESRGANer
@@ -37,6 +37,42 @@ import tempfile
37
  from PIL import Image
38
  import gradio as gr
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # --- Upscaling ---
42
  MAX_SEED = np.iinfo(np.int32).max
@@ -348,11 +384,17 @@ def infer(
348
  true_cfg_scale=true_guidance_scale,
349
  num_images_per_prompt=num_images_per_prompt,
350
  ).images
 
 
 
 
 
 
351
  if return_upscaled:
352
  upscaled = realesrgan(image[0], "realesr-general-x4v3", 0.5, 2)
353
  return image, upscaled, seed
354
  else:
355
- return image, None, seed
356
 
357
 
358
  # --- Examples and UI Layout ---
 
3
  import random
4
  import torch
5
  import spaces
6
+ from transformers import AutoModelForImageSegmentation
7
+ from torchvision import transforms
8
+ from typing import Union, Tuple
9
  from PIL import Image
10
  from diffusers import FlowMatchEulerDiscreteScheduler,DiffusionPipeline
11
  from optimization import optimize_pipeline_
 
17
  import math
18
  from huggingface_hub import hf_hub_download
19
  from safetensors.torch import load_file
20
+ from typing import Union, Tuple
21
  from basicsr.archs.rrdbnet_arch import RRDBNet
22
  from basicsr.utils.download_util import load_file_from_url
23
  from realesrgan import RealESRGANer
 
37
  from PIL import Image
38
  import gradio as gr
39
 
40
+ torch.set_float32_matmul_precision(["high", "highest"][0])
41
+
42
+ birefnet = AutoModelForImageSegmentation.from_pretrained(
43
+ "ZhengPeng7/BiRefNet", trust_remote_code=True
44
+ )
45
+ birefnet.to("cuda")
46
+
47
+ transform_image = transforms.Compose(
48
+ [
49
+ transforms.Resize((1024, 1024)),
50
+ transforms.ToTensor(),
51
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
52
+ ]
53
+ )
54
+
55
+ @spaces.GPU
56
+ def processRemove(image: Image.Image) -> Image.Image:
57
+ """
58
+ Apply BiRefNet-based image segmentation to remove the background.
59
+ This function preprocesses the input image, runs it through a BiRefNet segmentation model to obtain a mask,
60
+ and applies the mask as an alpha (transparency) channel to the original image.
61
+ Args:
62
+ image (PIL.Image): The input RGB image.
63
+ Returns:
64
+ PIL.Image: The image with the background removed, using the segmentation mask as transparency.
65
+ """
66
+ image_size = image.size
67
+ input_images = transform_image(image).unsqueeze(0).to("cuda")
68
+ # Prediction
69
+ with torch.no_grad():
70
+ preds = birefnet(input_images)[-1].sigmoid().cpu()
71
+ pred = preds[0].squeeze()
72
+ pred_pil = transforms.ToPILImage()(pred)
73
+ mask = pred_pil.resize(image_size)
74
+ image.putalpha(mask)
75
+ return image
76
 
77
  # --- Upscaling ---
78
  MAX_SEED = np.iinfo(np.int32).max
 
384
  true_cfg_scale=true_guidance_scale,
385
  num_images_per_prompt=num_images_per_prompt,
386
  ).images
387
+
388
+ im = load_img(image[0], output_type="pil")
389
+ im = im.convert("RGB")
390
+ origin = im.copy()
391
+ removed_image = process(im)
392
+
393
  if return_upscaled:
394
  upscaled = realesrgan(image[0], "realesr-general-x4v3", 0.5, 2)
395
  return image, upscaled, seed
396
  else:
397
+ return image, removed_image, seed
398
 
399
 
400
  # --- Examples and UI Layout ---