Tyler Ng commited on
Commit
7f42e2b
·
verified ·
1 Parent(s): 49352fe

init commit

Browse files

---
title: InSPyReNet Background Remover
emoji: 🖼️
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: mit
---

# InSPyReNet Background Remover

This Space uses InSPyReNet for high-quality background removal and image masking.

## Features

- **Transparent Background (RGBA)**: Remove background and get PNG with transparency
- **Green Background**: Replace background with green screen
- **White Background**: Replace background with white color
- **Blur Background**: Keep background but blur it
- **Mask Only**: Get the raw saliency mask (grayscale)
- **Overlay**: Highlight detected object with edges

## Credits

- [InSPyReNet](https://github.com/plemeri/InSPyReNet) - ACCV 2022
- [transparent-background](https://github.com/plemeri/transparent-background) - Python package

Files changed (3) hide show
  1. README.md +29 -14
  2. app.py +108 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,14 +1,29 @@
1
- ---
2
- title: Inspyrenet Background Remove
3
- emoji: 🐨
4
- colorFrom: indigo
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 6.2.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: Using Inspyrenet to remove backgrounds
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: InSPyReNet Background Remover
3
+ emoji: 🖼️
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # InSPyReNet Background Remover
14
+
15
+ This Space uses InSPyReNet for high-quality background removal and image masking.
16
+
17
+ ## Features
18
+
19
+ - **Transparent Background (RGBA)**: Remove background and get PNG with transparency
20
+ - **Green Background**: Replace background with green screen
21
+ - **White Background**: Replace background with white color
22
+ - **Blur Background**: Keep background but blur it
23
+ - **Mask Only**: Get the raw saliency mask (grayscale)
24
+ - **Overlay**: Highlight detected object with edges
25
+
26
+ ## Credits
27
+
28
+ - [InSPyReNet](https://github.com/plemeri/InSPyReNet) - ACCV 2022
29
+ - [transparent-background](https://github.com/plemeri/transparent-background) - Python package
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from transparent_background import Remover
5
+ import numpy as np
6
+
7
+ # Initialize the model globally
8
+ remover = Remover(jit=False)
9
+
10
+ @spaces.GPU
11
+ def process_image(input_image, output_type):
12
+ """
13
+ Process image with different output types:
14
+ - Transparent Background (RGBA): Shows masked image with transparent background
15
+ - Green Background: Shows masked image with green screen
16
+ - White Background: Shows masked image with white background
17
+ - Blur Background: Shows image with blurred background
18
+ - Mask Only: Shows the grayscale saliency mask
19
+ - Overlay: Shows object highlighted with edges on original image
20
+ """
21
+ global remover
22
+
23
+ if input_image is None:
24
+ return None
25
+
26
+ if output_type == "Transparent Background (RGBA)":
27
+ # Returns the masked image with transparent background
28
+ output = remover.process(input_image, type='rgba')
29
+
30
+ elif output_type == "Green Background":
31
+ # Returns the masked image with green screen background
32
+ output = remover.process(input_image, type='green')
33
+
34
+ elif output_type == "White Background":
35
+ # Returns the masked image with white background
36
+ output = remover.process(input_image, type='white')
37
+
38
+ elif output_type == "Blur Background":
39
+ # Returns the image with blurred background
40
+ output = remover.process(input_image, type='blur')
41
+
42
+ elif output_type == "Mask Only":
43
+ # Returns only the grayscale saliency mask
44
+ output = remover.process(input_image, type='map')
45
+ # Convert to grayscale if needed
46
+ if isinstance(output, Image.Image):
47
+ output = output.convert('L')
48
+ else:
49
+ output = Image.fromarray((output * 255).astype(np.uint8), mode='L')
50
+
51
+ elif output_type == "Overlay":
52
+ # Returns image with salient object highlighted
53
+ output = remover.process(input_image, type='overlay')
54
+
55
+ else:
56
+ # Default: Transparent background
57
+ output = remover.process(input_image, type='rgba')
58
+
59
+ return output
60
+
61
+ # Description with links
62
+ description = """
63
+ <h1 align="center">InSPyReNet Background Remover</h1>
64
+ <p align="center">
65
+ <a href="https://github.com/plemeri/InSPyReNet" target="_blank">[GitHub - InSPyReNet]</a>
66
+ <a href="https://github.com/plemeri/transparent-background" target="_blank">[GitHub - transparent-background]</a>
67
+ </p>
68
+ <p align="center">
69
+ Powered by InSPyReNet (ACCV 2022) - High Resolution Salient Object Detection
70
+ </p>
71
+ """
72
+
73
+ # Output type choices with descriptions
74
+ output_choices = [
75
+ "Transparent Background (RGBA)", # Actual masked image with transparency
76
+ "Green Background", # Masked image on green screen
77
+ "White Background", # Masked image on white
78
+ "Blur Background", # Original with blurred background
79
+ "Mask Only", # Just the saliency mask
80
+ "Overlay" # Highlighted edges on original
81
+ ]
82
+
83
+ # Create the Gradio interface
84
+ iface = gr.Interface(
85
+ fn=process_image,
86
+ inputs=[
87
+ gr.Image(type="pil", label="Input Image", height=512),
88
+ gr.Radio(
89
+ choices=output_choices,
90
+ label="Output Type",
91
+ value="Transparent Background (RGBA)",
92
+ info="Select how you want the masked image displayed"
93
+ )
94
+ ],
95
+ outputs=gr.Image(type="pil", label="Output Image", height=512),
96
+ description=description,
97
+ theme='bethecloud/storj_theme',
98
+ examples=[
99
+ ["1.png", "Transparent Background (RGBA)"],
100
+ ["1.png", "Green Background"],
101
+ ["2.png", "White Background"],
102
+ ["2.png", "Mask Only"],
103
+ ],
104
+ cache_examples=False # Set to True after adding example images
105
+ )
106
+
107
+ if __name__ == "__main__":
108
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ transparent-background
3
+ numpy
4
+ Pillow
5
+ torch
6
+ torchvision