Kims12 commited on
Commit
543442b
ยท
verified ยท
1 Parent(s): 4e96c9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -24
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import gradio as gr
2
  from gradio_imageslider import ImageSlider
3
  from loadimg import load_img
4
- import spaces
5
  from transformers import AutoModelForImageSegmentation
6
  import torch
7
  from torchvision import transforms
 
8
 
9
  # GPU ์„ค์ •์„ CPU๋กœ ๋ณ€๊ฒฝ
10
- # GPU ์„ค์ •์„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ "cuda"๋ฅผ "cpu"๋กœ ๋ณ€๊ฒฝ
11
- # torch.set_float32_matmul_precision("high")๋Š” CPU์—์„  ํ•„์š” ์—†์Œ.
12
-
13
  birefnet = AutoModelForImageSegmentation.from_pretrained(
14
  "ZhengPeng7/BiRefNet", trust_remote_code=True
15
  )
@@ -28,10 +25,11 @@ def fn(image):
28
  im = im.convert("RGB")
29
  origin = im.copy()
30
  processed_image = process(im)
31
- return (processed_image, origin)
32
-
33
- # @spaces.GPU ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ œ๊ฑฐ
34
- # CPU ํ™˜๊ฒฝ์—์„œ ๋™์ž‘ํ•˜๋„๋ก ์„ค์ •
 
35
 
36
  def process(image):
37
  image_size = image.size
@@ -45,31 +43,41 @@ def process(image):
45
  image.putalpha(mask)
46
  return image
47
 
48
- def process_file(f):
49
- name_path = f.rsplit(".", 1)[0] + ".png"
50
  im = load_img(f, output_type="pil")
51
  im = im.convert("RGB")
52
  transparent = process(im)
53
- transparent.save(name_path)
54
- return name_path
 
 
 
 
 
 
55
 
56
- slider1 = ImageSlider(label="Processed Image", type="pil")
57
- slider2 = ImageSlider(label="Processed Image from URL", type="pil")
58
  image_upload = gr.Image(label="Upload an image")
59
- image_file_upload = gr.Image(label="Upload an image", type="filepath")
60
- url_input = gr.Textbox(label="Paste an image URL")
61
- output_file = gr.File(label="Output PNG File")
62
 
63
- # Example images
64
- chameleon = load_img("butterfly.jpg", output_type="pil")
65
- url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
 
 
 
66
 
67
- tab1 = gr.Interface(fn, inputs=image_upload, outputs=slider1, examples=[chameleon], api_name="image")
68
- tab2 = gr.Interface(fn, inputs=url_input, outputs=slider2, examples=[url_example], api_name="text")
69
- tab3 = gr.Interface(process_file, inputs=image_file_upload, outputs=output_file, examples=["butterfly.jpg"], api_name="png")
 
 
 
 
70
 
71
  demo = gr.TabbedInterface(
72
- [tab1, tab2, tab3], ["Image Upload", "URL Input", "File Output"], title="Background Removal Tool"
 
 
73
  )
74
 
75
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from gradio_imageslider import ImageSlider
3
  from loadimg import load_img
 
4
  from transformers import AutoModelForImageSegmentation
5
  import torch
6
  from torchvision import transforms
7
+ from io import BytesIO
8
 
9
  # GPU ์„ค์ •์„ CPU๋กœ ๋ณ€๊ฒฝ
 
 
 
10
  birefnet = AutoModelForImageSegmentation.from_pretrained(
11
  "ZhengPeng7/BiRefNet", trust_remote_code=True
12
  )
 
25
  im = im.convert("RGB")
26
  origin = im.copy()
27
  processed_image = process(im)
28
+ # Convert processed image to JPEG
29
+ buffered = BytesIO()
30
+ processed_image.convert("RGB").save(buffered, format="JPEG")
31
+ buffered.seek(0)
32
+ return processed_image, buffered
33
 
34
  def process(image):
35
  image_size = image.size
 
43
  image.putalpha(mask)
44
  return image
45
 
46
+ def process_download(f):
47
+ name_path = f.rsplit(".", 1)[0] + ".jpg"
48
  im = load_img(f, output_type="pil")
49
  im = im.convert("RGB")
50
  transparent = process(im)
51
+ # Convert to JPEG
52
+ buffered = BytesIO()
53
+ transparent.convert("RGB").save(buffered, format="JPEG")
54
+ buffered.seek(0)
55
+ return buffered
56
+
57
+ slider = ImageSlider(label="Processed Image", type="pil")
58
+ download_output = gr.File(label="Download JPG File")
59
 
 
 
60
  image_upload = gr.Image(label="Upload an image")
 
 
 
61
 
62
+ # ์ƒˆ๋กœ์šด ์ƒ˜ํ”Œ ์ด๋ฏธ์ง€
63
+ sample_images = [
64
+ "1.png",
65
+ "2.jpg",
66
+ "3.png"
67
+ ]
68
 
69
+ tab = gr.Interface(
70
+ fn=fn,
71
+ inputs=image_upload,
72
+ outputs=[slider, download_output],
73
+ examples=sample_images,
74
+ api_name="image"
75
+ )
76
 
77
  demo = gr.TabbedInterface(
78
+ [tab],
79
+ ["Image Upload"],
80
+ title="Background Removal Tool"
81
  )
82
 
83
  if __name__ == "__main__":