peterkros commited on
Commit
7811a63
·
1 Parent(s): a4dc984

new version

Browse files
.streamlit/config.toml DELETED
@@ -1,6 +0,0 @@
1
- [server]
2
- maxUploadSize = 10
3
-
4
- [theme]
5
- base="light"
6
- primaryColor="#0074ff"
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,80 +1,55 @@
1
- import streamlit as st
2
- import os
3
- from datetime import datetime
4
  from PIL import Image
5
  from io import BytesIO
6
 
7
  from src.utils import change_background, matte
8
- from src.st_style import apply_prod_style
9
-
10
- # apply_prod_style(st) # NOTE: Uncomment this for production!
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- def image_download_button(pil_image, filename: str, fmt: str, label="Download"):
14
- if fmt not in ["jpg", "png"]:
15
- raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")
 
 
 
 
 
 
 
 
16
 
17
- pil_format = "JPEG" if fmt == "jpg" else "PNG"
18
- file_format = "jpg" if fmt == "jpg" else "png"
19
- mime = "image/jpeg" if fmt == "jpg" else "image/png"
20
 
21
- buf = BytesIO()
22
- pil_image.save(buf, format=pil_format)
23
 
24
- return st.download_button(
25
- label=label,
26
- data=buf.getvalue(),
27
- file_name=f'{filename}.{file_format}',
28
- mime=mime,
29
  )
30
 
31
-
32
- st.title("AI Photo Background Removal")
33
- st.image(Image.open("assets/demo.jpg"))
34
- st.write(
35
- """
36
- You want to remove your photo background, but don't have the time and effort to learn photo editing skills?
37
- **This app will change or remove your photo background, in seconds.**
38
- """
39
- )
40
-
41
- uploaded_file = st.file_uploader(
42
- label="Upload your photo here",
43
- accept_multiple_files=False, type=["png", "jpg", "jpeg"],
44
- )
45
-
46
- if uploaded_file is not None:
47
-
48
- with st.expander("Original photo", expanded=True):
49
- if uploaded_file is not None:
50
- st.image(uploaded_file)
51
- else:
52
- st.warning("You haven't uploaded any photo yet")
53
-
54
- in_mode = st.selectbox("Choose background color", ["Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"])
55
- in_submit = st.button("Submit")
56
-
57
- if uploaded_file is not None and in_submit:
58
- img_input = Image.open(uploaded_file)
59
-
60
- with st.spinner("AI is doing magic to your photo. Please wait..."):
61
- hexmap = {
62
- "Transparent (PNG)": "#000000",
63
- "Black": "#000000",
64
- "White": "#FFFFFF",
65
- "Green": "#22EE22",
66
- "Red": "#EE2222",
67
- "Blue": "#2222EE",
68
- }
69
- alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0
70
- img_matte = matte(img_input)
71
- img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[in_mode])
72
-
73
- with st.expander("Success!", expanded=True):
74
- st.image(img_output)
75
- uploaded_name = os.path.splitext(uploaded_file.name)[0]
76
- image_download_button(
77
- pil_image=img_output,
78
- filename=uploaded_name,
79
- fmt="png"
80
- )
 
1
+ import gradio as gr
 
 
2
  from PIL import Image
3
  from io import BytesIO
4
 
5
  from src.utils import change_background, matte
 
 
 
6
 
7
+ def process_image(uploaded_file, background_color):
8
+ # Add debugging statement
9
+ print(f"background_color received: {background_color}")
10
+
11
+ # Default value check
12
+ if background_color is None:
13
+ return "Please select a background color."
14
+
15
+ hexmap = {
16
+ "Transparent (PNG)": "#000000",
17
+ "Black": "#000000",
18
+ "White": "#FFFFFF",
19
+ "Green": "#22EE22",
20
+ "Red": "#EE2222",
21
+ "Blue": "#2222EE",
22
+ }
23
+ alpha = 0.0 if background_color == "Transparent (PNG)" else 1.0
24
+
25
+ img_input = uploaded_file # No need to open, it's already a PIL image
26
+ img_matte = matte(img_input)
27
+ img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[background_color])
28
+
29
+ return img_output
30
 
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("""
33
+ # AI Photo Background Removal
34
+
35
+ You want to remove your photo background, but don't have the time and effort to learn photo editing skills?
36
+ **This app will change or remove your photo background, in seconds.**
37
+ """)
38
+
39
+ with gr.Row():
40
+ image_input = gr.Image(type="pil", label="Upload your photo here")
41
+ bg_color = gr.Dropdown(choices=["Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"], label="Choose background color")
42
 
43
+ output_image = gr.Image(type="pil", label="Processed Image")
 
 
44
 
45
+ btn = gr.Button("Submit")
 
46
 
47
+ btn.click(fn=process_image, inputs=[image_input, bg_color], outputs=output_image)
48
+
49
+ gr.Examples(
50
+ examples=[["assets/demo.jpg", "Transparent (PNG)"]],
51
+ inputs=[image_input, bg_color]
52
  )
53
 
54
+ if __name__ == "__main__":
55
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
assets/demo.jpg DELETED
Binary file (34.4 kB)
 
assets/test_character.png ADDED
requirements.txt CHANGED
@@ -3,5 +3,5 @@ torchvision
3
  numpy
4
  opencv-python-headless
5
  matplotlib
6
- streamlit
7
- altair<5
 
3
  numpy
4
  opencv-python-headless
5
  matplotlib
6
+ altair<5
7
+ gradio