Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transparent_background import Remover
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Initialize the model globally
|
| 7 |
+
remover = Remover()
|
| 8 |
+
|
| 9 |
+
def process_image(input_image, torchscript_jit, output_type):
|
| 10 |
+
global remover
|
| 11 |
+
if torchscript_jit == "on" and not isinstance(remover, Remover(jit=True).__class__):
|
| 12 |
+
remover = Remover(jit=True)
|
| 13 |
+
elif torchscript_jit == "default" and not isinstance(remover, Remover().__class__):
|
| 14 |
+
remover = Remover()
|
| 15 |
+
|
| 16 |
+
if output_type == "Mask only":
|
| 17 |
+
# Process the image and get only the mask
|
| 18 |
+
output = remover.process(input_image, type='map')
|
| 19 |
+
# Convert the mask to a black and white image
|
| 20 |
+
mask = Image.fromarray((output * 255).astype(np.uint8))
|
| 21 |
+
return mask
|
| 22 |
+
else:
|
| 23 |
+
# Process the image and return the RGBA result
|
| 24 |
+
output = remover.process(input_image, type='rgba')
|
| 25 |
+
return output
|
| 26 |
+
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=process_image,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Image(type="pil", label="Input Image"),
|
| 31 |
+
gr.Radio(["default", "on"], label="TorchScript JIT", value="default"),
|
| 32 |
+
gr.Radio(["Default", "Mask only"], label="Output Type", value="Default")
|
| 33 |
+
],
|
| 34 |
+
outputs=gr.Image(type="pil", label="Output Image"),
|
| 35 |
+
title="Inspyrenet Background Remover",
|
| 36 |
+
description="Remove the background from an image using Inspyrenet. Choose 'Mask only' for a black and white mask, or 'Default' for the image with transparent background.",
|
| 37 |
+
theme='bethecloud/storj_theme',
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch()
|