-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
455 lines (382 loc) · 13.3 KB
/
script.js
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
var tmImage = window.tmImage;
var _ = window._;
var clipboard = window.clipboard;
async function main() {
const exampleURL = 'https://teachablemachine.withgoogle.com/models/oX1iyaiu/';
document.querySelector('.load-example-link').href = `?model=${encodeURIComponent(exampleURL)}`;
const outEl = document.querySelector('.TileTwo');
// init from query string
const queryString = window.location.search;
if (queryString.indexOf('?model=') === 0) {
const modelKey = decodeURIComponent(queryString.slice(7));
document.querySelector('#model-key').value = modelKey;
initForModelKey(outEl, modelKey);
}
// handler for loading model
document.querySelector('#model-button').addEventListener('click', e => {
const modelKey = document.querySelector('#model-key').value;
initForModelKey(outEl, modelKey);
});
// dump out predictions
document.querySelector('#load-dump-json').addEventListener('change', async function(event) {
const files = await readInputFilesAsText(event.target.files);
const items = JSON.parse(files[0]);
const predictionsCount = _.uniq(_.flatMap(items, item => item.prediction.map(p => p.className)));
renderItems(outEl, predictionsCount, items);
});
}
async function loadModel(modelKey) {
// the json file (model topology) has a reference to the bin file (model weights)
const checkpointURL = `${modelKey}model.json`;
// the metatadata json file contains the text labels of your model and additional information
const metadataURL = `${modelKey}metadata.json`;
console.log('checkpointURL', checkpointURL);
console.log('metadataURL', metadataURL);
// load the model and metadata
const model = await tmImage.load(checkpointURL, metadataURL);
const maxPredictions = model.getTotalClasses();
return {model, maxPredictions};
}
async function init(outEl, model, maxPredictions) {
document.querySelector('#dump').disabled = 'disabled';
// state across changes to dataset
var items = [];
// load images
document.querySelector('#file-selector').addEventListener('change', async function(event) {
const files = await readInputFilesAsDataURL(event.target.files);
console.log(' loaded files:', files.length);
// predict
for (var i = 0; i < files.length; i++) {
const {prediction} = await predictForUri(model, maxPredictions, files[i].uri);
items.push({
source: 'disk',
prediction,
uri: files[i].uri,
filename: files[i].filename,
filenameLabel: guessFilenameLabel(files[i].filename)
});
}
// render
renderItems(outEl, maxPredictions, items);
});
// search
document.querySelector('#search-button').addEventListener('click', async function(e) {
const query = prompt('Search for:');
if (!query) return;
const json = await fetchJson(query, 'abc');
const uris = (json.items || []).map(item => item.image.thumbnailLink);
// const uris = (json.items || []).map(item => item.link);
console.log(' search results:', uris.length);
// predict
for (var i = 0; i < uris.length; i++) {
const {prediction} = await predictForUri(model, maxPredictions, uris[i]);
items.push({
source: 'search',
prediction,
query,
uri: uris[i]
});
}
// render
renderItems(outEl, maxPredictions, augmented(items));
});
// webcam
var webcamEl = null;
document.querySelector('#webcam-button').addEventListener('click', async function(e) {
if (webcamEl) {
webcamEl.captureStream().getTracks().forEach(track => track.stop());
if (webcamEl.parentElement) webcamEl.parentElement.removeChild(webcamEl);
webcamEl = null;
return;
}
webcamEl = await startWebcam(outEl);
var startTimestamp = (new Date()).getTime();
// loop
const WEBCAM_SNAP_INTERVAL = 4000;
async function tick() {
// snapshot and predict
const {prediction, uri} = await readAndPredictFromWebcam(webcamEl, model, maxPredictions);
const timestamp = (new Date()).getTime();
console.log('ts:', startTimestamp, timestamp, Math.round((timestamp - startTimestamp)/1000/60));
items.push({
source: 'webcam',
camera: true,
prediction,
uri,
timestamp,
elapsedSeconds: Math.round((timestamp - startTimestamp)/1000)
});
// render
renderItems(outEl, maxPredictions, augmented(items));
if (webcamEl) setTimeout(tick, WEBCAM_SNAP_INTERVAL);
}
tick();
});
}
async function predictForUri(model, maxPredictions, uri) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.onload = async function() {
const prediction = await model.predict(img, false, maxPredictions);
resolve({prediction, uri});
};
img.crossOrigin = "Anonymous"; // allow images from search to be read by model
img.src = uri;
});
}
// items [{prediction, uri}]
function renderItems(targetEl, maxPredictions, items) {
const facetsEl = targetEl.querySelector('.Facets');
facets(facetsEl, items);
allowDump(targetEl, items);
}
function allowDump(outEl, items) {
const buttonEl = document.querySelector('#dump');
buttonEl.disabled = false;
const clonedButtonEl = buttonEl.cloneNode(true);
buttonEl.parentNode.replaceChild(clonedButtonEl, buttonEl);
clonedButtonEl.addEventListener('click', function(e) {
var dt = new clipboard.DT();
dt.setData("text/plain", JSON.stringify(items));
clipboard.write(dt);
console.log(JSON.stringify(items));
download('data:application/json,' + JSON.stringify(items), 'analysis.json');
})
}
// eg 'english_setter_2.jpg'
function guessFilenameLabel(filename) {
return filename.split(/[\._]/).slice(0, -1).filter(s => !s.match(/\d+/)).join('_');
}
// webcam
async function startWebcam(outEl) {
// webcam has a square ratio and is flipped by default to match training
const webcamFlipped = true;
const webcamEl = await tmImage.getWebcam(200, 200, 'front', webcamFlipped);
webcamEl.play();
// outEl.appendChild(webcamEl);
return webcamEl;
}
async function readAndPredictFromWebcam(webcamEl, model, maxPredictions) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.drawImage(webcamEl, 0, 0, 200, 200);
// document.body.appendChild(canvas);
const prediction = await model.predict(canvas, false, maxPredictions);
const uri = canvas.toDataURL();
return {prediction, uri};
}
// // predict can take in an image, video or canvas html element
// // we set flip to true since the webcam was only flipped in CSS
// const flip = true;
// const prediction = await model.predict(webcamEl, flip, maxPredictions);
// return prediction;
// }
// facets
function textLabelForClassName(className) {
return `${className} score`;
}
async function facets(targetEl, items) {
// flatten data, round numbers for UX
const facetsData = items.map(function(item, i) {
const classification = _.maxBy(item.prediction, 'probability').className;
const labels = item.prediction.reduce((map, p) => {
return {
...map,
[textLabelForClassName(p.className)]: parseFloat(p.probability.toFixed(4))
};
}, {});
return {
i,
classification,
hashedURI: hash64(item.uri),
source: item.source || 'uknown',
filename: item.filename,
filenameLabel: item.filenameLabel,
searchQuery: item.query,
elapsedSeconds: item.elapsedSeconds,
...labels
};
});
const classNames = _.uniq(_.flatMap(items, item => item.prediction.map(p => p.className)));
// create or grab the polymer el
var facetsDiveEl = targetEl.querySelector('facets-dive');
var didCreate = false;
if (!facetsDiveEl) {
const el = document.createElement('div');
el.innerHTML = '<facets-dive width="800" height="600" />';
targetEl.appendChild(el);
facetsDiveEl = targetEl.querySelector('facets-dive');
didCreate = true;
}
// the order of these calls matters
// only set defaults; otherwise let user interactions stick through renders
facetsDiveEl.data = facetsData;
facetsDiveEl.infoRenderer = facetsInfoRenderer.bind(null, items);
if (didCreate) {
facetsDiveEl.hideInfoCard = false;
facetsDiveEl.verticalFacet = textLabelForClassName(classNames[0]);
facetsDiveEl.verticalBuckets = 4;
facetsDiveEl.horizontalFacet = 'searchQuery';
// facetsDiveEl.tweenDuration = 0;
// facetsDiveEl.fadeDuration = 0;
}
// sprite sheet
// see https://github.com/PAIR-code/facets/tree/master/facets_dive#providing-sprites-for-dive-to-render
// 64x64 is the assumption
const {canvas, uri} = await createFacetsAtlas(items, 64, 64);
// console.log('uri', uri);
// document.body.appendChild(canvas);
facetsDiveEl.atlasUrl = uri;
facetsDiveEl.spriteImageWidth = 64;
facetsDiveEl.spriteImageHeight = 64;
}
async function createFacetsAtlas(items, width, height) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const cols = Math.ceil(Math.sqrt(items.length));
canvas.width = cols * width;
canvas.height = cols * width;
await Promise.all(items.map((item, index) => {
const x = width * (index % cols);
const y = height * Math.floor(index / cols);
const img = new Image();
return new Promise(function(resolve, reject) {
img.onload = function() {
context.drawImage(img, x, y, width, height);
resolve();
};
img.crossOrigin = 'Anonymous'; // allow images from search
img.src = item.uri;
});
}));
const uri = canvas.toDataURL();
return {canvas, uri};
}
// see https://github.com/PAIR-code/facets/blob/967e764dd8fbc8327ba9d4e39f3c0d76dce834b9/facets_dive/lib/info-renderers.ts#L26
function facetsInfoRenderer(items, selectedObject, elem) {
// copied
const dl = document.createElement('dl');
// inserted
const item = _.find(items, item => hash64(item.uri) === selectedObject.hashedURI);
if (item) {
const img = document.createElement('img');
img.crossOrigin = 'Anonymous';
img.src = item.uri;
img.style.width = '100%';
dl.appendChild(img);
}
// copied
for (const field in selectedObject) {
// modified
if (['i', 'hashedURI'].indexOf(field) !== -1) {
continue;
}
if (!selectedObject.hasOwnProperty(field)) {
continue;
}
// modified
if (selectedObject[field] === undefined) {
continue;
}
const dt = document.createElement('dt');
dt.textContent = field;
dl.appendChild(dt);
const dd = document.createElement('dd');
dd.textContent = selectedObject[field];
dl.appendChild(dd);
}
elem.appendChild(dl);
};
async function initForModelKey(outEl, modelKey) {
document.querySelector('#model-button').disabled = 'disabled';
document.querySelector('#model-button').innerHTML = 'Loading...';
const {model, maxPredictions} = await loadModel(modelKey);
document.querySelector('#model-button').innerHTML = 'Loaded.';
init(outEl, model, maxPredictions);
}
// search stuff
function readDomainFromEnv() {
return 'https://services-edu.herokuapp.com';
}
function fetchJson(query, apiKey) {
const domain = readDomainFromEnv();
const headers = {'X-Services-Edu-Api-Key': apiKey};
const url = `${domain}/images/search?q=${encodeURIComponent(query)}`;
return fetch(url, {headers})
.then(response => response.json());
}
function renderResults(targetEl, json) {
targetEl.innerHTML = '';
(json.items || []).forEach(item => {
const el = document.createElement('div');
const img = document.createElement('img');
img.classList.add('ImageSearch-image');
img.src = item.image.thumbnailLink;
img.alt = item.title;
img.width = item.image.thumbnailWidth;
img.height = item.image.thumbnailHeight;
el.appendChild(img);
const source = document.createElement('div');
source.classList.add('ImageSearch-image-source');
source.target = '_blank';
source.rel = 'noopener noreferrer';
source.href = item.image.contextLink;
source.text = item.displayLink;
el.appendChild(source);
targetEl.appendChild(el);
});
}
// augmentation
function augmented(items) {
return items;
// var augs = [];
// items.forEach(item => {
// augs.push({
// item,
// augmented: false
// });
// augs.push({
// ...item,
// augmented: true,
// uri: augmentedUri(item.uri)
// });
// });
// return augs;
}
function augmentedUri(uri) {
// todo(kr)
}
// util bits
function download(uri, filename) {
var a = document.createElement("a");
a.href = uri;
a.setAttribute("download", filename);
a.click();
return false;
}
function hash64(str) {
return window.CryptoJS.MD5(str).toString(window.CryptoJS.enc.Base64);
}
async function readInputFilesAsDataURL(files) {
return await Promise.all([].map.call(files, file => {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function(e) {
resolve({uri: e.target.result, filename: file.name});
};
reader.readAsDataURL(file);
});
}));
}
async function readInputFilesAsText(files) {
return await Promise.all([].map.call(files, file => {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function(e) {
resolve(e.target.result);
};
reader.readAsText(file);
});
}));
}
main();