forked from zootalures/fn-faces-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
346 lines (281 loc) · 11.6 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
<html>
<head>
<title>Object Storage -> Cloud Events -> Serverless</title>
</head>
<body style="background: #fcfcfc; color: #222;">
<div style="margin: auto; width: 85%; padding: 1em; text-align: center">
<h3>Object Storage -> Cloud Events -> Serverless</h3>
</div>
<div style="display: flex; flex-direction: column">
<div style="display: flex; flex-direction: row">
<div id="playerParent" style="margin: 1em; flex-grow: 2; flex-basis: 70%">
<video id="player" style="margin: 0.25em; width: 100%;" playsinline autoplay></video>
<button id="capture" style="margin: 0.25em; width: 100%; height: 2em; font-size: 1em; alignment: center">Capture</button>
</div>
<div style="margin: 1em; flex-grow: 1">
<p><strong>Instructions:</strong> <em>Click the Webcam video or the "Capture" button to take a picture!</em></p>
<p>This demo shows integration between the <strong>Object Storage</strong>, <strong>Cloud Events</strong>,
and <strong>Serverless Functions</strong> services. Pictures will be uploaded to Object Storage.
On every upload, a Cloud Event is generated. The event triggers a Serverless Function.</p>
<p id="description">The function applies a filter and uploads it to the output bucket</p>
</div>
</div>
<div>
<table>
<thead>
<tr style="padding: 5px">
<td style="font-size: 1.2em; text-align: center">Captured Images</td>
<td style="font-size: 1.2em; text-align: center">Processed Images</td>
</tr>
</thead>
<tbody id="tablebody">
</tbody>
</table>
</div>
</div>
<style type="text/css">
h2, h3, h4, h5, h6 {
margin: 0 0 10px;
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
font-weight: 400;
color: #222;
text-transform: none;
}
table, th {
border-collapse: collapse;
border: 1px solid black;
width: 100%;
margin-left: auto;
margin-right: auto;
}
td {
border: 1px solid black;
width: 50%;
align: center;
}
.smallimage {
width: 90%;
height: auto;
display: block;
margin: 5px;
margin-left: auto;
margin-right: auto;
border: #B9CEEA solid 5px;
}
.spinner {
width: 40px;
height: 40px;
position: relative;
margin: 100px auto;
}
.double-bounce1, .double-bounce2 {
width: 100%;
height: 100%;
border-radius: 50%;
background-color: #333;
opacity: 0.6;
position: absolute;
top: 0;
left: 0;
-webkit-animation: sk-bounce 2.0s infinite ease-in-out;
animation: sk-bounce 2.0s infinite ease-in-out;
}
.double-bounce2 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
@-webkit-keyframes sk-bounce {
0%, 100% { -webkit-transform: scale(0.0) }
50% { -webkit-transform: scale(1.0) }
}
@keyframes sk-bounce {
0%, 100% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 50% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
}
}
</style>
<script type="module">
const player = document.getElementById('player');
const playerParent = document.getElementById('playerParent');
const captureButton = document.getElementById('capture');
const urlParams = new URLSearchParams(window.location.search);
const constraints = {
video: true,
};
// Pre-Authenticated Request URL for uploading into the "facedetection-incoming" bucket.
// Please follow the instructions on how to create these URLs
const sombreroSourceBucketURLBase = "Pre-Authenticated Request URL";
const sombreroOutputURLBase = "Public URL";
// Calculate the upload URL to put a file into the source bucket.
function putImageURL(inputImageFileName) {
return `${sombreroSourceBucketURLBase}${inputImageFileName}`
}
// Calculate the output file name based on the input file name.
function outputImageURL(inputImageFileName) {
return sombreroOutputURLBase + inputImageFileName;
}
// pollForImage() performs an HTTP get to *imageURL*. On success it clears out all
// children from *parentElement* and takes the content on the response and adds it
// as a child to the element.
//
// On 404, it will retry again in 1 second up to *retries* number of times.
function pollForImage(imageURL, retries, widthHeightParent) {
let parentElement = widthHeightParent["parent"];
if (retries === 0) {
while (parentElement.firstChild) { parentElement.removeChild(parentElement.firstChild) }
let para = document.createElement("P");
para.innerText = "Timed out waiting for image :(";
parentElement.appendChild(para);
return;
}
// Asynchronously fetch.
let getRequest = new XMLHttpRequest();
getRequest.open("GET", imageURL, true);
getRequest.onload = function (oEvent) {
switch (getRequest.status) {
case 200: {
while (parentElement.firstChild) { parentElement.removeChild(parentElement.firstChild) }
let image = new Image(widthHeightParent["width"], widthHeightParent["heigth"]);
image.src = imageURL;
image.className = "smallimage";
parentElement.appendChild(image);
break;
}
case 404: {
document.defaultView.setTimeout(pollForImage, 1000, imageURL, retries - 1, widthHeightParent);
break;
}
default: {
console.log(`pollForImage() received unexpected code ${getRequest.status}`);
// On other HTTP error codes we stop the polling process immediately.
while (parentElement.firstChild) { parentElement.removeChild(parentElement.firstChild) }
let para = document.createElement("P");
para.innerText = "Error :(";
parentElement.appendChild(para);
break;
}
}
};
getRequest.send();
}
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
// makeSpinner makes a div that shows a spinner. It looks like this.
//
// <div class="spinner">
// </div class="double-bounce1">
// </div class="double-bounce2">
// </div>
//
// Add this as a child to your thing.
function makeSpinner() {
// Draw a spinner in the right cell.
let spinner = document.createElement("DIV");
spinner.className = "spinner";
let b1 = document.createElement("DIV");
b1.className = "double-bounce1";
let b2 = document.createElement("DIV");
b2.className = "double-bounce1";
spinner.appendChild(b1);
spinner.appendChild(b2);
return spinner;
}
let captureImage = function () {
player.removeAttribute("controls");
// Every time the capture button is clicked we add a row at the top of the table. On the left size
// we insert a canvas object and drop the image into the canvas. On the right side we'll wait for the
// image to show up in the processed state and then we'll load it.
const tableBody = document.getElementById('tablebody');
// Insert a row
let row = tableBody.insertRow(0);
let left = row.insertCell();
let right = row.insertCell();
// Get the dimensions of the video track. The video stream we get will
// probably be more than 320 pixels wide or 240 pixels tall. Larger
// image sizes increase the amount of time spent doing image analysis.
// We are going to cap the dimensions of the input image to at most
// 320 wide and at most 240 tall, while also respecting the original
// aspect ratio (width / height).
let tracks = player.srcObject.getVideoTracks();
let videoSettings = tracks[0].getSettings();
let heightRatio = 240 / videoSettings.height;
let widthRatio = 320 / videoSettings.width;
// Choose the lesser of the two ratios to use as scaling factor.
let lesserRatio = heightRatio;
if (heightRatio > widthRatio) {
lesserRatio = widthRatio;
}
let dimensionsAndParent = {
"width": lesserRatio * videoSettings.width,
"height": lesserRatio * videoSettings.height,
"parent": right
};
// Draw the video frame to the canvas.
let canvas = document.createElement("CANVAS");
canvas.width = dimensionsAndParent.width;
canvas.height = dimensionsAndParent.height;
canvas.className = "smallimage";
let context = canvas.getContext('2d');
context.drawImage(player, 0, 0, canvas.width, canvas.height);
left.append(canvas);
if (urlParams.has("debug")) {
let debugpar = document.createElement("P");
debugpar.innerText = `aspect ratio is ${videoSettings.aspectRatio}, videoSettings.width=${videoSettings.width}, videoSettings.height=${videoSettings.height} width=${dimensionsAndParent.width}, height=${dimensionsAndParent.height}`;
left.appendChild(debugpar)
}
// Draw a spinner in the right cell.
right.appendChild(makeSpinner());
let dataUrl = canvas.toDataURL("image/jpeg");
let byteString = atob(dataUrl.split(',')[1]);
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
// create a view into the buffer
var ia = new Uint8Array(ab);
// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: "image/jpeg"});
var oReq = new XMLHttpRequest();
let fileId = guid() + ".jpg";
oReq.open("PUT", putImageURL(fileId), true);
oReq.onload = function (oEvent) {
var outputURL = outputImageURL(fileId);
console.log(`Uploaded ${fileId}, polling for ${outputURL}, status=${oReq.status}`);
pollForImage(outputURL, 100, dimensionsAndParent);
};
oReq.send(blob);
};
player.addEventListener('click', () => {
captureImage();
});
captureButton.addEventListener('click', () => {
captureImage();
});
// Attach the video stream to the video element and autoplay.
navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => {
player.srcObject = stream;
player.play();
})
.catch( (err) => {
while (playerParent.firstChild) { playerParent.removeChild(playerParent.firstChild) }
let para = document.createElement("P");
para.innerText = "Video Camera not available. Try enabling camera for the browser and restarting the page.";
para.style.textAlign = "center";
playerParent.appendChild(para);
});
</script>
</body>
</html>