Spaces:
Running
Running
sab
commited on
Commit
•
b7139ee
1
Parent(s):
73bd124
- app.py +44 -25
- images/elephant.jpg +0 -0
- images/lion.png +0 -0
- images/tartaruga.png +0 -0
app.py
CHANGED
@@ -1,60 +1,79 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import base64
|
4 |
import os
|
|
|
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
from io import BytesIO
|
7 |
-
|
8 |
-
from gradio_imageslider import ImageSlider # Assicurati di avere questa libreria installata
|
9 |
-
from loadimg import load_img # Assicurati che questa funzione sia disponibile
|
10 |
-
|
11 |
from dotenv import load_dotenv
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
load_dotenv()
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
|
|
21 |
"""Convert a numpy array to a PIL Image."""
|
22 |
-
if image.dtype == np.uint8
|
23 |
-
mode = "RGB"
|
24 |
-
else:
|
25 |
-
mode = "F" # Floating point
|
26 |
return Image.fromarray(image.astype('uint8'), mode)
|
27 |
|
28 |
|
29 |
-
def process_image(image):
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
buffered = BytesIO()
|
32 |
-
|
33 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
|
|
|
|
34 |
response = requests.post(
|
35 |
os.getenv('BACKEND_URL'),
|
36 |
files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
|
37 |
)
|
|
|
|
|
38 |
result = response.json()
|
39 |
processed_image_b64 = result["processed_image"]
|
40 |
processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
|
41 |
-
|
42 |
-
|
|
|
43 |
processed_image.save(image_path)
|
44 |
-
return (processed_image, image), image_path
|
45 |
|
|
|
46 |
|
47 |
-
# Carica l'esempio di immagine
|
48 |
-
chameleon = load_img("elephant.jpg", output_type="pil")
|
49 |
|
|
|
50 |
image = gr.Image(label="Upload a photo")
|
51 |
output_slider = ImageSlider(label="Processed photo", type="pil")
|
|
|
52 |
demo = gr.Interface(
|
53 |
fn=process_image,
|
54 |
inputs=image,
|
55 |
outputs=[output_slider, gr.File(label="output png file")],
|
56 |
title="Magic Eraser",
|
57 |
-
examples=[
|
|
|
|
|
|
|
|
|
58 |
)
|
59 |
|
60 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import uuid
|
3 |
+
import base64
|
4 |
+
import requests
|
5 |
+
import numpy as np
|
6 |
from PIL import Image
|
7 |
from io import BytesIO
|
8 |
+
from pathlib import Path
|
|
|
|
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
+
import gradio as gr
|
11 |
+
from gradio_imageslider import ImageSlider # Ensure this library is installed
|
12 |
|
13 |
+
# Load environment variables from the .env file
|
14 |
load_dotenv()
|
15 |
|
16 |
+
# Define the output folder
|
17 |
+
output_folder = Path('output_images')
|
18 |
+
output_folder.mkdir(exist_ok=True)
|
19 |
|
20 |
+
|
21 |
+
def numpy_to_pil(image: np.ndarray) -> Image.Image:
|
22 |
"""Convert a numpy array to a PIL Image."""
|
23 |
+
mode = "RGB" if image.dtype == np.uint8 else "F"
|
|
|
|
|
|
|
24 |
return Image.fromarray(image.astype('uint8'), mode)
|
25 |
|
26 |
|
27 |
+
def process_image(image: np.ndarray):
|
28 |
+
"""
|
29 |
+
Process the input image by sending it to the backend and saving the output.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
image (np.ndarray): Input image in numpy array format.
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
tuple: Processed images and the path to the saved image.
|
36 |
+
"""
|
37 |
+
# Convert numpy array to PIL Image
|
38 |
+
image_pil = numpy_to_pil(image)
|
39 |
+
|
40 |
+
# Encode image to base64
|
41 |
buffered = BytesIO()
|
42 |
+
image_pil.save(buffered, format="PNG")
|
43 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
44 |
+
|
45 |
+
# Send image to backend
|
46 |
response = requests.post(
|
47 |
os.getenv('BACKEND_URL'),
|
48 |
files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
|
49 |
)
|
50 |
+
|
51 |
+
# Process the response
|
52 |
result = response.json()
|
53 |
processed_image_b64 = result["processed_image"]
|
54 |
processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
|
55 |
+
|
56 |
+
# Save the processed image
|
57 |
+
image_path = output_folder / f"no_bg_image_{uuid.uuid4().hex}.png"
|
58 |
processed_image.save(image_path)
|
|
|
59 |
|
60 |
+
return (processed_image, image_pil), str(image_path)
|
61 |
|
|
|
|
|
62 |
|
63 |
+
# Define inputs and outputs for the Gradio interface
|
64 |
image = gr.Image(label="Upload a photo")
|
65 |
output_slider = ImageSlider(label="Processed photo", type="pil")
|
66 |
+
|
67 |
demo = gr.Interface(
|
68 |
fn=process_image,
|
69 |
inputs=image,
|
70 |
outputs=[output_slider, gr.File(label="output png file")],
|
71 |
title="Magic Eraser",
|
72 |
+
examples=[
|
73 |
+
["images/elephant.jpg"],
|
74 |
+
["images/lion.png"],
|
75 |
+
["images/tartaruga.png"],
|
76 |
+
]
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|
images/elephant.jpg
ADDED
images/lion.png
ADDED
images/tartaruga.png
ADDED