Jonny001 commited on
Commit
717cb3a
·
verified ·
1 Parent(s): 1de2b0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -17
app.py CHANGED
@@ -3,27 +3,107 @@ import gradio as gr
3
  from transparent_background import Remover
4
  from PIL import Image
5
  import numpy as np
 
 
 
 
 
6
 
7
  @spaces.GPU
8
  def remove_background(image):
9
- remover = Remover()
10
- if isinstance(image, Image.Image):
 
 
 
 
 
11
  output = remover.process(image)
12
- elif isinstance(image, np.ndarray):
13
- image_pil = Image.fromarray(image)
14
- output = remover.process(image_pil)
15
- else:
16
- raise TypeError("Unsupported image type")
17
- return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- iface = gr.Interface(
20
- fn=remove_background,
21
- inputs=gr.Image(label="Upload Image"),
22
- outputs=gr.Image(label="Output Image"),
23
- title="✂️ Image Background Remover ✂️",
24
- description="⚠️ Sorry for the inconvenience. The model is currently running on the CPU, which might affect performance. We appreciate your understanding.",
25
- theme="Yntec/HaleyCH_Theme_Orange"
26
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  if __name__ == "__main__":
29
- iface.launch()
 
3
  from transparent_background import Remover
4
  from PIL import Image
5
  import numpy as np
6
+ import io
7
+ import time
8
+
9
+ # Initialize the remover once to improve performance
10
+ remover = Remover()
11
 
12
  @spaces.GPU
13
  def remove_background(image):
14
+ try:
15
+ # Convert input to PIL Image if needed
16
+ if isinstance(image, np.ndarray):
17
+ image = Image.fromarray(image)
18
+
19
+ # Process the image
20
+ start_time = time.time()
21
  output = remover.process(image)
22
+ processing_time = time.time() - start_time
23
+
24
+ # Convert to PNG with transparency
25
+ img_byte_arr = io.BytesIO()
26
+ output.save(img_byte_arr, format='PNG')
27
+ img_byte_arr.seek(0)
28
+
29
+ return img_byte_arr, f"Processing time: {processing_time:.2f}s"
30
+
31
+ except Exception as e:
32
+ return None, f"Error: {str(e)}"
33
+
34
+ # Custom CSS for a more professional look
35
+ custom_css = """
36
+ #header {
37
+ text-align: center;
38
+ }
39
+ #processing-info {
40
+ text-align: center;
41
+ color: #666;
42
+ font-size: 0.9em;
43
+ }
44
+ .output-image {
45
+ border: 1px solid #e0e0e0;
46
+ border-radius: 8px;
47
+ }
48
+ .download-btn {
49
+ background-color: #f36d3a !important;
50
+ color: white !important;
51
+ border: none !important;
52
+ }
53
+ .download-btn:hover {
54
+ background-color: #e55a2b !important;
55
+ }
56
+ """
57
 
58
+ with gr.Blocks(title="Professional Background Remover", css=custom_css, theme="Yntec/HaleyCH_Theme_Orange") as demo:
59
+ gr.Markdown("""
60
+ # ✂️ Professional Image Background Remover
61
+ Remove backgrounds from your images with high precision. Perfect for product photos, portraits, and more.
62
+ """)
63
+
64
+ with gr.Row():
65
+ with gr.Column():
66
+ input_image = gr.Image(label="Upload Image", type="filepath")
67
+ process_btn = gr.Button("Remove Background", variant="primary")
68
+
69
+ with gr.Column():
70
+ output_image = gr.Image(label="Result with Transparency", type="filepath", elem_classes="output-image")
71
+ processing_info = gr.Markdown(elem_id="processing-info")
72
+
73
+ with gr.Row():
74
+ download_btn = gr.DownloadButton("Download PNG", variant="primary", elem_classes="download-btn")
75
+
76
+ # Examples
77
+ gr.Examples(
78
+ examples=[
79
+ ["https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Sunflower_from_Silesia2.jpg/800px-Sunflower_from_Silesia2.jpg"],
80
+ ["https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/800px-Image_created_with_a_mobile_phone.png"]
81
+ ],
82
+ inputs=input_image,
83
+ outputs=[output_image, processing_info],
84
+ fn=remove_background,
85
+ cache_examples=True,
86
+ label="Click on any example below to start"
87
+ )
88
+
89
+ # Set up processing
90
+ process_btn.click(
91
+ fn=remove_background,
92
+ inputs=input_image,
93
+ outputs=[output_image, processing_info]
94
+ )
95
+
96
+ # Set up download
97
+ def get_download_link(output_data):
98
+ if output_data:
99
+ return output_data
100
+ return None
101
+
102
+ output_image.change(
103
+ fn=get_download_link,
104
+ inputs=output_image,
105
+ outputs=download_btn
106
+ )
107
 
108
  if __name__ == "__main__":
109
+ demo.launch()