-
Notifications
You must be signed in to change notification settings - Fork 134
/
portrait_video.py
85 lines (65 loc) · 2.33 KB
/
portrait_video.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
import cv2, sys, time
import numpy as np
import tensorflow as tf
from PIL import Image
# Normalize the input image
def normalize(imgOri, scale=1, mean=[103.94, 116.78, 123.68], val=[0.017, 0.017, 0.017]):
img = np.array(imgOri.copy(), np.float32)/scale
return (img - mean) * val
# Alpha blend frame with background
def blend(frame, alpha):
background = np.zeros(frame.shape) + [255, 255, 255]
alphargb = cv2.cvtColor(alpha, cv2.COLOR_GRAY2BGR)
result = np.uint8(frame * alphargb + background * (1-alphargb))
return frame, alphargb*255, result
# Initialize tflite-interpreter
interpreter = tf.lite.Interpreter(model_path="portrait_video.tflite") # Use 'tf.lite' on recent tf versions
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
height, width = input_details[0]['shape'][1:3]
# Initialize video capturer
cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
cnt = 1
while True:
# Read the BGR frames
ret, frame = cap.read()
image=Image.fromarray(frame)
# Resize the image
image= image.resize((width, height), Image.ANTIALIAS)
image=np.asarray(image)
# Normalize the input
image = normalize(image)
# Choose prior mask
if cnt == 1:
prior = np.zeros((height, width, 1)) # first frame
else:
prior = pred_video
# Add prior as fourth channel
image=np.dstack([image,prior])
prepimg = image[np.newaxis, :, :, :]
# Invoke interpreter for inference
interpreter.set_tensor(input_details[0]['index'], np.array(prepimg, dtype=np.float32))
interpreter.invoke()
outputs = interpreter.get_tensor(output_details[0]['index'])
outputs = outputs.reshape(height,width,1)
# Save output to feed subsequent inputs
pred_video = outputs
# Process the output
outputs = cv2.resize(outputs, size)
_,_,outputs=blend(frame, outputs)
# Display the output
cv2.imshow('Portrait Video',outputs)
if cv2.waitKey(1)&0xFF == ord('q'):
break
# Print the frame count
cnt += 1
if cnt % 100 == 0:
print ("cnt: ", cnt)
# When everything done, release the capturer
cap.release()
cv2.destroyAllWindows()
'''
Sample run: python portrait_video.py
'''