File size: 1,643 Bytes
ac1586c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import requests
import base64
import os
from PIL import Image
from io import BytesIO
import numpy as np
from gradio_imageslider import ImageSlider  # Assicurati di avere questa libreria installata
from loadimg import load_img  # Assicurati che questa funzione sia disponibile


def numpy_to_pil(image):
    """Convert a numpy array to a PIL Image."""
    if image.dtype == np.uint8:  # Most common case
        mode = "RGB"
    else:
        mode = "F"  # Floating point
    return Image.fromarray(image.astype('uint8'), mode)


def process_image(image):
    image = numpy_to_pil(image)  # Convert numpy array to PIL Image
    buffered = BytesIO()
    image.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
    response = requests.post(
        os.getenv('BACKEND_URL'),
        files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
    )
    result = response.json()
    processed_image_b64 = result["processed_image"]
    processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
    return [image, processed_image]  # Return the original and processed images


# Carica l'esempio di immagine
chameleon = load_img("elephant.jpg", output_type="pil")
url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"

image = gr.Image(label="Upload a photo")
output_slider = ImageSlider(label="Processed photo", type="pil")
demo = gr.Interface(
    fn=process_image,
    inputs=image,
    outputs=output_slider,
    title="Magic Eraser",
    examples=[["elephant.jpg"]]  # Esempio locale
)

if __name__ == "__main__":
    demo.launch()