Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,66 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
from src.utils import change_background, matte
|
|
|
|
| 7 |
|
| 8 |
+
def process_images(uploaded_files, background_color):
|
| 9 |
+
# Add debugging statement
|
| 10 |
+
print(f"background_color received: {background_color}")
|
| 11 |
+
|
| 12 |
+
# Default value check
|
| 13 |
+
if background_color is None:
|
| 14 |
+
return "Please select a background color."
|
| 15 |
+
|
| 16 |
+
hexmap = {
|
| 17 |
+
"Transparent (PNG)": "#000000",
|
| 18 |
+
"Black": "#000000",
|
| 19 |
+
"White": "#FFFFFF",
|
| 20 |
+
"Green": "#22EE22",
|
| 21 |
+
"Red": "#EE2222",
|
| 22 |
+
"Blue": "#2222EE",
|
| 23 |
+
}
|
| 24 |
+
alpha = 0.0 if background_color == "Transparent (PNG)" else 1.0
|
| 25 |
+
|
| 26 |
+
results = []
|
| 27 |
+
times = []
|
| 28 |
+
for uploaded_file in uploaded_files:
|
| 29 |
+
start_time = time.time()
|
| 30 |
+
img_input = Image.open(uploaded_file) # Open each file
|
| 31 |
+
img_matte = matte(img_input)
|
| 32 |
+
img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[background_color])
|
| 33 |
+
end_time = time.time()
|
| 34 |
+
|
| 35 |
+
processing_time = end_time - start_time
|
| 36 |
+
times.append(f"{os.path.basename(uploaded_file)}: {processing_time:.2f} seconds")
|
| 37 |
+
results.append(img_output)
|
| 38 |
+
|
| 39 |
+
return results, "\n".join(times)
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("""
|
| 43 |
+
# AI Photo Background Removal
|
| 44 |
+
|
| 45 |
+
You want to remove your photo background, but don't have the time and effort to learn photo editing skills?
|
| 46 |
+
**This app will change or remove your photo background, in seconds.**
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
folder_input = gr.Files(type="filepath", label="Upload your photos here")
|
| 51 |
+
bg_color = gr.Dropdown(choices=["Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"], label="Choose background color")
|
| 52 |
+
|
| 53 |
+
output_images = gr.Gallery(label="Processed Images")
|
| 54 |
+
processing_times = gr.Textbox(label="Processing Times")
|
| 55 |
+
|
| 56 |
+
btn = gr.Button("Submit")
|
| 57 |
+
|
| 58 |
+
btn.click(fn=process_images, inputs=[folder_input, bg_color], outputs=[output_images, processing_times])
|
| 59 |
+
|
| 60 |
+
gr.Examples(
|
| 61 |
+
examples=[["assets/demo.jpg", "Transparent (PNG)"]],
|
| 62 |
+
inputs=[folder_input, bg_color]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch(share=True)
|