-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathblendshape.py
130 lines (111 loc) · 3.48 KB
/
blendshape.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
from io import BytesIO
import numpy as np
import cv2
import matplotlib.pyplot as plt
Labels = [
"_neutral",
"browDownLeft",
"browDownRight",
"browInnerUp",
"browOuterUpLeft",
"browOuterUpRight",
"cheekPuff",
"cheekSquintLeft",
"cheekSquintRight",
"eyeBlinkLeft",
"eyeBlinkRight",
"eyeLookDownLeft",
"eyeLookDownRight",
"eyeLookInLeft",
"eyeLookInRight",
"eyeLookOutLeft",
"eyeLookOutRight",
"eyeLookUpLeft",
"eyeLookUpRight",
"eyeSquintLeft",
"eyeSquintRight",
"eyeWideLeft",
"eyeWideRight",
"jawForward",
"jawLeft",
"jawOpen",
"jawRight",
"mouthClose",
"mouthDimpleLeft",
"mouthDimpleRight",
"mouthFrownLeft",
"mouthFrownRight",
"mouthFunnel",
"mouthLeft",
"mouthLowerDownLeft",
"mouthLowerDownRight",
"mouthPressLeft",
"mouthPressRight",
"mouthPucker",
"mouthRight",
"mouthRollLower",
"mouthRollUpper",
"mouthShrugLower",
"mouthShrugUpper",
"mouthSmileLeft",
"mouthSmileRight",
"mouthStretchLeft",
"mouthStretchRight",
"mouthUpperUpLeft",
"mouthUpperUpRight",
"noseSneerLeft",
"noseSneerRight"
]
LandmarksSubsetIdxs = [
0, 1, 4, 5, 6, 7, 8, 10, 13, 14, 17, 21, 33, 37, 39,
40, 46, 52, 53, 54, 55, 58, 61, 63, 65, 66, 67, 70, 78, 80,
81, 82, 84, 87, 88, 91, 93, 95, 103, 105, 107, 109, 127, 132, 133,
136, 144, 145, 146, 148, 149, 150, 152, 153, 154, 155, 157, 158, 159, 160,
161, 162, 163, 168, 172, 173, 176, 178, 181, 185, 191, 195, 197, 234, 246,
249, 251, 263, 267, 269, 270, 276, 282, 283, 284, 285, 288, 291, 293, 295,
296, 297, 300, 308, 310, 311, 312, 314, 317, 318, 321, 323, 324, 332, 334,
336, 338, 356, 361, 362, 365, 373, 374, 375, 377, 378, 379, 380, 381, 382,
384, 385, 386, 387, 388, 389, 390, 397, 398, 400, 402, 405, 409, 415, 454,
466, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477
]
def plot_to_npy(fig):
buf = BytesIO()
fig.savefig(buf, format="png")
buf.seek(0)
img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8)
buf.close()
img = cv2.imdecode(img_arr, 1)
return img
def plot_face_blendshapes_bar_graph(face_blendshapes):
# Extract the face blendshapes category names and scores.
face_blendshapes_names = Labels
face_blendshapes_scores = face_blendshapes
# The blendshapes are ordered in decreasing score value.
face_blendshapes_ranks = range(len(face_blendshapes_names))
fig, ax = plt.subplots(figsize=(12, 12))
bar = ax.barh(face_blendshapes_ranks, face_blendshapes_scores, label=[str(x) for x in face_blendshapes_ranks])
ax.set_yticks(face_blendshapes_ranks, face_blendshapes_names)
ax.invert_yaxis()
# Label each bar with values
for score, patch in zip(face_blendshapes_scores, bar.patches):
plt.text(patch.get_x() + patch.get_width(), patch.get_y(), f"{score:.4f}", va="top")
ax.set_xlabel('Score')
ax.set_title("Face Blendshapes")
plt.tight_layout()
img = plot_to_npy(fig)
return img
def face_blendshapes(net, landmarks, image_size, onnx):
landmarks = landmarks[LandmarksSubsetIdxs]
h, w = image_size
for lmk in landmarks:
lmk[0] *= w
lmk[1] *= h
input = landmarks[:, :2]
input = np.expand_dims(input, axis=0)
input = input.astype(np.float32)
if not onnx:
output = net.predict([input])
else:
output = net.run(None, {'serving_default_input_points:0': input})
score = output[0]
return score