-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfHoloPoc.js
135 lines (104 loc) · 3.9 KB
/
tfHoloPoc.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
let net;
let webcamElement;
const IMAGE_SIZE = 224;
const INPUT_MIN = 0;
const INPUT_MAX = 1;
const NORMALIZATION_CONSTANT = (INPUT_MAX - INPUT_MIN) / 255.0;
async function createVideoElement() {
webcamElement = document.createElement('video');
webcamElement.setAttribute("autoplay", "");
webcamElement.setAttribute("playsinline", "");
webcamElement.setAttribute("width", "224");
webcamElement.setAttribute("height", "224");
}
async function setupWebcam() {
return new Promise((resolve, reject) => {
const navigatorAny = navigator;
navigator.getUserMedia = navigator.getUserMedia ||
navigatorAny.webkitGetUserMedia || navigatorAny.mozGetUserMedia ||
navigatorAny.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true },
stream => {
webcamElement.srcObject = stream;
webcamElement.addEventListener('loadeddata', () => resolve(), false);
},
error => reject());
} else {
reject();
}
});
}
function preprocess(img) {
// Adapted from https://github.com/tensorflow/tfjs-models/blob/master/mobilenet/src/index.ts
return tf.tidy(() => {
img = tf.browser.fromPixels(img);
// Normalize the image from [0, 255] to [inputMin, inputMax].
const normalized = tf.add(
tf.mul(tf.cast(img, 'float32'), NORMALIZATION_CONSTANT),
INPUT_MIN);
// Resize the image to
let resized = normalized;
if (img.shape[0] !== IMAGE_SIZE || img.shape[1] !== IMAGE_SIZE) {
const alignCorners = true;
resized = tf.image.resizeBilinear(
normalized, [IMAGE_SIZE, IMAGE_SIZE], alignCorners);
}
// Reshape so we can pass it to predict.
const result = tf.reshape(resized, [-1, IMAGE_SIZE, IMAGE_SIZE, 3]);
return result;
});
}
async function run() {
let i = 0;
let blockSize = 100;
let times = new Array(blockSize);
let avgDuration = -1;
while (true) {
if (i == blockSize) {
avgDuration = calcAvgArray(times);
i = 0;
}
const img = preprocess(webcamElement);
const start = performance.now();
const result = await net.predict(img);
const end = performance.now();
const duration = end - start;
const predClassA = await result.argMax(1).data();
const predClass = predClassA[0];
const probA = await result.array();
const prob = probA[0][predClass];
result.dispose();
img.dispose();
let outputText = labels[predClass] + "\n(" + Math.round(prob * 100) + "% | " + Math.round(duration) + "ms | Avg: " + Math.round(avgDuration) + "ms)";
document.getElementById("output").setAttribute("text", "value", outputText);
times[i] = duration;
i++;
await tf.nextFrame();
}
}
async function setup() {
console.log('Loading mobilenet..');
// Load the model.
//net = await mobilenet.load();
net = await tf.loadLayersModel(window.location.href + 'MobileNetV2/model.json');
console.log('Successfully loaded model');
console.log("Used tf.js backend: " + tf.getBackend());
await createVideoElement();
await setupWebcam();
document.getElementById("output").setAttribute("text", "value", "Ready!\nPlease click on the AR button in\nthe bottom right corner to start!");
// Add listener to AR start button
document.querySelector('a-scene').addEventListener('enter-vr', function () {
// Set text color white
document.getElementById("output").setAttribute("text", "color", "#ffffff");
run();
});
}
function calcAvgArray(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
setup();