Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 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
|
| 49 |
-
name_path = f.rsplit(".", 1)[0] + ".
|
| 50 |
im = load_img(f, output_type="pil")
|
| 51 |
im = im.convert("RGB")
|
| 52 |
transparent = process(im)
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
demo = gr.TabbedInterface(
|
| 72 |
-
[
|
|
|
|
|
|
|
| 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__":
|