Spaces:
Sleeping
Sleeping
sab
commited on
Commit
•
ac1586c
1
Parent(s):
f8f1e44
first commit
Browse files- .gitignore +1 -0
- app.py +51 -0
- elephant.jpg +0 -0
- requirements.txt +6 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/config.yaml
|
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import numpy as np
|
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 |
+
|
12 |
+
def numpy_to_pil(image):
|
13 |
+
"""Convert a numpy array to a PIL Image."""
|
14 |
+
if image.dtype == np.uint8: # Most common case
|
15 |
+
mode = "RGB"
|
16 |
+
else:
|
17 |
+
mode = "F" # Floating point
|
18 |
+
return Image.fromarray(image.astype('uint8'), mode)
|
19 |
+
|
20 |
+
|
21 |
+
def process_image(image):
|
22 |
+
image = numpy_to_pil(image) # Convert numpy array to PIL Image
|
23 |
+
buffered = BytesIO()
|
24 |
+
image.save(buffered, format="PNG")
|
25 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
26 |
+
response = requests.post(
|
27 |
+
os.getenv('BACKEND_URL'),
|
28 |
+
files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
|
29 |
+
)
|
30 |
+
result = response.json()
|
31 |
+
processed_image_b64 = result["processed_image"]
|
32 |
+
processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
|
33 |
+
return [image, processed_image] # Return the original and processed images
|
34 |
+
|
35 |
+
|
36 |
+
# Carica l'esempio di immagine
|
37 |
+
chameleon = load_img("elephant.jpg", output_type="pil")
|
38 |
+
url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"
|
39 |
+
|
40 |
+
image = gr.Image(label="Upload a photo")
|
41 |
+
output_slider = ImageSlider(label="Processed photo", type="pil")
|
42 |
+
demo = gr.Interface(
|
43 |
+
fn=process_image,
|
44 |
+
inputs=image,
|
45 |
+
outputs=output_slider,
|
46 |
+
title="Magic Eraser",
|
47 |
+
examples=[["elephant.jpg"]] # Esempio locale
|
48 |
+
)
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch()
|
elephant.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
gradio_imageslider
|
3 |
+
requests
|
4 |
+
pillow
|
5 |
+
loadimg>=0.1.1
|
6 |
+
numpy
|