-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.html
368 lines (324 loc) · 13 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
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
<!doctype html>
<html>
<head>
<title>Camera Project</title>
<meta charset="utf-8">
<style>
#overlay {
position: absolute;
top: 0px;
left: 0px;
-o-transform : scaleX(-1);
-webkit-transform : scaleX(-1);
transform : scaleX(-1);
-ms-filter : fliph; /*IE*/
filter : fliph; /*IE*/
}
#camera {
-o-transform : scaleX(-1);
-webkit-transform : scaleX(-1);
transform : scaleX(-1);
-ms-filter : fliph; /*IE*/
filter : fliph; /*IE*/
border: 1px black dotted;
}
#tryon {
position : relative;
margin : 0px auto;
}
#start {
margin:0 auto;
width:100px;
display: block;
}
#debug {
width:640px;
margin: 0 auto;
font-family: "Courier New", Courier, monospace;
font-size: 11px;
line-height:12px;
}
</style>
</head>
<body>
<!-- jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- three.js r54 -->
<script src="js/three.js"></script>
<!-- clmtrack -->
<script src="js/utils.js"></script>
<script src="js/clmtrackr.js"></script>
<div id="tryon">
<video id="camera" loop></video>
<canvas id="overlay"></canvas>
</div>
<button id="start">Start</button>
<div id="debug"></div>
<script>
/**
* Requires:
* three.js (r54) http://threejs.org/
* clmtrack https://github.com/auduno/clmtrackr
*
* @author Yevhen Matasar <matasar.ei@gmail.com>
*
* @param {Object} params
*
* @version 20190615
*/
var TryOnFace = function (params) {
var ref = this;
this.selector = 'tryon';
//sizes
this.object = params.object;
this.width = params.width;
this.height = params.height;
if (params.statusHandler) {
this.statusHandler = params.statusHandler;
} else {
this.statusHandler = function(){};
}
this.changeStatus = function(status) {
this.status = status;
this.statusHandler(this.status);
};
this.changeStatus('STATUS_READY');
if (params.debug) {
this.debug = true;
this.debugMsg = this.status;
} else {
this.debug = false;
}
/* CAMERA */
this.video = document.getElementById('camera');
document.getElementById(this.selector).style.width = this.width + "px";
this.video.setAttribute('width', this.width);
this.video.setAttribute('height', this.height);
/* face tracker */
this.tracker = new clm.tracker({useWebGL: true});
this.tracker.init();
/**
* Start try-on
* @returns {undefined}
*/
this.start = function() {
var video = ref.video;
navigator.getUserMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia
);
if (navigator.getUserMedia) {
navigator.getUserMedia(
{
video: true
},
function (localMediaStream) {
video.srcObject = localMediaStream;
video.play();
ref.changeStatus('STATUS_CAMERA_ERROR');
},
function (err) {
ref.changeStatus('STATUS_CAMERA_ERROR');
}
);
} else {
ref.changeStatus('STATUS_CAMERA_ERROR');
}
//start tracking
ref.tracker.start(video);
//continue in loop
ref.loop();
};
this.debug = function(msg) {
if (this.debug) {
this.debugMsg += msg + "<br>";
}
};
this.printDebug = function() {
if (this.debug) {
document.getElementById('debug').innerHTML = this.debugMsg;
this.debugMsg = '';
}
};
this.loop = function() {
requestAnimFrame(ref.loop);
var positions = ref.tracker.getCurrentPosition();
if (positions) {
//current distance
var distance = Math.abs(90 / ((positions[0][0].toFixed(0) - positions[14][0].toFixed(0)) / 2));
//horizontal angle // горизонтальный угол (поворот лица)
var hAngle = 90 - (positions[14][0].toFixed(0) - positions[33][0].toFixed(0)) * distance;
//center point
var center = {
x: positions[33][0],
y: (positions[33][1] + positions[41][1]) / 2
};
center = ref.correct(center.x, center.y);
var zAngle = (positions[33][0] - positions[7][0]) * -1;
//allowable distance
if (distance < 1.5 && distance > 0.5) {
ref.changeStatus('STATUS_FOUND');
//set positions
ref.position.x = center.x - (hAngle / 2);
ref.position.y = center.y;
ref.rotation.y = hAngle / 100 / 2;
ref.rotation.z = zAngle / 100 / 1.5;
//size
ref.size.x = ((positions[14][0] - positions[0][0]) / 2) + 0.05 * (positions[14][0] - positions[0][0]);
ref.size.y = (ref.size.x / ref.images['front'].width) * ref.images['front'].height;
ref.size.z = ref.size.x * 3;
ref.position.z = (ref.size.z / 2) * -1;
//render
} else {
ref.changeStatus('STATUS_SEARCH');
ref.size.x = 0;
ref.size.y = 0;
}
ref.render();
ref.debug(ref.status);
}
//print debug
ref.printDebug();
};
/* 3D */
var canvas = document.getElementById("overlay");
var renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
alpha: true
});
renderer.setClearColor(0xffffff, 0);
renderer.setSize(this.width, this.height);
//add scene
var scene = new THREE.Scene;
//define sides
var outside = {
0 : 'left',
1 : 'right',
4 : 'front'
};
this.images = [];
var materials = [];
for (i = 0; i < 6; i++) {
if (this.object.outside[outside[i]] !== undefined) {
var image = new Image();
image.src = this.object.outside[outside[i]];
this.images[outside[i]] = image;
materials.push(new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture(this.object.outside[outside[i]]), transparent: true
}));
} else {
materials.push(new THREE.MeshLambertMaterial({
color: 0xffffff, transparent: true, opacity: 0
}));
}
}
//init position and size
this.position = {
x: 0,
y: 0,
z: 0
};
this.rotation = {
x: 0,
y: 0
};
this.size = {
x: 1,
y: 1,
z: 1
};
//set up object
var geometry = new THREE.CubeGeometry(1, 1, 1);
var materials = new THREE.MeshFaceMaterial(materials);
var cube = new THREE.Mesh( geometry, materials );
cube.doubleSided = true;
scene.add(cube);
//set up camera
var camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 5000);
camera.lookAt(cube.position);
camera.position.z = this.width / 2;
scene.add(camera);
//set up lights
var lightFront = new THREE.PointLight(0xffffff);
lightFront.position.set(0, 0, 1000);
lightFront.intensity = 0.6;
scene.add(lightFront);
var lightLeft = new THREE.PointLight(0xffffff);
lightLeft.position.set(1000, 0, 0);
lightLeft.intensity = 0.7;
scene.add(lightLeft);
var lightRight = new THREE.PointLight(0xffffff);
lightRight.position.set(-1000, 0, 0);
lightRight.intensity = 0.7;
scene.add(lightRight);
/**
* Render object
*/
this.render = function() {
//update position
cube.position.x = this.position.x;
cube.position.y = this.position.y;
cube.position.z = this.position.z;
cube.rotation.y = this.rotation.y;
cube.rotation.z = this.rotation.z;
//upate size
cube.scale.x = this.size.x;
cube.scale.y = this.size.y;
cube.scale.z = this.size.z;
renderer.render(scene, camera);
};
/**
* Transform position for 3D scene
*/
this.correct = function(x, y) {
return {
x: ((this.width / 2 - x) * -1) / 2,
y: (this.height / 2 - y) / 2
};
}
//print debug
this.printDebug();
};
var tryOn = null;
$(window).load(function () {
$('#start').hide();
var object = {
outside: {
left: './glasses/left.png',
right: './glasses/right.png',
front: './glasses/front.png'
}
};
tryOn = new TryOnFace({
width: 640,
height: 480,
debug: true,
object: object,
statusHandler: function(status) {
switch(status) {
case "STATUS_READY": {
/* Ready! Show start button or something... */
$('#start').show();
}; break;
case "STATUS_CAMERA_ERROR": {
/* Handle camera error */
}; break;
case "STATUS_SEARCH": {
/* Show some message while searching a face */
}; break;
case "STATUS_FOUND": {
/* OK! */
}
}
}
});
$('#start').click(function() {
tryOn.start();
});
});
</script>
</body>
</html>