-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
253 lines (209 loc) · 6.64 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
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
<html>
<head>
<title>Facemask (tfjs + facemesh)</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf-backend-wasm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="./triangulation.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
.canvas-wrapper {
position: relative;
z-index: 1;
}
#model_canvas {
position: static;
background: gold;
z-index: 2;
}
#threejs_canvas {
position: absolute;
z-index: 3;
}
.video_block {
position: absolute;
/* background: green; */
z-index: 2;
}
</style>
</head>
<body>
<div id="main">
<div class="container">
<div class="canvas-wrapper">
<canvas id="model_canvas"></canvas>
</div>
<div id="threejs_canvas"></div>
<div class="video_block">
<canvas id="output"></canvas>
<video id="video" playsinline style="
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
visibility: hidden;
width: auto;
height: auto;
">
</video>
</div>
</div>
</div>
<script>
const videoWidth = 300;
const videoHeight = 200;
const modelWidth = 256;
const modelHeight = 128;
const state = {
maxFaces: 1,
triangulateMesh: true
};
let model,model_canvas,video,ctx,canvas,camera;
// Threejs Init
const geometry = THREE.BufferGeometry();
const scene = new THREE.Scene();
const threejs_canvas = document.getElementById('threejs_canvas');
const threejs_render = new THREE.WebGLRenderer({ alpha: true } );
threejs_render.setPixelRatio(window.devicePixelRatio);
threejs_render.setSize(videoWidth, videoHeight);
threejs_render.setClearColor( 0x000000, 0 );
threejs_canvas.appendChild(threejs_render.domElement);
const material = new THREE.MeshBasicMaterial();
const mesh = new THREE.Mesh(geometry,material);
mesh.rotation.set(Math.PI,Math.PI,0);
scene.add(mesh);
tf.setBackend('wasm').then(() => main());
async function setupModelCanvas() {
model_canvas = document.getElementById('model_canvas');
model_canvas.width = modelWidth;
model_canvas.height = modelHeight;
let model_ctx = model_canvas.getContext('2d');
// let src = 'lena30.jpg';
let src = 'hair_biyou_kirei_ojiisan.png';
const img = await loadImage(src).catch(e => {
console.log('onload error', e);
});
model_ctx.drawImage(img,0,0,modelWidth,modelHeight);
}
function loadImage(src) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = (e) => reject(e);
img.src = src;
});
}
async function main() {
await setupModelCanvas();
await setupCamera();
video.play();
// threejs camera create
const fov = 1;
const fovRad = (fov / 2) * (Math.PI / 180);
const dist = (video.videoHeight / 2) / Math.tan(fovRad);
camera = new THREE.PerspectiveCamera(fov, video.videoWidth/ video.videoHeight, 1, dist * 2);
camera.position.z = dist;
camera.position.x = video.videoWidth/2 * -1;
camera.position.y = video.videoHeight/2 * -1;
canvas = document.getElementById('output');
canvas.width = videoWidth;
canvas.height = videoHeight;
const canvasContainer = document.querySelector('.video_block');
canvasContainer.style = `width: ${videoWidth}px; height: ${videoHeight}px`;
ctx = canvas.getContext('2d');
ctx.translate(canvas.width, 0);
ctx.scale(-1, 1);
ctx.fillStyle = '#32EEDB';
ctx.strokeStyle = '#32EEDB';
ctx.lineWidth = 0.5;
model = await facemesh.load({maxFaces: state.maxFaces});
await threejsRenderPrediction();
renderPrediction();
}
async function threejsRenderPrediction() {
const predictions = await model.estimateFaces(model_canvas);
if (predictions.length > 0) {
predictions.forEach(prediction => {
const keypoints = prediction.scaledMesh;
threejsCreateMesh(keypoints);
});
}
}
function threejsCreateMesh(keypoints) {
let texture = new THREE.CanvasTexture(model_canvas);
texture.flipY = false;
let pos = new Float32Array(
Array.prototype.concat.apply([],TRIANGULATION.map(index => keypoints[index])));
let uv = new Float32Array(
Array.prototype.concat.apply([],
TRIANGULATION.map(index =>
[keypoints[index][0] / modelWidth, keypoints[index][1] / modelHeight])));
mesh.geometry.setAttribute('position', new THREE.BufferAttribute(pos, 3));
mesh.geometry.setAttribute('uv', new THREE.BufferAttribute(uv, 2));
mesh.material = new THREE.MeshBasicMaterial({ map: texture ,
side: THREE.DoubleSide });
threejs_render.render(scene, camera);
}
function threejsUpdateMesh(keypoints) {
mesh.geometry.attributes.position.needsUpdate = true;
mesh.geometry.attributes.position.array = new Float32Array(
Array.prototype.concat.apply([],TRIANGULATION.map(index => keypoints[index])));
threejs_render.render(scene, camera);
}
async function setupCamera() {
video = document.getElementById('video');
const stream = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': {
facingMode: 'user',
// Only setting the video to a specified size in order to accommodate a
// point cloud, so on mobile devices accept the default size.
width: videoWidth,
height: videoHeight
},
});
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
};
});
}
async function renderPrediction() {
const predictions = await model.estimateFaces(video);
ctx.drawImage(
video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, canvas.width, canvas.height);
if (predictions.length > 0) {
predictions.forEach(prediction => {
const keypoints = prediction.scaledMesh;
// if (state.triangulateMesh) {
// for (let i = 0; i < TRIANGULATION.length / 3; i++) {
// const points = [
// TRIANGULATION[i * 3], TRIANGULATION[i * 3 + 1],
// TRIANGULATION[i * 3 + 2]
// ].map(index => keypoints[index]);
// drawPath(ctx, points, true);
// }
// }
threejsUpdateMesh(keypoints);
});
}
requestAnimationFrame(renderPrediction);
}
function drawPath(ctx, points, closePath) {
const region = new Path2D();
region.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
const point = points[i];
region.lineTo(point[0], point[1]);
}
if (closePath) {
region.closePath();
}
ctx.stroke(region);
}
</script>
</body>
</html>