forked from xuebinqin/U-2-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u2net_portrait_demo.py
175 lines (135 loc) · 4.64 KB
/
u2net_portrait_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import cv2
import torch
from model import U2NET
from torch.autograd import Variable
import numpy as np
from glob import glob
import os
def detect_single_face(face_cascade,img):
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
if(len(faces)==0):
print("Warming: no face detection, the portrait u2net will run on the whole image!")
return None
# filter to keep the largest face
wh = 0
idx = 0
for i in range(0,len(faces)):
(x,y,w,h) = faces[i]
if(wh<w*h):
idx = i
wh = w*h
return faces[idx]
# crop, pad and resize face region to 512x512 resolution
def crop_face(img, face):
# no face detected, return the whole image and the inference will run on the whole image
if(face is None):
return img
(x, y, w, h) = face
height,width = img.shape[0:2]
# crop the face with a bigger bbox
hmw = h - w
# hpad = int(h/2)+1
# wpad = int(w/2)+1
l,r,t,b = 0,0,0,0
lpad = int(float(w)*0.4)
left = x-lpad
if(left<0):
l = lpad-x
left = 0
rpad = int(float(w)*0.4)
right = x+w+rpad
if(right>width):
r = right-width
right = width
tpad = int(float(h)*0.6)
top = y - tpad
if(top<0):
t = tpad-y
top = 0
bpad = int(float(h)*0.2)
bottom = y+h+bpad
if(bottom>height):
b = bottom-height
bottom = height
im_face = img[top:bottom,left:right]
if(len(im_face.shape)==2):
im_face = np.repeat(im_face[:,:,np.newaxis],(1,1,3))
im_face = np.pad(im_face,((t,b),(l,r),(0,0)),mode='constant',constant_values=((255,255),(255,255),(255,255)))
# pad to achieve image with square shape for avoding face deformation after resizing
hf,wf = im_face.shape[0:2]
if(hf-2>wf):
wfp = int((hf-wf)/2)
im_face = np.pad(im_face,((0,0),(wfp,wfp),(0,0)),mode='constant',constant_values=((255,255),(255,255),(255,255)))
elif(wf-2>hf):
hfp = int((wf-hf)/2)
im_face = np.pad(im_face,((hfp,hfp),(0,0),(0,0)),mode='constant',constant_values=((255,255),(255,255),(255,255)))
# resize to have 512x512 resolution
im_face = cv2.resize(im_face, (512,512), interpolation = cv2.INTER_AREA)
return im_face
def normPRED(d):
ma = torch.max(d)
mi = torch.min(d)
dn = (d-mi)/(ma-mi)
return dn
def inference(net,input):
# normalize the input
tmpImg = np.zeros((input.shape[0],input.shape[1],3))
input = input/np.max(input)
tmpImg[:,:,0] = (input[:,:,2]-0.406)/0.225
tmpImg[:,:,1] = (input[:,:,1]-0.456)/0.224
tmpImg[:,:,2] = (input[:,:,0]-0.485)/0.229
# convert BGR to RGB
tmpImg = tmpImg.transpose((2, 0, 1))
tmpImg = tmpImg[np.newaxis,:,:,:]
tmpImg = torch.from_numpy(tmpImg)
# convert numpy array to torch tensor
tmpImg = tmpImg.type(torch.FloatTensor)
if torch.cuda.is_available():
tmpImg = Variable(tmpImg.cuda())
else:
tmpImg = Variable(tmpImg)
# inference
d1,d2,d3,d4,d5,d6,d7= net(tmpImg)
# normalization
pred = 1.0 - d1[:,0,:,:]
pred = normPRED(pred)
# convert torch tensor to numpy array
pred = pred.squeeze()
pred = pred.cpu().data.numpy()
del d1,d2,d3,d4,d5,d6,d7
return pred
def main():
# get the image path list for inference
im_list = glob('./test_data/test_portrait_images/your_portrait_im/*')
print("Number of images: ",len(im_list))
# indicate the output directory
out_dir = './test_data/test_portrait_images/your_portrait_results'
if(not os.path.exists(out_dir)):
os.mkdir(out_dir)
# Load the cascade face detection model
face_cascade = cv2.CascadeClassifier('./saved_models/face_detection_cv2/haarcascade_frontalface_default.xml')
# u2net_portrait path
model_dir = './saved_models/u2net_portrait/u2net_portrait.pth'
# load u2net_portrait model
net = U2NET(3,1)
net.load_state_dict(torch.load(model_dir))
if torch.cuda.is_available():
net.cuda()
net.eval()
# do the inference one-by-one
for i in range(0,len(im_list)):
print("--------------------------")
print("inferencing ", i, "/", len(im_list), im_list[i])
# load each image
img = cv2.imread(im_list[i])
height,width = img.shape[0:2]
face = detect_single_face(face_cascade,img)
im_face = crop_face(img, face)
im_portrait = inference(net,im_face)
# save the output
cv2.imwrite(out_dir+"/"+im_list[i].split('/')[-1][0:-4]+'.png',(im_portrait*255).astype(np.uint8))
if __name__ == '__main__':
main()