-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
213 lines (187 loc) · 8.18 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Understanding MediaPipe Face Mesh Output</title>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/drawing_utils.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/face_mesh.min.js"></script>
<script src="face.png.js"></script>
<script src="uvcoords.js"></script>
<script type="module">
// --- Constants
const outputSize = 4000;
const colorBackground = { color: "#E0E0E0" };
const colorTriangles = { color: "#C0C0C070", lineWidth: 4 };
const colorSilhouette = { color: "#30FFFF70", lineWidth: 4 };
const colorRightEye = { color: "#FF303070", lineWidth: 4 };
const colorLeftEye = { color: "#30FF3070", lineWidth: 4 };
const colorLips = { color: "#3030FF70", lineWidth: 4 };
// --- Load Image
const image = new Image();
image.crossOrigin = "anonymous";
image.src = FACE_PNG_DATA_URL;
await image.decode();
// --- Get FaceMesh landmarks
const faceMesh = new FaceMesh({
locateFile: (file) =>
`https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh@${VERSION}/${file}`,
});
let unrefinedLandmarks = undefined;
faceMesh.setOptions({ staticImageMode: true, enableFaceGeometry: true });
faceMesh.onResults((results) => (unrefinedLandmarks = results));
await faceMesh.send({ image });
console.assert(unrefinedLandmarks.multiFaceLandmarks.length == 1);
console.assert(unrefinedLandmarks.multiFaceGeometry.length == 1);
const meshProto = unrefinedLandmarks.multiFaceGeometry[0].getMesh();
const mesh = {
primitiveType: meshProto.getPrimitiveType(),
vertexType: meshProto.getVertexType(),
vertexBufferList: Array.from(meshProto.getVertexBufferList()),
indexBufferList: Array.from(meshProto.getIndexBufferList()),
};
const poseTransformMatrixProto =
unrefinedLandmarks.multiFaceGeometry[0].getPoseTransformMatrix();
const poseTransformMatrix = matrixDataToMatrix(poseTransformMatrixProto);
let refinedLandmarks = undefined;
faceMesh.setOptions({ refineLandmarks: true, enableFaceGeometry: false });
faceMesh.onResults((results) => (refinedLandmarks = results));
await faceMesh.send({ image });
console.assert(refinedLandmarks.multiFaceLandmarks.length == 1);
console.assert(refinedLandmarks.multiFaceGeometry.length == 0);
// --- Create document
const button = document.createElement("button");
button.append(document.createTextNode("Download next"));
document.body.append(button);
document.body.append(document.createElement("br"));
const downloadLink = document.createElement("a");
// Not appended to document because we click it via .click() and not user.
const canvas = document.createElement("canvas");
canvas.width = outputSize;
canvas.height = outputSize;
document.body.append(canvas);
const ctx = canvas.getContext("2d");
// --- Download function
function downloadImage(fileName) {
downloadLink.setAttribute("download", fileName);
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob);
downloadLink.setAttribute("href", url);
downloadLink.click();
});
}
function downloadText(fileName, content) {
downloadLink.setAttribute("download", fileName);
downloadLink.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(content)
);
downloadLink.click();
}
// --- Configuration to draw
const configs = [
{ uvCoords: false, face: false, refined: false, numbered: false },
{ uvCoords: false, face: false, refined: false, numbered: true },
{ uvCoords: false, face: false, refined: true, numbered: false },
{ uvCoords: false, face: false, refined: true, numbered: true },
{ uvCoords: false, face: true, refined: false, numbered: false },
{ uvCoords: false, face: true, refined: false, numbered: true },
{ uvCoords: false, face: true, refined: true, numbered: false },
{ uvCoords: false, face: true, refined: true, numbered: true },
{ uvCoords: true, face: false, refined: false, numbered: false },
{ uvCoords: true, face: false, refined: false, numbered: true },
];
// --- Generate next image on button click and download it
let curConfig = -4;
button.addEventListener("click", function () {
button.disabled = true;
if (curConfig === -4) {
downloadText(
"landmarks.json",
JSON.stringify(unrefinedLandmarks.multiFaceLandmarks[0], null, 2)
);
++curConfig;
} else if (curConfig === -3) {
downloadText(
"refined-landmarks.json",
JSON.stringify(refinedLandmarks.multiFaceLandmarks[0], null, 2)
);
++curConfig;
} else if (curConfig === -2) {
downloadText(
"geometry.json",
JSON.stringify(
{
mesh,
poseTransformMatrix,
},
null,
2
)
);
++curConfig;
} else if (curConfig === -1) {
ctx.fillStyle = colorBackground.color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
downloadImage("face.png");
++curConfig;
} else if (curConfig === configs.length) {
alert("Done!");
} else {
const config = configs[curConfig];
++curConfig;
console.assert(!(config.uvCoords && config.face));
console.assert(!(config.uvCoords && config.refined));
let filenameParts = [];
if (config.uvCoords) filenameParts.push("uvCoords");
if (config.face) filenameParts.push("face");
if (config.refined) filenameParts.push("refined");
if (!config.uvCoords) filenameParts.push("landmarks");
if (config.numbered) filenameParts.push("numbered");
const filename = filenameParts.join("-") + ".png";
console.log(`Drawing ${filename}`);
let landmarks = unrefinedLandmarks.multiFaceLandmarks[0];
if (config.refined)
landmarks = refinedLandmarks.multiFaceLandmarks[0];
else if (config.uvCoords) landmarks = FACEMESH_UVCOORDS;
ctx.fillStyle = colorBackground.color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (config.face)
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
drawConnectors(ctx, landmarks, FACEMESH_TESSELATION, colorTriangles);
drawConnectors(ctx, landmarks, FACEMESH_FACE_OVAL, colorSilhouette);
drawConnectors(ctx, landmarks, FACEMESH_RIGHT_EYE, colorRightEye);
drawConnectors(ctx, landmarks, FACEMESH_RIGHT_EYEBROW, colorRightEye);
drawConnectors(ctx, landmarks, FACEMESH_RIGHT_IRIS, colorRightEye);
drawConnectors(ctx, landmarks, FACEMESH_LEFT_EYE, colorLeftEye);
drawConnectors(ctx, landmarks, FACEMESH_LEFT_EYEBROW, colorLeftEye);
drawConnectors(ctx, landmarks, FACEMESH_LEFT_IRIS, colorLeftEye);
drawConnectors(ctx, landmarks, FACEMESH_LIPS, colorLips);
if (config.numbered) {
ctx.font = "10px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.fillStyle = "white";
for (const [i, landmark] of landmarks.entries()) {
ctx.strokeText(
i.toString(),
landmark.x * canvas.width,
landmark.y * canvas.height
);
ctx.fillText(
i.toString(),
landmark.x * canvas.width,
landmark.y * canvas.height
);
}
}
downloadImage(filename);
}
button.disabled = false;
});
</script>
</head>
<body></body>
</html>