Spaces:
Build error
Build error
init
Browse files
app.py
CHANGED
@@ -6,82 +6,45 @@ import sys
|
|
6 |
sys.path.insert(0, 'U-2-Net')
|
7 |
|
8 |
from skimage import io, transform
|
9 |
-
import torch
|
10 |
-
import torchvision
|
11 |
-
from torch.autograd import Variable
|
12 |
-
import torch.nn as nn
|
13 |
-
import torch.nn.functional as F
|
14 |
-
from torch.utils.data import Dataset, DataLoader
|
15 |
-
from torchvision import transforms#, utils
|
16 |
-
# import torch.optim as optim
|
17 |
|
18 |
import numpy as np
|
19 |
from PIL import Image
|
20 |
-
import glob
|
21 |
|
22 |
-
from data_loader import RescaleT
|
23 |
-
from data_loader import ToTensor
|
24 |
-
from data_loader import ToTensorLab
|
25 |
-
from data_loader import SalObjDataset
|
26 |
-
|
27 |
-
from model import U2NET # full size version 173.6 MB
|
28 |
-
from model import U2NETP # small version u2net 4.7 MB
|
29 |
-
|
30 |
-
import huggingface_hub
|
31 |
from utils.face_seg import FaceSeg
|
32 |
import cv2
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
mi = torch.min(d)
|
38 |
-
|
39 |
-
dn = (d-mi)/(ma-mi)
|
40 |
-
|
41 |
-
return dn
|
42 |
-
def save_output(image_name,pred,d_dir):
|
43 |
-
predict = pred
|
44 |
-
predict = predict.squeeze()
|
45 |
-
predict_np = predict.cpu().data.numpy()
|
46 |
-
|
47 |
-
im = Image.fromarray(predict_np*255).convert('RGB')
|
48 |
-
img_name = image_name.split(os.sep)[-1]
|
49 |
-
image = io.imread(image_name)
|
50 |
-
imo = im.resize((image.shape[1],image.shape[0]),resample=Image.BILINEAR)
|
51 |
-
|
52 |
-
pb_np = np.array(imo)
|
53 |
-
|
54 |
-
aaa = img_name.split(".")
|
55 |
-
bbb = aaa[0:-1]
|
56 |
-
imidx = bbb[0]
|
57 |
-
for i in range(1,len(bbb)):
|
58 |
-
imidx = imidx + "." + bbb[i]
|
59 |
-
|
60 |
-
imo.save(d_dir+'/'+imidx+'.png')
|
61 |
-
return d_dir+'/'+imidx+'.png'
|
62 |
|
63 |
segment = FaceSeg()
|
64 |
|
65 |
-
# --------- 1. get image path and name ---------
|
66 |
-
model_name='u2net_portrait'#u2netp
|
67 |
-
|
68 |
-
image_dir = 'portrait_im'
|
69 |
-
prediction_dir = 'portrait_results'
|
70 |
-
if(not os.path.exists(prediction_dir)):
|
71 |
-
os.mkdir(prediction_dir)
|
72 |
-
|
73 |
-
model_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'U-2-Net/saved_models/u2net_portrait/u2net_portrait.pth')
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
|
87 |
def process(im):
|
@@ -98,48 +61,9 @@ def process(im):
|
|
98 |
foreground = image * matte + np.full(image.shape, 255) * (1 - matte)
|
99 |
cv2.imwrite(im.name, foreground)
|
100 |
|
101 |
-
|
102 |
-
print("Number of images: ", len(img_name_list))
|
103 |
-
# --------- 2. dataloader ---------
|
104 |
-
# 1. dataloader
|
105 |
-
test_salobj_dataset = SalObjDataset(img_name_list=img_name_list,
|
106 |
-
lbl_name_list=[],
|
107 |
-
transform=transforms.Compose([RescaleT(512),
|
108 |
-
ToTensorLab(flag=0)])
|
109 |
-
)
|
110 |
-
test_salobj_dataloader = DataLoader(test_salobj_dataset,
|
111 |
-
batch_size=1,
|
112 |
-
shuffle=False,
|
113 |
-
num_workers=1)
|
114 |
-
|
115 |
-
results = []
|
116 |
-
# --------- 4. inference for each image ---------
|
117 |
-
for i_test, data_test in enumerate(test_salobj_dataloader):
|
118 |
-
|
119 |
-
print("inferencing:", img_name_list[i_test].split(os.sep)[-1])
|
120 |
-
|
121 |
-
inputs_test = data_test['image']
|
122 |
-
inputs_test = inputs_test.type(torch.FloatTensor)
|
123 |
-
|
124 |
-
# if torch.cuda.is_available():
|
125 |
-
# inputs_test = Variable(inputs_test.cuda())
|
126 |
-
# else:
|
127 |
-
inputs_test = Variable(inputs_test)
|
128 |
-
|
129 |
-
d1, d2, d3, d4, d5, d6, d7 = net(inputs_test)
|
130 |
-
|
131 |
-
# normalization
|
132 |
-
pred = 1.0 - d1[:, 0, :, :]
|
133 |
-
pred = normPRED(pred)
|
134 |
-
|
135 |
-
# save results to test_results folder
|
136 |
-
results.append(save_output(img_name_list[i_test], pred, prediction_dir))
|
137 |
-
|
138 |
-
del d1, d2, d3, d4, d5, d6, d7
|
139 |
-
|
140 |
-
print(results)
|
141 |
|
142 |
-
return Image.open(
|
143 |
|
144 |
title = "U-2-Net"
|
145 |
description = "Gradio demo for U-2-Net, https://github.com/xuebinqin/U-2-Net"
|
|
|
6 |
sys.path.insert(0, 'U-2-Net')
|
7 |
|
8 |
from skimage import io, transform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
import numpy as np
|
11 |
from PIL import Image
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
from utils.face_seg import FaceSeg
|
14 |
import cv2
|
15 |
|
16 |
+
import requests
|
17 |
+
import base64
|
18 |
+
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
segment = FaceSeg()
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
def profuai(im_path, out_path):
|
24 |
+
r = requests.post(
|
25 |
+
'http://nebula.cs.ualberta.ca/predict',
|
26 |
+
files={
|
27 |
+
'file': open(im_path, 'rb'),
|
28 |
+
},
|
29 |
+
headers={'Host': 'nebula.cs.ualberta.ca', 'Origin': 'http://nebula.cs.ualberta.ca','Referer':'http://nebula.cs.ualberta.ca/',
|
30 |
+
'X-Requested-With':'XMLHttpRequest',
|
31 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0'}
|
32 |
+
|
33 |
+
)
|
34 |
+
#print(r)
|
35 |
+
if (r.status_code == 200):
|
36 |
+
data = r.text
|
37 |
+
#data:image/png;base64,
|
38 |
+
a = data[len("data:image/png;base64,"):]
|
39 |
+
missing_padding = 4 - len(a) % 4
|
40 |
+
if missing_padding:
|
41 |
+
a += '=' * missing_padding
|
42 |
+
|
43 |
+
img = Image.open(BytesIO(base64.urlsafe_b64decode(a)))
|
44 |
+
# print(a)
|
45 |
+
img.save(out_path, quality=80)
|
46 |
+
else:
|
47 |
+
raise Exception('error 1001')
|
48 |
|
49 |
|
50 |
def process(im):
|
|
|
61 |
foreground = image * matte + np.full(image.shape, 255) * (1 - matte)
|
62 |
cv2.imwrite(im.name, foreground)
|
63 |
|
64 |
+
profuai(im.name, im.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
return Image.open(im.name)
|
67 |
|
68 |
title = "U-2-Net"
|
69 |
description = "Gradio demo for U-2-Net, https://github.com/xuebinqin/U-2-Net"
|