Spaces:
Sleeping
Sleeping
change background
Browse files- app.py +30 -3
- requirements.txt +4 -5
app.py
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import torchvision.transforms as transforms
|
| 7 |
|
|
@@ -18,6 +27,18 @@ def image_to_tensor(image):
|
|
| 18 |
])(image)
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def make_transparent_foreground(image, mask):
|
| 22 |
# split the image into channels
|
| 23 |
b, g, r = cv2.split(np.array(image).astype('uint8'))
|
|
@@ -52,9 +73,13 @@ def makeMask(image):
|
|
| 52 |
return np.where(mask, 255, background).astype(np.uint8)
|
| 53 |
|
| 54 |
|
| 55 |
-
def predict(image):
|
| 56 |
mask = makeMask(image)
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
title = "Zero Background"
|
|
@@ -78,9 +103,11 @@ This demo is running on a CPU, if you like this project please make us a donatio
|
|
| 78 |
<center><img src='https://visitor-badge.glitch.me/badge?page_id=deoldify.visitor-badge' alt='visitor badge'></center>
|
| 79 |
"""
|
| 80 |
|
|
|
|
| 81 |
demo = gr.Interface(
|
| 82 |
predict, [
|
| 83 |
gr.Image(type="pil", label="Image"),
|
|
|
|
| 84 |
], [
|
| 85 |
gr.Image(type="pil", label="Image alpha background")
|
| 86 |
],
|
|
|
|
| 1 |
+
############################################################################################################
|
| 2 |
+
#
|
| 3 |
+
# Source from
|
| 4 |
+
# https://github.com/eugenesiow/practical-ml/blob/master/notebooks/Remove_Image_Background_DeepLabV3.ipynb
|
| 5 |
+
#
|
| 6 |
+
############################################################################################################
|
| 7 |
+
|
| 8 |
import os
|
| 9 |
+
|
| 10 |
import cv2
|
|
|
|
| 11 |
import torch
|
| 12 |
+
import PIL.Image
|
| 13 |
+
import numpy as np
|
| 14 |
import gradio as gr
|
| 15 |
import torchvision.transforms as transforms
|
| 16 |
|
|
|
|
| 27 |
])(image)
|
| 28 |
|
| 29 |
|
| 30 |
+
def custom_background(background, foreground):
|
| 31 |
+
x = (background.size[0] - foreground.size[0]) / 2
|
| 32 |
+
y = (background.size[1] - foreground.size[1]) / 2
|
| 33 |
+
box = (x, y, foreground.size[0] + x, foreground.size[1] + y)
|
| 34 |
+
crop = background.crop(box)
|
| 35 |
+
final_image = crop.copy()
|
| 36 |
+
# put the foreground in the centre of the background
|
| 37 |
+
paste_box = (0, final_image.size[1] - foreground.size[1], final_image.size[0], final_image.size[1])
|
| 38 |
+
final_image.paste(foreground, paste_box, mask=foreground)
|
| 39 |
+
return final_image
|
| 40 |
+
|
| 41 |
+
|
| 42 |
def make_transparent_foreground(image, mask):
|
| 43 |
# split the image into channels
|
| 44 |
b, g, r = cv2.split(np.array(image).astype('uint8'))
|
|
|
|
| 73 |
return np.where(mask, 255, background).astype(np.uint8)
|
| 74 |
|
| 75 |
|
| 76 |
+
def predict(image, new_background=None):
|
| 77 |
mask = makeMask(image)
|
| 78 |
+
foreground = make_transparent_foreground(image, mask)
|
| 79 |
+
if new_background is not None:
|
| 80 |
+
foreground = PIL.Image.fromarray(foreground)
|
| 81 |
+
return custom_background(new_background, foreground)
|
| 82 |
+
return foreground
|
| 83 |
|
| 84 |
|
| 85 |
title = "Zero Background"
|
|
|
|
| 103 |
<center><img src='https://visitor-badge.glitch.me/badge?page_id=deoldify.visitor-badge' alt='visitor badge'></center>
|
| 104 |
"""
|
| 105 |
|
| 106 |
+
|
| 107 |
demo = gr.Interface(
|
| 108 |
predict, [
|
| 109 |
gr.Image(type="pil", label="Image"),
|
| 110 |
+
gr.Image(type="pil", label="Optionally: Set a new background")
|
| 111 |
], [
|
| 112 |
gr.Image(type="pil", label="Image alpha background")
|
| 113 |
],
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
torch>=2.0.1
|
| 2 |
-
torchvision
|
| 3 |
-
opencv-python
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 1 |
torch>=2.0.1
|
| 2 |
+
torchvision~=0.15.2
|
| 3 |
+
opencv-python~=4.7.0.72
|
| 4 |
+
numpy~=1.24.3
|
| 5 |
+
pillow~=9.5.0
|
|
|