-
Notifications
You must be signed in to change notification settings - Fork 4
/
MediaPipeFaceSolver.cs
411 lines (343 loc) · 13.4 KB
/
MediaPipeFaceSolver.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mediapipe.Unity.CoordinateSystem;
using Mediapipe.Unity.Holistic;
using Google.Protobuf;
using Mediapipe;
using Mediapipe.Unity;
public class MediaPipeFaceSolver : MonoBehaviour
{
public struct Face
{
public Head Head;
public Eyes Eyes;
// It only has one value for the eye brows and pupil in the original code (Kaleido)
// TODO: investigate getting 2 different brow and pupil values
public float Brow;
public Vector2 Pupils;
public Mouth Mouth;
}
public struct Head
{
// X, Y and Z represent 3D radian angles.
public float X;
public float Y;
public float Z;
public float Width;
public float Height;
/// Center of face detection square.
public Vector3 Position;
// TODO: convert to a Quaternion
// Euler angles normalized between -1 and 1.
public Vector3 NormalizedAngles;
}
public struct Mouth
{
// Horizontal mouth open
public float X;
// Vertical mouth open
public float Y;
// Mouth vowel shape
public Phoneme Shape;
}
public struct Phoneme
{
// 'A' shape.
public float A;
// 'E' shape.
public float E;
// 'I' shape.
public float I;
// 'O' shape.
public float O;
// 'U' shape.
public float U;
}
public struct Eyes
{
// Wideness of left eye
public float Left;
/// Wideness of right eye
public float Right;
}
public readonly int[] EyeLeftPoints = new int[] { 130, 133, 160, 159, 158, 144, 145, 153 };
public readonly int[] EyeRightPoints = new int[] { 263, 362, 387, 386, 385, 373, 374, 380 };
public readonly int[] BrowLeftPoints = new int[] { 35, 244, 63, 105, 66, 229, 230, 231 };
public readonly int[] BrowRightPoints = new int[] { 265, 464, 293, 334, 296, 449, 450, 451 };
public readonly int[] PupilLeftPoints = new int[] { 468, 469, 470, 471, 472 };
public readonly int[] PupilRightPoints = new int[] { 473, 474, 475, 476, 477 };
public Vector3 ToVector(Landmark landmark) => new(landmark.X, landmark.Y, landmark.Z);
public Vector2 ToVector2(Vector3 vector) => new(vector.x, vector.y);
public Vector3 ToVector(NormalizedLandmark landmark) => new(landmark.X, landmark.Y, landmark.Z);
public Vector2 ToVector2(NormalizedLandmark landmark) => new(landmark.X, landmark.Y);
public Face Solve(
NormalizedLandmarkList list,
bool smoothBlink = false,
float blinkHigh = .35f,
float blinkLow = .5f
)
{
Head head = CalcHead(list);
Mouth mouth = CalcMouth(list);
Eyes eyes = CalcEyes(list, blinkHigh, blinkLow);
if (smoothBlink)
StabilizeBlink(ref eyes, head.Y);
Vector2 pupils = CalcPupils(list);
float brow = CalcBrow(list);
return new Face
{
Head = head,
Eyes = eyes,
Brow = brow,
Pupils = pupils,
Mouth = mouth,
};
}
public Mouth CalcMouth(NormalizedLandmarkList list)
{
var landmarks = list.Landmark;
// Eye keypoints
Vector3 eyeInnerCornerL = ToVector(landmarks[133]);
Vector3 eyeInnerCornerR = ToVector(landmarks[362]);
Vector3 eyeOuterCornerL = ToVector(landmarks[130]);
Vector3 eyeOuterCornerR = ToVector(landmarks[263]);
// Eye keypoint distances
float eyeInnerDistance = Vector3.Distance(eyeInnerCornerL, eyeInnerCornerR);
float eyeOuterDistance = Vector3.Distance(eyeOuterCornerL, eyeOuterCornerR);
// Mouth keypoints
Vector3 upperInnerLip = ToVector(landmarks[13]);
Vector3 lowerInnerLip = ToVector(landmarks[14]);
Vector3 mouthCornerLeft = ToVector(landmarks[61]);
Vector3 mouthCornerRight = ToVector(landmarks[291]);
// Mouth keypoint distances
float mouthOpen = Vector3.Distance(upperInnerLip, lowerInnerLip);
float mouthWidth = Vector3.Distance(mouthCornerLeft, mouthCornerRight);
// Mouth open and mouth shape ratios
float ratioY = mouthOpen / eyeInnerDistance;
float ratioX = mouthWidth / eyeOuterDistance;
// Normalize and scale mouth open
ratioY = ratioY.Remap(0.15f, 0.7f);
// Normalize and scale mouth shape
ratioX = ratioX.Remap(0.45f, 0.9f);
ratioX = (ratioX - 0.3f) * 2;
float mouthX = ratioX;
float mouthY = (mouthOpen / eyeInnerDistance).Remap(0.17f, 0.5f);
float ratioI = Math.Clamp(mouthX.Remap(0, 1) * 2 * mouthY.Remap(0.2f, 0.7f), 0, 1);
float ratioA = mouthY * 0.4f + mouthY * (1 - ratioI) * 0.6f;
float ratioU = mouthY * (1 - ratioI).Remap(0, 0.3f) * 0.1f;
float ratioE = ratioU.Remap(0.2f, 1) * (1 - ratioI) * 0.3f;
float ratioO = (1 - ratioI) * mouthY.Remap(0.3f, 1) * 0.4f;
return new Mouth
{
X = ratioX,
Y = ratioY,
Shape = new Phoneme
{
A = ratioA,
E = ratioE,
I = ratioI,
O = ratioO,
U = ratioU,
},
};
}
public Head CalcHead(NormalizedLandmarkList list)
{
// Find 3 vectors that form a plane to represent the head
Vector3[] plane = FaceEulerPlane(list);
Vector3 rotate = VectorExtensions.RollPitchYaw(plane[0], plane[1], plane[2]);
// Find center of face detection box
Vector3 midPoint = Vector3.Lerp(plane[0], plane[1], 0.5f);
// Roughly find the dimensions of the face detection box
float width = Vector3.Distance(plane[0], plane[1]);
float height = Vector3.Distance(midPoint, plane[2]);
// Flip
rotate.x *= -1;
rotate.y *= -1;
return new Head
{
X = rotate.x * MathF.PI,
Y = rotate.y * MathF.PI,
Z = rotate.z * MathF.PI,
Width = width,
Height = height,
Position = Vector3.Lerp(midPoint, plane[2], 0.5f),
NormalizedAngles = new Vector3(rotate.x, rotate.y, rotate.z),
};
}
public Vector3[] FaceEulerPlane(NormalizedLandmarkList list)
{
// TODO: This vector processing could probably be optimised
var landmarks = list.Landmark;
// Create face detection square bounds
Vector3 topLeft = ToVector(landmarks[21]);
Vector3 topRight = ToVector(landmarks[251]);
Vector3 bottomRight = ToVector(landmarks[397]);
Vector3 bottomLeft = ToVector(landmarks[172]);
Vector3 bottomMidpoint = Vector3.Lerp(bottomRight, bottomLeft, 0.5f);
return new Vector3[] { topLeft, topRight, bottomMidpoint };
}
public float GetEyeOpen(NormalizedLandmarkList list, string side, float high = .85f, float low = .55f)
{
var landmarks = list.Landmark;
int[] eyePoints = side == "right" ? EyeRightPoints : EyeLeftPoints;
float eyeDistance = EyeLidRatio(
landmarks[eyePoints[0]],
landmarks[eyePoints[1]],
landmarks[eyePoints[2]],
landmarks[eyePoints[3]],
landmarks[eyePoints[4]],
landmarks[eyePoints[5]],
landmarks[eyePoints[6]],
landmarks[eyePoints[7]]
);
// Human eye width to height ratio is roughly .3
float maxRatio = 0.285f;
// Compare ratio against max ratio
float ratio = Math.Clamp(eyeDistance / maxRatio, 0, 2);
// Remap eye open and close ratios to increase sensitivity
float eyeOpenRatio = ratio.Remap(low, high);
return eyeOpenRatio;
}
public float EyeLidRatio(
NormalizedLandmark outerCorner,
NormalizedLandmark innerCorner,
NormalizedLandmark outerUpperLid,
NormalizedLandmark midUpperLid,
NormalizedLandmark innerUpperLid,
NormalizedLandmark outerLowerLid,
NormalizedLandmark midLowerLid,
NormalizedLandmark innerLowerLid)
{
Vector2 eyeOuterCorner = ToVector2(outerCorner);
Vector2 eyeInnerCorner = ToVector2(innerCorner);
Vector2 eyeOuterUpperLid = ToVector2(outerUpperLid);
Vector2 eyeMidUpperLid = ToVector2(midUpperLid);
Vector2 eyeInnerUpperLid = ToVector2(innerUpperLid);
Vector2 eyeOuterLowerLid = ToVector2(outerLowerLid);
Vector2 eyeMidLowerLid = ToVector2(midLowerLid);
Vector2 eyeInnerLowerLid = ToVector2(innerLowerLid);
// Use 2D Distances instead of 3D for less jitter
float eyeWidth = Vector2.Distance(eyeOuterCorner, eyeInnerCorner);
float eyeOuterLidDistance = Vector2.Distance(eyeOuterUpperLid, eyeOuterLowerLid);
float eyeMidLidDistance = Vector2.Distance(eyeMidUpperLid, eyeMidLowerLid);
float eyeInnerLidDistance = Vector2.Distance(eyeInnerUpperLid, eyeInnerLowerLid);
float eyeLidAvg = (eyeOuterLidDistance + eyeMidLidDistance + eyeInnerLidDistance) / 3;
float ratio = eyeLidAvg / eyeWidth;
return ratio;
}
// Calculates pupil position [-1, 1].
public Vector2 PupilPos(NormalizedLandmarkList list, string side)
{
var landmarks = list.Landmark;
int[] eyePoints = side == "right" ? EyeRightPoints : EyeLeftPoints;
Vector3 eyeOuterCorner = ToVector(landmarks[eyePoints[0]]);
Vector3 eyeInnerCorner = ToVector(landmarks[eyePoints[1]]);
float eyeWidth = Vector2.Distance(ToVector2(eyeOuterCorner), ToVector2(eyeInnerCorner));
Vector3 midPoint = Vector3.Lerp(eyeOuterCorner, eyeInnerCorner, .5f);
int[] pupilPoints = side == "right" ? PupilRightPoints : PupilLeftPoints;
Vector3 pupil = ToVector(landmarks[pupilPoints[0]]);
float dx = midPoint.x - pupil.x;
float dy = midPoint.y - pupil.y - eyeWidth * .075f;
float ratioX = 4 * dx / (eyeWidth / 2);
float ratioY = 4 * dy / (eyeWidth / 4);
return new Vector2(ratioX, ratioY);
}
public void StabilizeBlink(ref Eyes eyes, float headY, bool enableWink = true, float maxRotation = .5f)
{
eyes.Left = Math.Clamp(eyes.Left, 0, 1);
eyes.Right = Math.Clamp(eyes.Right, 0, 1);
// Difference between each eye
float blinkDiff = MathF.Abs(eyes.Left - eyes.Right);
// Threshold to which difference is considered a wink
float blinkThresh = enableWink ? .8f : 1.2f;
bool isClosing = eyes.Left < .3f && eyes.Right < .3f;
bool isOpening = eyes.Left > .6f && eyes.Right > .6f;
// Sets obstructed eye to the opposite eye value
if (headY > maxRotation)
{
eyes.Left = eyes.Right;
return;
}
if (headY < -maxRotation)
{
eyes.Right = eyes.Left;
return;
}
// Wink of averaged blink values
if (!(blinkDiff >= blinkThresh && !isClosing && !isOpening))
{
float value = Mathf.Lerp(eyes.Right, eyes.Left, eyes.Right > eyes.Left ? .95f : .05f);
eyes.Left = value;
eyes.Right = value;
}
}
// Calculate eyes.
public Eyes CalcEyes(NormalizedLandmarkList list, float high = .85f, float low = .55f)
{
var landmarks = list.Landmark;
// Return early if no iris tracking
if (landmarks.Count != 478)
{
return new Eyes
{
Left = 1,
Right = 1,
};
}
// Open [0, 1]
return new Eyes
{
Left = GetEyeOpen(list, "left", high, low),
Right = GetEyeOpen(list, "right", high, low),
};
}
// Calculate pupil location normalized to eye bounds
public Vector2 CalcPupils(NormalizedLandmarkList list)
{
var landmarks = list.Landmark;
// Pupil (x: [-1, 1], y: [-1, 1])
if (landmarks.Count != 478)
{
return new Vector2(0, 0);
}
// Track pupils using left eye
Vector2 pupilLeft = PupilPos(list, "left");
Vector2 pupilRight = PupilPos(list, "right");
return (pupilLeft + pupilRight) * .5f;
}
/// Calculate brow raise
public float GetBrowRaise(NormalizedLandmarkList list, string side)
{
var landmarks = list.Landmark;
int[] browPoints = side == "right" ? BrowRightPoints : BrowLeftPoints;
float browDistance = EyeLidRatio(
landmarks[browPoints[0]],
landmarks[browPoints[1]],
landmarks[browPoints[2]],
landmarks[browPoints[3]],
landmarks[browPoints[4]],
landmarks[browPoints[5]],
landmarks[browPoints[6]],
landmarks[browPoints[7]]
);
float maxBrowRatio = 1.15f;
float browHigh = .125f;
float browLow = .07f;
float browRatio = browDistance / maxBrowRatio - 1;
float browRaiseRatio = (Math.Clamp(browRatio, browLow, browHigh) - browLow) / (browHigh - browLow);
return browRaiseRatio;
}
// Take the average of left and right eyebrow raise
public float CalcBrow(NormalizedLandmarkList list)
{
var landmarks = list.Landmark;
if (landmarks.Count != 478)
return 0;
float leftBrow = GetBrowRaise(list, "left");
float rightBrow = GetBrowRaise(list, "right");
return (leftBrow + rightBrow) / 2;
}
}