Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 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
|
|
|
|
| 8 |
|
| 9 |
# GPU ์ค์ ์ CPU๋ก ๋ณ๊ฒฝ
|
| 10 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
|
@@ -25,11 +27,14 @@ def fn(image):
|
|
| 25 |
im = im.convert("RGB")
|
| 26 |
origin = im.copy()
|
| 27 |
processed_image = process(im)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def process(image):
|
| 35 |
image_size = image.size
|
|
@@ -43,30 +48,33 @@ def process(image):
|
|
| 43 |
image.putalpha(mask)
|
| 44 |
return image
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 49 |
image_upload = gr.Image(label="Upload an image")
|
|
|
|
| 50 |
|
| 51 |
-
# ์๋ก์ด ์ํ ์ด๋ฏธ์ง
|
| 52 |
-
sample_images = [
|
| 53 |
-
["1.png"],
|
| 54 |
-
["2.jpg"],
|
| 55 |
-
["3.png"]
|
| 56 |
-
]
|
| 57 |
|
| 58 |
-
|
| 59 |
fn=fn,
|
| 60 |
inputs=image_upload,
|
| 61 |
-
outputs=[
|
| 62 |
examples=sample_images,
|
| 63 |
api_name="image"
|
| 64 |
)
|
| 65 |
|
| 66 |
-
demo = gr.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 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 |
+
from PIL import Image
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
# GPU ์ค์ ์ CPU๋ก ๋ณ๊ฒฝ
|
| 12 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
|
|
|
| 27 |
im = im.convert("RGB")
|
| 28 |
origin = im.copy()
|
| 29 |
processed_image = process(im)
|
| 30 |
+
|
| 31 |
+
# JPG๋ก ๋ณํํ์ฌ ์ ์ฅ
|
| 32 |
+
jpg_image = origin.copy()
|
| 33 |
+
jpg_image = jpg_image.convert("RGB")
|
| 34 |
+
jpg_path = "output.jpg"
|
| 35 |
+
jpg_image.save(jpg_path, format="JPEG")
|
| 36 |
+
|
| 37 |
+
return processed_image, jpg_path
|
| 38 |
|
| 39 |
def process(image):
|
| 40 |
image_size = image.size
|
|
|
|
| 48 |
image.putalpha(mask)
|
| 49 |
return image
|
| 50 |
|
| 51 |
+
def process_file(f):
|
| 52 |
+
name_path = f.rsplit(".", 1)[0] + ".png"
|
| 53 |
+
im = load_img(f, output_type="pil")
|
| 54 |
+
im = im.convert("RGB")
|
| 55 |
+
transparent = process(im)
|
| 56 |
+
transparent.save(name_path)
|
| 57 |
+
return name_path
|
| 58 |
|
| 59 |
+
slider1 = ImageSlider(label="Processed Image", type="pil")
|
| 60 |
image_upload = gr.Image(label="Upload an image")
|
| 61 |
+
output_download = gr.File(label="Download JPG File")
|
| 62 |
|
| 63 |
+
# ์๋ก์ด ์ํ ์ด๋ฏธ์ง ์ถ๊ฐ
|
| 64 |
+
sample_images = ["1.png", "2.jpg", "3.png"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
tab1 = gr.Interface(
|
| 67 |
fn=fn,
|
| 68 |
inputs=image_upload,
|
| 69 |
+
outputs=[slider1, output_download],
|
| 70 |
examples=sample_images,
|
| 71 |
api_name="image"
|
| 72 |
)
|
| 73 |
|
| 74 |
+
demo = gr.Interface(
|
| 75 |
+
tab1,
|
| 76 |
+
title="Background Removal Tool",
|
| 77 |
+
description="์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฐฐ๊ฒฝ์ด ์ ๊ฑฐ๋ ์ด๋ฏธ์ง๋ฅผ ํ์ธํ๊ณ JPG ํ์ผ๋ก ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค."
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|