Spaces:
Runtime error
Runtime error
yourusername
commited on
Commit
β’
f63e6a9
0
Parent(s):
:beers: cheers
Browse files- .gitattributes +27 -0
- .github/workflows/check_size.yaml +17 -0
- .github/workflows/sync_to_hub.yaml +20 -0
- README.md +19 -0
- app.py +116 -0
- packages.txt +3 -0
- requirements.txt +6 -0
.gitattributes
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.github/workflows/check_size.yaml
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Check file size
|
2 |
+
|
3 |
+
on:
|
4 |
+
pull_request:
|
5 |
+
branches: [main]
|
6 |
+
|
7 |
+
# to run this workflow manually from the Actions tab
|
8 |
+
workflow_dispatch:
|
9 |
+
|
10 |
+
jobs:
|
11 |
+
sync-to-hub:
|
12 |
+
runs-on: ubuntu-latest
|
13 |
+
steps:
|
14 |
+
- name: Check large files
|
15 |
+
uses: ActionsDesk/lfs-warning@v2.0
|
16 |
+
with:
|
17 |
+
filesizelimit: 10485760 # = 10MB, so we can sync to HF spaces
|
.github/workflows/sync_to_hub.yaml
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Sync to Hugging Face hub
|
2 |
+
|
3 |
+
on:
|
4 |
+
push:
|
5 |
+
branches: [main]
|
6 |
+
|
7 |
+
# to run this workflow manually from the Actions tab
|
8 |
+
workflow_dispatch:
|
9 |
+
|
10 |
+
jobs:
|
11 |
+
sync-to-hub:
|
12 |
+
runs-on: ubuntu-latest
|
13 |
+
steps:
|
14 |
+
- uses: actions/checkout@v2
|
15 |
+
with:
|
16 |
+
fetch-depth: 0
|
17 |
+
- name: Push to hub
|
18 |
+
env:
|
19 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
20 |
+
run: git push https://nateraw:$HF_TOKEN@huggingface.co/spaces/nateraw/background-remover main --force
|
README.md
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: background-remover
|
3 |
+
emoji: π€
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
+
sdk: gradio
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
---
|
10 |
+
|
11 |
+
# background-remover
|
12 |
+
|
13 |
+
[![Generic badge](https://img.shields.io/badge/π€-Open%20In%20Spaces-blue.svg)](https://huggingface.co/spaces/nateraw/background-remover)
|
14 |
+
|
15 |
+
A Gradio app to remove the background from an image
|
16 |
+
|
17 |
+
---
|
18 |
+
|
19 |
+
Autogenerated using [this template](https://github.com/nateraw/spaces-template)
|
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import onnxruntime
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
|
9 |
+
# Get x_scale_factor & y_scale_factor to resize image
|
10 |
+
def get_scale_factor(im_h, im_w, ref_size=512):
|
11 |
+
|
12 |
+
if max(im_h, im_w) < ref_size or min(im_h, im_w) > ref_size:
|
13 |
+
if im_w >= im_h:
|
14 |
+
im_rh = ref_size
|
15 |
+
im_rw = int(im_w / im_h * ref_size)
|
16 |
+
elif im_w < im_h:
|
17 |
+
im_rw = ref_size
|
18 |
+
im_rh = int(im_h / im_w * ref_size)
|
19 |
+
else:
|
20 |
+
im_rh = im_h
|
21 |
+
im_rw = im_w
|
22 |
+
|
23 |
+
im_rw = im_rw - im_rw % 32
|
24 |
+
im_rh = im_rh - im_rh % 32
|
25 |
+
|
26 |
+
x_scale_factor = im_rw / im_w
|
27 |
+
y_scale_factor = im_rh / im_h
|
28 |
+
|
29 |
+
return x_scale_factor, y_scale_factor
|
30 |
+
|
31 |
+
|
32 |
+
MODEL_PATH = hf_hub_download('nateraw/background-remover-files', 'modnet.onnx', repo_type='dataset')
|
33 |
+
|
34 |
+
|
35 |
+
def main(image_path):
|
36 |
+
|
37 |
+
# read image
|
38 |
+
im = cv2.imread(image_path)
|
39 |
+
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
40 |
+
|
41 |
+
# unify image channels to 3
|
42 |
+
if len(im.shape) == 2:
|
43 |
+
im = im[:, :, None]
|
44 |
+
if im.shape[2] == 1:
|
45 |
+
im = np.repeat(im, 3, axis=2)
|
46 |
+
elif im.shape[2] == 4:
|
47 |
+
im = im[:, :, 0:3]
|
48 |
+
|
49 |
+
# normalize values to scale it between -1 to 1
|
50 |
+
im = (im - 127.5) / 127.5
|
51 |
+
|
52 |
+
im_h, im_w, im_c = im.shape
|
53 |
+
x, y = get_scale_factor(im_h, im_w)
|
54 |
+
|
55 |
+
# resize image
|
56 |
+
im = cv2.resize(im, None, fx=x, fy=y, interpolation=cv2.INTER_AREA)
|
57 |
+
|
58 |
+
# prepare input shape
|
59 |
+
im = np.transpose(im)
|
60 |
+
im = np.swapaxes(im, 1, 2)
|
61 |
+
im = np.expand_dims(im, axis=0).astype('float32')
|
62 |
+
|
63 |
+
# Initialize session and get prediction
|
64 |
+
session = onnxruntime.InferenceSession(MODEL_PATH, None)
|
65 |
+
input_name = session.get_inputs()[0].name
|
66 |
+
output_name = session.get_outputs()[0].name
|
67 |
+
result = session.run([output_name], {input_name: im})
|
68 |
+
|
69 |
+
# refine matte
|
70 |
+
matte = (np.squeeze(result[0]) * 255).astype('uint8')
|
71 |
+
matte = cv2.resize(matte, dsize=(im_w, im_h), interpolation=cv2.INTER_AREA)
|
72 |
+
|
73 |
+
# HACK - Could probably just convert this to PIL instead of writing
|
74 |
+
cv2.imwrite('out.png', matte)
|
75 |
+
|
76 |
+
image = Image.open(image_path)
|
77 |
+
matte = Image.open('out.png')
|
78 |
+
|
79 |
+
# obtain predicted foreground
|
80 |
+
image = np.asarray(image)
|
81 |
+
if len(image.shape) == 2:
|
82 |
+
image = image[:, :, None]
|
83 |
+
if image.shape[2] == 1:
|
84 |
+
image = np.repeat(image, 3, axis=2)
|
85 |
+
elif image.shape[2] == 4:
|
86 |
+
image = image[:, :, 0:3]
|
87 |
+
matte = np.repeat(np.asarray(matte)[:, :, None], 3, axis=2) / 255
|
88 |
+
foreground = image * matte + np.full(image.shape, 255) * (1 - matte)
|
89 |
+
return Image.fromarray(foreground.astype(np.uint8))
|
90 |
+
|
91 |
+
|
92 |
+
title = "MODNet Background Remover"
|
93 |
+
description = "Gradio demo for MODNet, a model that can remove the background from a given image. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
94 |
+
article = "<div style='text-align: center;'> <a href='https://github.com/ZHKKKe/MODNet' target='_blank'>Github Repo</a> | <a href='https://arxiv.org/abs/2011.11961' target='_blank'>MODNet: Real-Time Trimap-Free Portrait Matting via Objective Decomposition</a> </div>"
|
95 |
+
|
96 |
+
interface = gr.Interface(
|
97 |
+
fn=main,
|
98 |
+
inputs=gr.inputs.Image(type='filepath'),
|
99 |
+
outputs='image',
|
100 |
+
examples=[
|
101 |
+
[
|
102 |
+
hf_hub_download(
|
103 |
+
'nateraw/background-remover-files',
|
104 |
+
'twitter_profile_pic.jpeg',
|
105 |
+
repo_type='dataset',
|
106 |
+
force_filename='twitter_profile_pic.jpeg',
|
107 |
+
)
|
108 |
+
]
|
109 |
+
],
|
110 |
+
title=title,
|
111 |
+
description=description,
|
112 |
+
article=article,
|
113 |
+
)
|
114 |
+
|
115 |
+
if __name__ == '__main__':
|
116 |
+
interface.launch(debug=True)
|
packages.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
ffmpeg
|
2 |
+
libsm6
|
3 |
+
libxext6
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
onnxruntime==1.6.0
|
3 |
+
onnx
|
4 |
+
opencv-python
|
5 |
+
numpy
|
6 |
+
huggingface_hub
|