sab commited on
Commit
6f989dc
·
1 Parent(s): fffabea
Files changed (1) hide show
  1. app.py +25 -64
app.py CHANGED
@@ -1,48 +1,28 @@
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 not isinstance(image, np.ndarray):
31
- print(f"Type of input: {type(image)}")
32
- raise TypeError("Input must be a numpy array")
33
-
34
- # Determine the mode based on the shape and dtype of the image
35
- if image.ndim == 2: # Grayscale image
36
- mode = "L"
37
- elif image.ndim == 3 and image.shape[2] == 3: # RGB image
38
- mode = "RGB"
39
- elif image.ndim == 3 and image.shape[2] == 4: # RGBA image
40
- mode = "RGBA"
41
- else:
42
- raise ValueError("Unsupported image shape: {}".format(image.shape))
43
-
44
- return Image.fromarray(image, mode)
45
 
 
46
  image = numpy_to_pil(image) # Convert numpy array to PIL Image
47
  buffered = BytesIO()
48
  image.save(buffered, format="PNG")
@@ -54,40 +34,21 @@ def process(image):
54
  result = response.json()
55
  processed_image_b64 = result["processed_image"]
56
  processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
57
- return processed_image # Return the original and processed images
58
-
59
 
60
- def process_file(f):
61
- name_path = f.rsplit(".", 1)[0] + ".png"
62
- im = load_img(f, output_type="pil")
63
- im = im.convert("RGB")
64
- transparent = process(im)
65
- transparent.save(name_path)
66
- return name_path
67
-
68
-
69
- slider1 = ImageSlider(label="RMBG-2.0", type="pil")
70
- slider2 = ImageSlider(label="RMBG-2.0", type="pil")
71
- image = gr.Image(label="Upload an image")
72
- image2 = gr.Image(label="Upload an image", type="filepath")
73
- text = gr.Textbox(label="Paste an image URL")
74
- png_file = gr.File(label="output png file")
75
 
 
76
  chameleon = load_img("elephant.jpg", output_type="pil")
77
 
78
- url = "http://farm9.staticflickr.com/8488/8228323072_76eeddfea3_z.jpg"
79
-
80
- tab1 = gr.Interface(
81
- fn, inputs=image, outputs=[slider1, gr.File(label="output png file")], examples=[chameleon], api_name="image"
82
- )
83
-
84
- tab2 = gr.Interface(fn, inputs=text, outputs=[slider2, gr.File(label="output png file")], examples=[url],
85
- api_name="text")
86
- tab3 = gr.Interface(process_file, inputs=image2, outputs=png_file, examples=["elephant.jpg"], api_name="png")
87
-
88
- demo = gr.TabbedInterface(
89
- [tab1, tab2], ["input image", "input url"], title=" background removal"
90
  )
91
 
92
  if __name__ == "__main__":
93
- demo.launch(show_error=True)
 
 
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
+ from dotenv import load_dotenv
 
 
 
12
 
13
+ # Carica le variabili di ambiente dal file .env
14
+ load_dotenv()
 
 
 
 
 
 
15
 
16
+ def numpy_to_pil(image):
17
+ """Convert a numpy array to a PIL Image."""
18
+ if image.dtype == np.uint8: # Most common case
19
+ mode = "RGB"
20
+ else:
21
+ mode = "F" # Floating point
22
+ return Image.fromarray(image.astype('uint8'), mode)
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ def process_image(image):
26
  image = numpy_to_pil(image) # Convert numpy array to PIL Image
27
  buffered = BytesIO()
28
  image.save(buffered, format="PNG")
 
34
  result = response.json()
35
  processed_image_b64 = result["processed_image"]
36
  processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
37
+ return [image, processed_image] # Return the original and processed images
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ # Carica l'esempio di immagine
41
  chameleon = load_img("elephant.jpg", output_type="pil")
42
 
43
+ image = gr.Image(label="Upload a photo")
44
+ output_slider = ImageSlider(label="Processed photo", type="pil")
45
+ demo = gr.Interface(
46
+ fn=process_image,
47
+ inputs=image,
48
+ outputs=output_slider,
49
+ title="Magic Eraser",
50
+ examples=[["elephant.jpg"]] # Esempio locale
 
 
 
 
51
  )
52
 
53
  if __name__ == "__main__":
54
+ demo.launch()