Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
-
# app.py (Streamlit version)
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import torchvision.transforms as transforms
|
| 6 |
import numpy as np
|
| 7 |
-
import sys
|
| 8 |
import subprocess
|
| 9 |
|
| 10 |
-
# Install
|
| 11 |
try:
|
| 12 |
import pytorch_hub_examples
|
| 13 |
-
except ModuleNotFoundError:
|
| 14 |
subprocess.run(["git", "clone", "https://github.com/facebookresearch/pytorch_hub_examples.git"])
|
| 15 |
-
subprocess.run(["cd", "pytorch_hub_examples", "&&", "
|
| 16 |
import pytorch_hub_examples
|
| 17 |
|
| 18 |
# Load the U-2-Net model
|
|
@@ -21,19 +20,18 @@ model.eval()
|
|
| 21 |
|
| 22 |
# Define the transform
|
| 23 |
transform = transforms.Compose([
|
| 24 |
-
transforms.Resize((320, 320)),
|
| 25 |
transforms.ToTensor(),
|
| 26 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 27 |
])
|
| 28 |
|
| 29 |
-
|
| 30 |
def remove_background(image):
|
| 31 |
try:
|
| 32 |
img = transform(image).unsqueeze(0)
|
| 33 |
|
| 34 |
with torch.no_grad():
|
| 35 |
out = model(img)
|
| 36 |
-
mask = torch.sigmoid(out[0])
|
| 37 |
|
| 38 |
mask = mask.squeeze().cpu().numpy()
|
| 39 |
mask = (mask * 255).astype(np.uint8)
|
|
@@ -49,20 +47,19 @@ def remove_background(image):
|
|
| 49 |
|
| 50 |
return new_image
|
| 51 |
except Exception as e:
|
| 52 |
-
st.error(f"Error: {e}")
|
| 53 |
-
return None
|
| 54 |
-
|
| 55 |
|
| 56 |
st.title("Background Remover")
|
| 57 |
|
| 58 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 59 |
|
| 60 |
if uploaded_file is not None:
|
| 61 |
-
image = Image.open(uploaded_file).convert("RGB")
|
| 62 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 63 |
|
| 64 |
if st.button("Remove Background"):
|
| 65 |
-
with st.spinner("Removing background..."):
|
| 66 |
result_image = remove_background(image)
|
| 67 |
if result_image:
|
| 68 |
st.image(result_image, caption="Background Removed", use_column_width=True)
|
|
|
|
| 1 |
+
# app.py (Streamlit version - Corrected)
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import torchvision.transforms as transforms
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
import subprocess
|
| 8 |
|
| 9 |
+
# Install pytorch_hub_examples package (Corrected)
|
| 10 |
try:
|
| 11 |
import pytorch_hub_examples
|
| 12 |
+
except ModuleNotFoundError:
|
| 13 |
subprocess.run(["git", "clone", "https://github.com/facebookresearch/pytorch_hub_examples.git"])
|
| 14 |
+
subprocess.run(["cd", "pytorch_hub_examples", "&&", "python", "setup.py", "install"]) # Correct installation
|
| 15 |
import pytorch_hub_examples
|
| 16 |
|
| 17 |
# Load the U-2-Net model
|
|
|
|
| 20 |
|
| 21 |
# Define the transform
|
| 22 |
transform = transforms.Compose([
|
| 23 |
+
transforms.Resize((320, 320)),
|
| 24 |
transforms.ToTensor(),
|
| 25 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 26 |
])
|
| 27 |
|
|
|
|
| 28 |
def remove_background(image):
|
| 29 |
try:
|
| 30 |
img = transform(image).unsqueeze(0)
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
out = model(img)
|
| 34 |
+
mask = torch.sigmoid(out[0])
|
| 35 |
|
| 36 |
mask = mask.squeeze().cpu().numpy()
|
| 37 |
mask = (mask * 255).astype(np.uint8)
|
|
|
|
| 47 |
|
| 48 |
return new_image
|
| 49 |
except Exception as e:
|
| 50 |
+
st.error(f"Error: {e}")
|
| 51 |
+
return None
|
|
|
|
| 52 |
|
| 53 |
st.title("Background Remover")
|
| 54 |
|
| 55 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 56 |
|
| 57 |
if uploaded_file is not None:
|
| 58 |
+
image = Image.open(uploaded_file).convert("RGB") # Ensure RGB for transform
|
| 59 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 60 |
|
| 61 |
if st.button("Remove Background"):
|
| 62 |
+
with st.spinner("Removing background..."):
|
| 63 |
result_image = remove_background(image)
|
| 64 |
if result_image:
|
| 65 |
st.image(result_image, caption="Background Removed", use_column_width=True)
|