Update app.py
Browse files
app.py
CHANGED
@@ -2,19 +2,12 @@ import numpy as np
|
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
4 |
from torchvision.transforms.functional import normalize
|
5 |
-
# from foo import hello
|
6 |
import gradio as gr
|
7 |
from gradio_imageslider import ImageSlider
|
8 |
from briarmbg import BriaRMBG
|
9 |
import PIL
|
10 |
from PIL import Image
|
11 |
from typing import Tuple
|
12 |
-
# import git # pip install gitpython
|
13 |
-
|
14 |
-
# hello()
|
15 |
-
|
16 |
-
# git.Git(".").clone("https://huggingface.co/briaai/RMBG-1.4")
|
17 |
-
# git.Git(".").clone("git@hf.co:briaai/RMBG-1.4")
|
18 |
|
19 |
net=BriaRMBG()
|
20 |
model_path = "./model.pth"
|
@@ -54,13 +47,9 @@ def resize_image(image):
|
|
54 |
def process(image):
|
55 |
|
56 |
# prepare input
|
57 |
-
print(type(image))
|
58 |
-
print(image.shape)
|
59 |
orig_image = Image.fromarray(image)
|
60 |
-
# return [orig_image,orig_image]
|
61 |
w,h = orig_im_size = orig_image.size
|
62 |
image = resize_image(orig_image)
|
63 |
-
print("process debug1")
|
64 |
im_np = np.array(image)
|
65 |
im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2,0,1)
|
66 |
im_tensor = torch.unsqueeze(im_tensor,0)
|
@@ -69,16 +58,13 @@ def process(image):
|
|
69 |
if torch.cuda.is_available():
|
70 |
im_tensor=im_tensor.cuda()
|
71 |
|
72 |
-
print("process debug2")
|
73 |
#inference
|
74 |
result=net(im_tensor)
|
75 |
-
print("process debug3")
|
76 |
# post process
|
77 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h,w), mode='bilinear') ,0)
|
78 |
ma = torch.max(result)
|
79 |
mi = torch.min(result)
|
80 |
result = (result-mi)/(ma-mi)
|
81 |
-
print("process debug4")
|
82 |
# image to pil
|
83 |
im_array = (result*255).cpu().data.numpy().astype(np.uint8)
|
84 |
pil_im = Image.fromarray(np.squeeze(im_array))
|
@@ -112,12 +98,20 @@ def process(image):
|
|
112 |
|
113 |
# block.launch(debug = True)
|
114 |
|
|
|
115 |
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
examples = [['./input.jpg'],]
|
119 |
-
output = ImageSlider(position=0.5,label='Image without background
|
120 |
-
demo = gr.Interface(fn=process,inputs="
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
demo.launch(share=False)
|
|
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
4 |
from torchvision.transforms.functional import normalize
|
|
|
5 |
import gradio as gr
|
6 |
from gradio_imageslider import ImageSlider
|
7 |
from briarmbg import BriaRMBG
|
8 |
import PIL
|
9 |
from PIL import Image
|
10 |
from typing import Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
net=BriaRMBG()
|
13 |
model_path = "./model.pth"
|
|
|
47 |
def process(image):
|
48 |
|
49 |
# prepare input
|
|
|
|
|
50 |
orig_image = Image.fromarray(image)
|
|
|
51 |
w,h = orig_im_size = orig_image.size
|
52 |
image = resize_image(orig_image)
|
|
|
53 |
im_np = np.array(image)
|
54 |
im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2,0,1)
|
55 |
im_tensor = torch.unsqueeze(im_tensor,0)
|
|
|
58 |
if torch.cuda.is_available():
|
59 |
im_tensor=im_tensor.cuda()
|
60 |
|
|
|
61 |
#inference
|
62 |
result=net(im_tensor)
|
|
|
63 |
# post process
|
64 |
result = torch.squeeze(F.interpolate(result[0][0], size=(h,w), mode='bilinear') ,0)
|
65 |
ma = torch.max(result)
|
66 |
mi = torch.min(result)
|
67 |
result = (result-mi)/(ma-mi)
|
|
|
68 |
# image to pil
|
69 |
im_array = (result*255).cpu().data.numpy().astype(np.uint8)
|
70 |
pil_im = Image.fromarray(np.squeeze(im_array))
|
|
|
98 |
|
99 |
# block.launch(debug = True)
|
100 |
|
101 |
+
# block = gr.Blocks().queue()
|
102 |
|
103 |
+
gr.Markdown("## BRIA RMBG 1.4")
|
104 |
+
gr.HTML('''
|
105 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
106 |
+
This is a demo for BRIA RMBG 1.4 that using
|
107 |
+
<a href="https://huggingface.co/briaai/RMBG-1.4" target="_blank">BRIA RMBG-1.4 image matting model</a> as backbone.
|
108 |
+
</p>
|
109 |
+
''')
|
110 |
+
title = "Background Removal"
|
111 |
+
description = "Remove Image Background"
|
112 |
examples = [['./input.jpg'],]
|
113 |
+
output = ImageSlider(position=0.5,label='Image without background', type="pil", show_download_button=True)
|
114 |
+
demo = gr.Interface(fn=process,inputs="Image", outputs=output, examples=examples, title=title, description=description)
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
demo.launch(share=False)
|