Spaces:
Sleeping
Sleeping
init app
Browse files- .gitignore +3 -0
- app.py +89 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.idea/
|
2 |
+
__pycache__/
|
3 |
+
|
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
os.system("pip freeze")
|
9 |
+
|
10 |
+
model = torch.hub.load('pytorch/vision:v0.6.0', 'deeplabv3_resnet101', weights='DEFAULT')
|
11 |
+
model.eval()
|
12 |
+
|
13 |
+
|
14 |
+
def image_to_tensor(image):
|
15 |
+
return transforms.Compose([
|
16 |
+
transforms.ToTensor(),
|
17 |
+
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
|
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'))
|
24 |
+
# add an alpha channel with and fill all with transparent pixels (max 255)
|
25 |
+
a = np.ones(mask.shape, dtype='uint8') * 255
|
26 |
+
# merge the alpha channel back
|
27 |
+
alpha_im = cv2.merge([b, g, r, a], 4)
|
28 |
+
# create a transparent background
|
29 |
+
bg = np.zeros(alpha_im.shape)
|
30 |
+
# set up the new mask
|
31 |
+
new_mask = np.stack([mask, mask, mask, mask], axis=2)
|
32 |
+
# copy only the foreground color pixels from the original image where mask is set
|
33 |
+
return np.where(new_mask, alpha_im, bg).astype(np.uint8)
|
34 |
+
|
35 |
+
|
36 |
+
def predict(image):
|
37 |
+
input_tensor = image_to_tensor(image)
|
38 |
+
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
39 |
+
|
40 |
+
# move the input and model to GPU for speed if available
|
41 |
+
if torch.cuda.is_available():
|
42 |
+
input_batch = input_batch.to('cuda')
|
43 |
+
model.to('cuda')
|
44 |
+
|
45 |
+
with torch.no_grad():
|
46 |
+
output = model(input_batch)['out'][0]
|
47 |
+
output_predictions = output.argmax(0)
|
48 |
+
|
49 |
+
# create a binary (black and white) mask of the profile foreground
|
50 |
+
mask = output_predictions.byte().cpu().numpy()
|
51 |
+
background = np.zeros(mask.shape)
|
52 |
+
return np.where(mask, 255, background).astype(np.uint8)
|
53 |
+
|
54 |
+
|
55 |
+
def inference(image):
|
56 |
+
mask = predict(image)
|
57 |
+
return make_transparent_foreground(image, mask)
|
58 |
+
|
59 |
+
|
60 |
+
title = "Zero Background"
|
61 |
+
description = r"""
|
62 |
+
## Remove image background
|
63 |
+
|
64 |
+
This is another implementation of <a href='https://github.com/eugenesiow/practical-ml/blob/master/notebooks/Remove_Image_Background_DeepLabV3.ipynb' target='_blank'>eugenesiow</a>.
|
65 |
+
It has no any particular purpose than start research on AI models.
|
66 |
+
|
67 |
+
"""
|
68 |
+
|
69 |
+
article = r"""
|
70 |
+
Questions, doubts, comments, please email 📧 `[email protected]`
|
71 |
+
|
72 |
+
This demo is running on a CPU, if you like this project please make us a donation to run on a GPU or just give us a <a href='https://github.com/leonelhs/face-shine' target='_blank'>Github ⭐</a>
|
73 |
+
|
74 |
+
<a href="https://www.buymeacoffee.com/leonelhs"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=leonelhs&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff" /></a>
|
75 |
+
|
76 |
+
<center><img src='https://visitor-badge.glitch.me/badge?page_id=deoldify.visitor-badge' alt='visitor badge'></center>
|
77 |
+
"""
|
78 |
+
|
79 |
+
demo = gr.Interface(
|
80 |
+
inference, [
|
81 |
+
gr.Image(type="pil", label="Image"),
|
82 |
+
], [
|
83 |
+
gr.Image(type="pil", label="Image alpha background")
|
84 |
+
],
|
85 |
+
title=title,
|
86 |
+
description=description,
|
87 |
+
article=article)
|
88 |
+
|
89 |
+
demo.queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=2.0.1
|
2 |
+
opencv-python
|
3 |
+
|
4 |
+
|
5 |
+
|