Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,25 @@ 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 |
-
|
15 |
|
16 |
hexmap = {
|
17 |
"Transparent (PNG)": "#000000",
|
@@ -38,6 +47,33 @@ def process_images(uploaded_files, background_color):
|
|
38 |
|
39 |
return results, "\n".join(times)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
with gr.Blocks() as demo:
|
42 |
gr.Markdown("""
|
43 |
# AI Photo Background Removal
|
@@ -48,7 +84,7 @@ with gr.Blocks() as demo:
|
|
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")
|
@@ -63,4 +99,4 @@ with gr.Blocks() as demo:
|
|
63 |
)
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
-
demo.launch(
|
|
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import time
|
5 |
+
import json
|
6 |
+
from fastapi import FastAPI
|
7 |
+
from pydantic import BaseModel
|
8 |
|
9 |
from src.utils import change_background, matte
|
10 |
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
class ImageRequest(BaseModel):
|
14 |
+
image: str # Base64 encoded image
|
15 |
+
background_color: str
|
16 |
+
|
17 |
def process_images(uploaded_files, background_color):
|
18 |
# Add debugging statement
|
19 |
print(f"background_color received: {background_color}")
|
20 |
|
21 |
# Default value check
|
22 |
if background_color is None:
|
23 |
+
background_color = "Transparent (PNG)"
|
24 |
|
25 |
hexmap = {
|
26 |
"Transparent (PNG)": "#000000",
|
|
|
47 |
|
48 |
return results, "\n".join(times)
|
49 |
|
50 |
+
@app.post("/process")
|
51 |
+
def api_process_images(request: ImageRequest):
|
52 |
+
image_data = request.image
|
53 |
+
background_color = request.background_color
|
54 |
+
|
55 |
+
# Convert base64 to PIL image
|
56 |
+
img_input = Image.open(BytesIO(base64.b64decode(image_data)))
|
57 |
+
|
58 |
+
hexmap = {
|
59 |
+
"Transparent (PNG)": "#000000",
|
60 |
+
"Black": "#000000",
|
61 |
+
"White": "#FFFFFF",
|
62 |
+
"Green": "#22EE22",
|
63 |
+
"Red": "#EE2222",
|
64 |
+
"Blue": "#2222EE",
|
65 |
+
}
|
66 |
+
alpha = 0.0 if background_color == "Transparent (PNG)" else 1.0
|
67 |
+
|
68 |
+
img_matte = matte(img_input)
|
69 |
+
img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[background_color])
|
70 |
+
|
71 |
+
buffered = BytesIO()
|
72 |
+
img_output.save(buffered, format="PNG")
|
73 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
74 |
+
|
75 |
+
return {"image": img_str}
|
76 |
+
|
77 |
with gr.Blocks() as demo:
|
78 |
gr.Markdown("""
|
79 |
# AI Photo Background Removal
|
|
|
84 |
|
85 |
with gr.Row():
|
86 |
folder_input = gr.Files(type="filepath", label="Upload your photos here")
|
87 |
+
bg_color = gr.Dropdown(choices=["Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"], label="Choose background color", value="Transparent (PNG)")
|
88 |
|
89 |
output_images = gr.Gallery(label="Processed Images")
|
90 |
processing_times = gr.Textbox(label="Processing Times")
|
|
|
99 |
)
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
+
demo.launch()
|