-
Notifications
You must be signed in to change notification settings - Fork 62
/
demo.py
55 lines (33 loc) · 1.13 KB
/
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
import glob
import time
import cv2
import keras
import numpy as np
def predict(image, height=224, width=224):
im = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im = im / 255
im = cv2.resize(im, (height, width))
im = im.reshape((1,) + im.shape)
pred = model.predict(im)
mask = pred.reshape((224, 224))
return mask
def transfer(image, mask):
mask[mask > 0.5] = 255
mask[mask <= 0.5] = 0
mask = cv2.resize(mask, (image.shape[1], image.shape[0]))
mask_n = np.zeros_like(image)
mask_n[:, :, 0] = mask
alpha = 0.8
beta = (1.0 - alpha)
dst = cv2.addWeighted(image, alpha, mask_n, beta, 0.0)
return dst
if __name__ == '__main__':
model = keras.models.load_model('models/hairnet_matting.hdf5')
for name in glob.glob('test/images/*.jpg'):
img = cv2.imread(name)
st = time.time()
mask = predict(img)
d1 = time.time()
dst = transfer(img, mask)
print("segment: %f, color:%f" % (d1 - st, time.time() - d1))
cv2.imwrite(name.replace('images', 'outs'), dst)