requirements.txt
Browse filesstreamlit
rembg
pillow
numpy
requests
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from rembg import remove
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def process_image(image):
|
| 10 |
+
output = remove(image)
|
| 11 |
+
|
| 12 |
+
input_rgb = np.array(image)[:, :, 0:3]
|
| 13 |
+
output_rgba = np.array(output)
|
| 14 |
+
|
| 15 |
+
alpha = output_rgba[:, :, 3]
|
| 16 |
+
alpha3 = np.dstack((alpha, alpha, alpha))
|
| 17 |
+
|
| 18 |
+
background_rgb = input_rgb.astype(float) * (1 - alpha3.astype(float) / 255)
|
| 19 |
+
background_rgb = background_rgb.astype(np.uint8)
|
| 20 |
+
|
| 21 |
+
background = Image.fromarray(background_rgb)
|
| 22 |
+
return output, background
|
| 23 |
+
|
| 24 |
+
def display_images(image):
|
| 25 |
+
img1, img2 = process_image(image)
|
| 26 |
+
|
| 27 |
+
col3, col4 = st.columns(2)
|
| 28 |
+
with col3:
|
| 29 |
+
st.image(img1, caption='Foreground Extraction', use_column_width=True)
|
| 30 |
+
|
| 31 |
+
with col4:
|
| 32 |
+
st.image(img2, caption='Background Extraction', use_column_width=True)
|
| 33 |
+
|
| 34 |
+
def main():
|
| 35 |
+
st.title("Foreground & Background Extraction")
|
| 36 |
+
images = {
|
| 37 |
+
"Sample 1": "https://images2.alphacoders.com/931/931778.jpg",
|
| 38 |
+
"Sample 2": "https://64.media.tumblr.com/862739618bd130769be6efed4d2b8841/63e31bdeb0842a99-ee/s1280x1920/49eb215c37a2235c915ded605f0ebcb81962af6c.jpg"
|
| 39 |
+
}
|
| 40 |
+
col1, col2 = st.columns(2)
|
| 41 |
+
sample_keys = list(images.keys())
|
| 42 |
+
with col1:
|
| 43 |
+
st.image(f"{images.get(sample_keys[0])}", caption=f"{sample_keys[0]}", width = 300)
|
| 44 |
+
|
| 45 |
+
with col2:
|
| 46 |
+
st.image(f"{images.get(sample_keys[1])}", caption=f"{sample_keys[1]}", width = 150)
|
| 47 |
+
option = st.radio(
|
| 48 |
+
"Choose an option:",
|
| 49 |
+
("Upload an Image", f"{sample_keys[0]}", f"{sample_keys[1]}")
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if option in images:
|
| 53 |
+
# sample_image = Image.open(images[option])
|
| 54 |
+
response = requests.get(images[option])
|
| 55 |
+
sample_image = Image.open(BytesIO(response.content))
|
| 56 |
+
st.image(sample_image, caption=f'{option}', use_column_width=True)
|
| 57 |
+
display_images(sample_image)
|
| 58 |
+
|
| 59 |
+
elif option == "Upload an Image":
|
| 60 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 61 |
+
|
| 62 |
+
if uploaded_file is not None:
|
| 63 |
+
original_image = Image.open(uploaded_file)
|
| 64 |
+
st.image(original_image, caption='Uploaded Image', use_column_width=True)
|
| 65 |
+
display_images(original_image)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
main()
|