forked from cityscreen/deep-webXR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (266 loc) · 9.45 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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<title>DeepAR</title>
<style>
canvas.deepar {
border: 0px none;
background-color: black;
display: block;
margin: auto;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
body {
margin: 0px;
padding: 0px;
}
#loader-wrapper {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background-color: #fff;
text-align: center;
}
.loader {
display: inline-block;
position: relative;
top: 42%;
z-index: 1000;
width: 90px;
height: 90px;
margin: auto;
background-color: #00f;
border-radius: 100%;
-webkit-animation: sk-scaleout 1.5s infinite ease-in-out;
animation: sk-scaleout 1.5s infinite ease-in-out;
}
@-webkit-keyframes sk-scaleout {
0% { -webkit-transform: scale(0) }
100% {
-webkit-transform: scale(1.0);
opacity: 0;
}
}
@keyframes sk-scaleout {
0% {
-webkit-transform: scale(0);
transform: scale(0);
} 100% {
-webkit-transform: scale(1.0);
transform: scale(1.0);
opacity: 0;
}
}
.effect-carousel {
position: fixed !important;
width: 100%;
height: 130px;
bottom: 0px;
z-index: 999999;
background-color: rgba(255, 255, 255, 0.7);
}
.thumb {
margin-top: 15px;
margin-bottom: 15px;
margin-right: auto;
margin-left: auto;
position: relative;
opacity: 0.8;
transition: all 300ms ease;
height: 100px;
}
.slick-center .thumb {
-moz-transform: scale(1.3);
-ms-transform: scale(1.3);
-o-transform: scale(1.3);
-webkit-transform: scale(1.3);
color: #e67e22;
opacity: 1;
transform: scale(1.3);
}
.slick-slide {
width: 130px;
}
#recording-btn {
position: fixed;
top: 0px;
left: 0px;
width: 200px;
height: 80px;
z-index: 99999;
}
</style>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>
</head>
<body>
<button id="recording-btn">Start/stop video recording</button>
<canvas class="deepar" id="deepar-canvas" oncontextmenu="event.preventDefault()"></canvas>
<div id="loader-wrapper">
<span class="loader"></span></span>
</div>
<div class="effect-carousel">
<div><img class="thumb" src="thumbs/galaxy.png"></div>
<div><img class="thumb" src="thumbs/aviators.png"></div>
<div><img class="thumb" src="thumbs/beard.png"></div>
<div><img class="thumb" src="thumbs/dalmatian.png"></div>
<div><img class="thumb" src="thumbs/flowers.png"></div>
<div><img class="thumb" src="thumbs/koala.png"></div>
<div><img class="thumb" src="thumbs/lion.png"></div>
<div><img class="thumb" src="thumbs/teddy_cigar.png"></div>
</div>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
<script type="text/javascript" src="lib/deepar.js"></script>
<script type="text/javascript">
var canvasHeight = window.innerHeight;
var canvasWidth = window.innerWidth;
// desktop, the width of the canvas is 0.66 * window height and on mobile it's fullscreen
if (window.innerWidth > window.innerHeight) {
canvasWidth = Math.floor(window.innerHeight*0.66);
}
var deepAR = DeepAR({
canvasWidth: canvasWidth,
canvasHeight: canvasHeight,
licenseKey: 'your_license_key_goes_here',
canvas: document.getElementById('deepar-canvas'),
numberOfFaces: 1,
libPath: './lib',
segmentationInfoZip: 'segmentation.zip',
onInitialize: function() {
// start video immediately after the initalization, mirror = true
deepAR.startVideo(true);
// or we can setup the video element externally and call deepAR.setVideoElement (see startExternalVideo function below)
deepAR.switchEffect(0, 'slot', './effects/background_segmentation', function() {
// effect loaded
});
}
});
deepAR.onCameraPermissionAsked = function() {
console.log('camera permission asked');
};
deepAR.onCameraPermissionGranted = function() {
console.log('camera permission granted');
};
deepAR.onCameraPermissionDenied = function() {
console.log('camera permission denied');
};
deepAR.onScreenshotTaken = function(photo) {
console.log('screenshot taken');
};
deepAR.onImageVisibilityChanged = function(visible) {
console.log('image visible', visible);
};
deepAR.onFaceVisibilityChanged = function(visible) {
console.log('face visible', visible);
};
deepAR.onVideoStarted = function() {
var loaderWrapper = document.getElementById('loader-wrapper');
loaderWrapper.style.display = 'none';
};
deepAR.downloadFaceTrackingModel('lib/models-68-extreme.bin');
let isRecording = false;
document.getElementById('recording-btn').onclick = function(e) {
if(!isRecording) {
isRecording = true;
deepAR.startVideoRecording();
console.log("Recording started!");
} else {
deepAR.finishVideoRecording(function(video) {
window.open(URL.createObjectURL(video), '_blank').focus();
console.log("Recording finished!");
isRecording = false;
});
}
};
// Store video objects for cleanup
var videoObjects = {};
function startExternalVideo() {
cleanupVideoObjects();
// create video element
var video = document.createElement('video');
video.muted = true;
video.loop = true;
video.controls = true;
video.setAttribute('playsinline', 'playsinline');
video.style.width = '100%';
video.style.height = '100%';
// put it somewhere in the DOM
var videoContainer = document.createElement('div');
videoContainer.appendChild(video);
videoContainer.style.width = '1px';
videoContainer.style.height = '1px';
videoContainer.style.position = 'absolute';
videoContainer.style.top = '0px';
videoContainer.style.left = '0px';
videoContainer.style['z-index'] = '-1';
document.body.appendChild(videoContainer);
videoObjects.videoContainer = videoContainer;
videoObjects.video = video;
navigator.mediaDevices.getUserMedia({video: true}).then(function(stream) {
try {
video.srcObject = stream;
} catch (error) {
video.src = URL.createObjectURL(stream);
}
setTimeout(function() {
video.play();
}, 50);
}).catch(function(error) {
console.log('error in video play:', error);
});
// tell the DeepAR SDK about our new video element
deepAR.setVideoElement(video, true);
var loaderWrapper = document.getElementById('loader-wrapper');
loaderWrapper.style.display = 'none';
}
function cleanupVideoObjects() {
if (videoObjects.length > 0){
videoObjects.videoContainer.parentNode.removeChild(videoObjects.videoContainer);
videoObjects.videoContainer = null;
if (videoObjects.video.srcObject) {
//getUserMedia starts a stream, all tracks on all streams need to be stoppedbefore calling getUserMedia again
videoObjects.video.srcObject.getTracks().forEach(track => track.stop())
}
videoObjects.video.pause();
videoObjects = {};
}
}
// Position the carousel to cover the canvas
if (window.innerWidth > window.innerHeight) {
var width = Math.floor(window.innerHeight*0.66);
var carousel = document.getElementsByClassName('effect-carousel')[0];
carousel.style.width = width + 'px';
carousel.style.marginLeft = (window.innerWidth-width)/2 + "px";
}
$(document).ready(function() {
$('.effect-carousel').slick({
slidesToShow: 1,
centerMode: true,
focusOnSelect: true,
arrows: false,
accessibility: false,
variableWidth: true
});
var effects = [
'./effects/background_segmentation',
'./effects/aviators',
'./effects/beard',
'./effects/dalmatian',
'./effects/flowers',
'./effects/koala',
'./effects/lion',
'./effects/teddycigar'
];
$('.effect-carousel').on('afterChange', function(event, slick, currentSlide){
deepAR.switchEffect(0, 'slot', effects[currentSlide]);
});
});
</script>
</body>
</html>