Upload 2 files
Browse files- app.py +94 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from fire import Fire
|
| 5 |
+
|
| 6 |
+
from s3od import BackgroundRemoval
|
| 7 |
+
from s3od.visualizer import visualize_removal
|
| 8 |
+
|
| 9 |
+
detector = BackgroundRemoval()
|
| 10 |
+
|
| 11 |
+
VISUALIZATION_METHODS = {
|
| 12 |
+
'Transparent Background': 'transparent',
|
| 13 |
+
'White Background': 'white',
|
| 14 |
+
'Green Background': 'green',
|
| 15 |
+
'Mask Only': 'mask'
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def process_image(image, method, threshold):
|
| 20 |
+
if image is None:
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
result = detector.remove_background(image, threshold=threshold)
|
| 24 |
+
|
| 25 |
+
if method == 'transparent':
|
| 26 |
+
return result.rgba_image
|
| 27 |
+
elif method == 'white':
|
| 28 |
+
return visualize_removal(image, result, background_color=(255, 255, 255))
|
| 29 |
+
elif method == 'green':
|
| 30 |
+
return visualize_removal(image, result, background_color=(0, 255, 0))
|
| 31 |
+
elif method == 'mask':
|
| 32 |
+
mask_vis = (result.predicted_mask * 255).astype(np.uint8)
|
| 33 |
+
return Image.fromarray(mask_vis, mode='L')
|
| 34 |
+
|
| 35 |
+
return result.rgba_image
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=process_image,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Image(type="numpy", label="Upload an Image"),
|
| 42 |
+
gr.Radio(
|
| 43 |
+
list(VISUALIZATION_METHODS.keys()),
|
| 44 |
+
label="Output Format",
|
| 45 |
+
value='Transparent Background'
|
| 46 |
+
),
|
| 47 |
+
gr.Slider(
|
| 48 |
+
minimum=0.0,
|
| 49 |
+
maximum=1.0,
|
| 50 |
+
value=0.5,
|
| 51 |
+
step=0.05,
|
| 52 |
+
label="Mask Threshold"
|
| 53 |
+
)
|
| 54 |
+
],
|
| 55 |
+
outputs=gr.Image(type="pil", label="Result"),
|
| 56 |
+
title="Demo: S3OD - Synthetic Salient Object Detection",
|
| 57 |
+
description="""
|
| 58 |
+
Upload an image to remove its background using **S3OD**!
|
| 59 |
+
|
| 60 |
+
S3OD is trained on a large-scale fully synthetic dataset (140K+ images) generated with diffusion models.
|
| 61 |
+
Despite being trained only on synthetic data, it achieves state-of-the-art performance on real-world images.
|
| 62 |
+
|
| 63 |
+
The model uses a DPT-based architecture with DINOv3 vision transformer backbone for robust salient object detection
|
| 64 |
+
and can process images of any size. Choose from four visualization methods: transparent background (RGBA),
|
| 65 |
+
white background, green background (chroma key), or mask only.
|
| 66 |
+
|
| 67 |
+
**Key Features:**
|
| 68 |
+
- Single-step background removal
|
| 69 |
+
- Multi-mask prediction with IoU scoring
|
| 70 |
+
- Adjustable threshold for fine-tuning
|
| 71 |
+
- Works on any image resolution
|
| 72 |
+
|
| 73 |
+
Ideal for applications in e-commerce, content creation, photo editing, and computer vision research.
|
| 74 |
+
|
| 75 |
+
📄 [Paper](https://arxiv.org/abs/XXXX.XXXXX) | 💻 [GitHub](https://github.com/KupynOrest/s3od) | 🤗 [Model](https://huggingface.co/okupyn/s3od) | 🗂️ [Dataset](https://huggingface.co/datasets/okupyn/s3od_dataset)
|
| 76 |
+
""",
|
| 77 |
+
allow_flagging='never',
|
| 78 |
+
examples=[
|
| 79 |
+
# Add example images here when available
|
| 80 |
+
]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def main(server_name="0.0.0.0", server_port=7860, share=False):
|
| 85 |
+
iface.launch(
|
| 86 |
+
server_name=server_name,
|
| 87 |
+
server_port=server_port,
|
| 88 |
+
share=share
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == '__main__':
|
| 93 |
+
Fire(main)
|
| 94 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/KupynOrest/s3od.git
|
| 2 |
+
gradio>=4.0.0
|
| 3 |
+
fire>=0.5.0
|
| 4 |
+
numpy>=1.20.0
|
| 5 |
+
Pillow>=9.0.0
|
| 6 |
+
|