Spaces:
Running
Running
sab
commited on
Commit
•
fb68583
1
Parent(s):
2227f01
test v4
Browse files
app.py
CHANGED
@@ -1,28 +1,37 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import base64
|
4 |
-
import os
|
5 |
from PIL import Image
|
6 |
-
import gradio as gr
|
7 |
-
os.getenv('BACKEND_URL')
|
8 |
-
import numpy as np
|
9 |
from io import BytesIO
|
10 |
-
|
11 |
-
from loadimg import load_img # Assicurati che questa funzione sia disponibile
|
12 |
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
def numpy_to_pil(image):
|
16 |
-
"""Convert a numpy array to a PIL Image."""
|
17 |
-
if image.dtype == np.uint8: # Most common case
|
18 |
-
mode = "RGB"
|
19 |
-
else:
|
20 |
-
mode = "F" # Floating point
|
21 |
-
return Image.fromarray(image.astype('uint8'), mode)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
image = numpy_to_pil(image) # Convert numpy array to PIL Image
|
28 |
buffered = BytesIO()
|
@@ -35,17 +44,40 @@ def process_image(image):
|
|
35 |
result = response.json()
|
36 |
processed_image_b64 = result["processed_image"]
|
37 |
processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
|
38 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
demo = gr.Interface(
|
44 |
-
fn=process_image,
|
45 |
-
inputs=image,
|
46 |
-
outputs=output_slider,
|
47 |
-
title="Magic Eraser",
|
48 |
-
#examples=[["elephant.jpg"]] # Esempio locale
|
49 |
)
|
50 |
|
51 |
-
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from gradio_imageslider import ImageSlider
|
4 |
+
from loadimg import load_img
|
5 |
+
|
6 |
import requests
|
7 |
import base64
|
|
|
8 |
from PIL import Image
|
|
|
|
|
|
|
9 |
from io import BytesIO
|
10 |
+
import numpy as np
|
|
|
11 |
|
12 |
+
output_folder = 'output_images'
|
13 |
+
if not os.path.exists(output_folder):
|
14 |
+
os.makedirs(output_folder)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
def fn(image):
|
18 |
+
im = load_img(image, output_type="pil")
|
19 |
+
im = im.convert("RGB")
|
20 |
+
origin = im.copy()
|
21 |
+
image = process(im)
|
22 |
+
image_path = os.path.join(output_folder, "no_bg_image.png")
|
23 |
+
image.save(image_path)
|
24 |
+
return (image, origin), image_path
|
25 |
|
26 |
+
|
27 |
+
def process(image):
|
28 |
+
def numpy_to_pil(image):
|
29 |
+
"""Convert a numpy array to a PIL Image."""
|
30 |
+
if image.dtype == np.uint8: # Most common case
|
31 |
+
mode = "RGB"
|
32 |
+
else:
|
33 |
+
mode = "F" # Floating point
|
34 |
+
return Image.fromarray(image.astype('uint8'), mode)
|
35 |
|
36 |
image = numpy_to_pil(image) # Convert numpy array to PIL Image
|
37 |
buffered = BytesIO()
|
|
|
44 |
result = response.json()
|
45 |
processed_image_b64 = result["processed_image"]
|
46 |
processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
|
47 |
+
return processed_image # Return the original and processed images
|
48 |
+
|
49 |
+
|
50 |
+
def process_file(f):
|
51 |
+
name_path = f.rsplit(".", 1)[0] + ".png"
|
52 |
+
im = load_img(f, output_type="pil")
|
53 |
+
im = im.convert("RGB")
|
54 |
+
transparent = process(im)
|
55 |
+
transparent.save(name_path)
|
56 |
+
return name_path
|
57 |
+
|
58 |
+
|
59 |
+
slider1 = ImageSlider(label="RMBG-2.0", type="pil")
|
60 |
+
slider2 = ImageSlider(label="RMBG-2.0", type="pil")
|
61 |
+
image = gr.Image(label="Upload an image")
|
62 |
+
image2 = gr.Image(label="Upload an image", type="filepath")
|
63 |
+
text = gr.Textbox(label="Paste an image URL")
|
64 |
+
png_file = gr.File(label="output png file")
|
65 |
+
|
66 |
+
chameleon = load_img("elephant.jpg", output_type="pil")
|
67 |
+
|
68 |
+
url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"
|
69 |
+
|
70 |
+
tab1 = gr.Interface(
|
71 |
+
fn, inputs=image, outputs=[slider1, gr.File(label="output png file")], examples=[chameleon], api_name="image"
|
72 |
+
)
|
73 |
|
74 |
+
tab2 = gr.Interface(fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], examples=[url],
|
75 |
+
api_name="text")
|
76 |
+
tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["elephant.jpg"], api_name="png")
|
77 |
|
78 |
+
demo = gr.TabbedInterface(
|
79 |
+
[tab1, tab2], ["input image", "input url"], title=" background removal"
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
)
|
81 |
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch(show_error=True)
|