Spaces:
Running
on
T4
Running
on
T4
Commit
·
d5eecf2
1
Parent(s):
fc8aac9
Remove Gradio path; run Streamlit UI only
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@
|
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
import streamlit as st
|
| 7 |
-
import gradio as gr
|
| 8 |
import os
|
| 9 |
from datetime import datetime
|
| 10 |
from PIL import Image
|
|
@@ -42,6 +41,7 @@ if "color_to_label" not in st.session_state:
|
|
| 42 |
|
| 43 |
if 'reuse_image' not in st.session_state:
|
| 44 |
st.session_state.reuse_image = None
|
|
|
|
| 45 |
def set_image(img):
|
| 46 |
st.session_state.reuse_image = img
|
| 47 |
|
|
@@ -120,14 +120,12 @@ def run_streamlit_ui():
|
|
| 120 |
im[drawing]=[0,0,0,0] # RGBA
|
| 121 |
|
| 122 |
if st.button('Submit'):
|
| 123 |
-
|
| 124 |
with st.spinner("AI is doing the magic!"):
|
| 125 |
-
output = process_inpaint(np.array(img_input), np.array(im))
|
| 126 |
img_output = Image.fromarray(output).convert("RGB")
|
| 127 |
|
| 128 |
st.write("AI has finished the job!")
|
| 129 |
st.image(img_output)
|
| 130 |
-
# reuse = st.button('Edit again (Re-use this image)', on_click=set_image, args=(inpainted_img, ))
|
| 131 |
|
| 132 |
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
| 133 |
image_download_button(
|
|
@@ -141,49 +139,7 @@ def run_streamlit_ui():
|
|
| 141 |
"upload then remove the artifacts.")
|
| 142 |
|
| 143 |
|
| 144 |
-
def _prepare_mask_rgba(image: Image.Image, mask: np.ndarray) -> np.ndarray:
|
| 145 |
-
if mask is None:
|
| 146 |
-
rgba = np.zeros((image.height, image.width, 4), dtype=np.uint8)
|
| 147 |
-
rgba[:, :, 3] = 255
|
| 148 |
-
return rgba
|
| 149 |
-
mask_img = Image.fromarray(mask).convert("L").resize(image.size)
|
| 150 |
-
mask_np = np.array(mask_img)
|
| 151 |
-
rgba = np.zeros((image.height, image.width, 4), dtype=np.uint8)
|
| 152 |
-
rgba[:, :, :3] = 0
|
| 153 |
-
rgba[:, :, 3] = 255
|
| 154 |
-
rgba[mask_np > 0, 3] = 0
|
| 155 |
-
return rgba
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
def gradio_inpaint(data):
|
| 159 |
-
if data is None:
|
| 160 |
-
return None
|
| 161 |
-
img = data.get("image") if isinstance(data, dict) else data
|
| 162 |
-
mask = data.get("mask") if isinstance(data, dict) else None
|
| 163 |
-
if img is None:
|
| 164 |
-
return None
|
| 165 |
-
if not isinstance(img, Image.Image):
|
| 166 |
-
img = Image.fromarray(img)
|
| 167 |
-
img_rgba = img.convert("RGBA")
|
| 168 |
-
rgba_mask = _prepare_mask_rgba(img_rgba, mask)
|
| 169 |
-
output = process_inpaint(np.array(img_rgba), rgba_mask)
|
| 170 |
-
return Image.fromarray(output).convert("RGB")
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
with gr.Blocks() as demo:
|
| 174 |
-
gr.Markdown("# AI Photo Object Removal")
|
| 175 |
-
gr.Markdown("Draw on the image to remove objects. Use the Sketch tool to paint the mask.")
|
| 176 |
-
with gr.Row():
|
| 177 |
-
input_image = gr.Image(label="Image", type="pil", tool="sketch", image_mode="RGBA", sources=["upload"])
|
| 178 |
-
run_btn = gr.Button("Remove Object")
|
| 179 |
-
output_image = gr.Image(label="Result", type="pil")
|
| 180 |
-
run_btn.click(gradio_inpaint, inputs=input_image, outputs=output_image)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
if __name__ == "__main__":
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
demo.queue().launch()
|
| 188 |
-
else:
|
| 189 |
-
run_streamlit_ui()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
import streamlit as st
|
|
|
|
| 7 |
import os
|
| 8 |
from datetime import datetime
|
| 9 |
from PIL import Image
|
|
|
|
| 41 |
|
| 42 |
if 'reuse_image' not in st.session_state:
|
| 43 |
st.session_state.reuse_image = None
|
| 44 |
+
|
| 45 |
def set_image(img):
|
| 46 |
st.session_state.reuse_image = img
|
| 47 |
|
|
|
|
| 120 |
im[drawing]=[0,0,0,0] # RGBA
|
| 121 |
|
| 122 |
if st.button('Submit'):
|
|
|
|
| 123 |
with st.spinner("AI is doing the magic!"):
|
| 124 |
+
output = process_inpaint(np.array(img_input), np.array(im))
|
| 125 |
img_output = Image.fromarray(output).convert("RGB")
|
| 126 |
|
| 127 |
st.write("AI has finished the job!")
|
| 128 |
st.image(img_output)
|
|
|
|
| 129 |
|
| 130 |
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
| 131 |
image_download_button(
|
|
|
|
| 139 |
"upload then remove the artifacts.")
|
| 140 |
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
if __name__ == "__main__":
|
| 143 |
+
# Streamlit Spaces run the file with `streamlit run app.py`
|
| 144 |
+
# So keep a safe guard for direct execution as well
|
| 145 |
+
run_streamlit_ui()
|
|
|
|
|
|
|
|
|