Delete app1.py
Browse files
app1.py
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from rembg import remove
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
def separate_foreground_background(image):
|
| 7 |
-
# Remove the background using rembg
|
| 8 |
-
output_image = remove(image)
|
| 9 |
-
|
| 10 |
-
# Convert images to numpy arrays
|
| 11 |
-
input_rgb = np.array(image)[:, :, :3] # Keep only RGB channels
|
| 12 |
-
output_rgba = np.array(output_image) # Include alpha channel
|
| 13 |
-
|
| 14 |
-
# Extract the alpha channel
|
| 15 |
-
alpha = output_rgba[:, :, 3]
|
| 16 |
-
|
| 17 |
-
# Create a 3-channel alpha mask
|
| 18 |
-
alpha3 = np.dstack((alpha, alpha, alpha))
|
| 19 |
-
|
| 20 |
-
# Calculate the background
|
| 21 |
-
background_rgb = input_rgb.astype(np.float32) * (1 - alpha3.astype(np.float32) / 255)
|
| 22 |
-
background_rgb = background_rgb.astype(np.uint8)
|
| 23 |
-
|
| 24 |
-
# Convert numpy arrays back to PIL Images
|
| 25 |
-
foreground = Image.fromarray(output_rgba) # Foreground with transparency
|
| 26 |
-
background = Image.fromarray(background_rgb) # Background image
|
| 27 |
-
|
| 28 |
-
return foreground, background
|
| 29 |
-
|
| 30 |
-
# Gradio interface setup
|
| 31 |
-
gr.Interface(
|
| 32 |
-
fn=separate_foreground_background,
|
| 33 |
-
inputs=gr.Image(label="Upload Image"),
|
| 34 |
-
outputs=[gr.Image(label="Foreground"), gr.Image(label="Background")],
|
| 35 |
-
title="Foreground and Background Separation"
|
| 36 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|