-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageml.js
365 lines (339 loc) · 10.8 KB
/
imageml.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
let Camera = (function () {
const webCam = 0;
const wsCam = 1;
const jpgCam = 2;
class Camera {
// camType: 0,1,2 or http://192.168.0.11/jpg or ws://192.168.43.110:8889/rws/ws
constructor(camType) {
this.setCamType(camType);
this.setRotateCam(0)
}
setRotateCam(degree) {
this.rotateCam = degree;
}
getRotateCam() {
return this.rotateCam;
}
setCamType(camType) {
this.cameraList = [];
if (isNaN(parseInt(camType))) {
this.URL = camType;
if (camType.indexOf("ws://") == 0) {
this.camType = wsCam;
} else if (camType.indexOf("http://") == 0) {
this.camType = jpgCam;
}
} else {
this.camType = parseInt(camType);
}
}
enumerateDevices() {
var self = this;
return new Promise(function (resolve, reject) {
navigator.mediaDevices.enumerateDevices()
.then(function (o) {
self.gotDevices(self, o);
resolve();
}).catch(self.handleError);
});
}
gotDevices(self, deviceInfos) {
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
if (deviceInfo.kind === 'videoinput') {
self.cameraList.push(deviceInfo);
}
}
}
async startCam() {
switch (this.camType) {
case webCam:
await this.enumerateDevices();
if (window.stream) {
window.stream.getTracks().forEach(function (track) {
track.stop();
});
}
var deviceId = 0;
try {
deviceId = this.cameraList[this.camType].deviceId;
} catch (e) {
console.log("can't found camType:", this.camType, "error:", e);
console.log(this.cameraList);
}
var constraints = {
video: {
deviceId: { exact: deviceId }
}
};
var self = this;
navigator.mediaDevices.getUserMedia(constraints).
then(function (stream) {
if (self.video) {
self.video.srcObject = stream;
}
}).catch(function (error) {
console.log('Error: ', error);
});
break;
/* WebRTC */
case wsCam:
console.log("WebRTC:", this.camType);
ConnectWebSocket(this.URL);
break;
case jpgCam:
// http://192.168.43.201:9966/ok.png
console.log("JPGCam:", this.camType, " ,URL:", this.URL);
//this.setRotateCam(90);
break;
}
}
getEle(eleOrId) {
return typeof eleOrId === 'object' ?
eleOrId : document.getElementById(eleOrId);
}
onImage(imageId_or_ele, callback) {
var self = this;
var image = this.getEle(imageId_or_ele);
image.setAttribute("crossOrigin", 'Anonymous');
var camSnapshotDelay = 0.5;
var param = this.URL.indexOf("?");
if (param > 0) {
camSnapshotDelay = parseFloat(this.URL.substring(param + 1)) * 1000;
this.URL = this.URL.substring(0, param);
}
image.src = this.URL;
image.onload = function () {
setTimeout(function () {
if (typeof callback == 'function') {
callback(image);
}
image.src = self.URL + "?" + Math.random();
}, camSnapshotDelay);
}
}
onCanvas(eleOrId, callback) {
var self = this;
var canvas = self.getEle(eleOrId);
buttonTrigger(canvas, function () {
self.startCam();
switch (self.camType) {
case webCam:
case wsCam:
var video = self.createVideo();
window.remoteVideo = self.video = video;
video.onloadeddata = function () {
var loop = function () {
var ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight,
0, 0, canvas.width, canvas.height);
if (typeof callback == 'function') {
callback(canvas);
}
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
}
break;
case jpgCam:
self.onImage(document.createElement('img'), function (img) {
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
if (self.getRotateCam() > 0) {
self.drawRotated(canvas, img, 90);
}
if (typeof callback == 'function') {
callback(canvas);
}
});
break;
}
});
}
toVideo(eleOrId) {
var self = this;
window.remoteVideo = self.video = this.getEle(eleOrId);
buttonTrigger(self.video, function () {
self.startCam();
});
}
createVideo() {
var video = document.createElement('video');
video.autoplay = true;
return video;
}
drawRotated(canvas, image, degrees) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(degrees * Math.PI / 180);
context.drawImage(image, -image.width / 2, -image.width / 2);
context.restore();
}
}
function buttonTrigger(ele, callback) {
if (navigator.userAgent.indexOf("Chrome") < 0) {
var btn = document.createElement("BUTTON");
btn.setAttribute("style", "background-color: #e0f0e0;position: absolute;z-index:2;font-size:32px");
document.getElementsByTagName("body")[0].append(btn);
var rect = ele.getBoundingClientRect();
btn.style.top = rect.top;
btn.style.left = rect.left;
btn.style.width = rect.width;
btn.style.height = rect.height;
btn.innerHTML = "Start Camera";
btn.addEventListener('click', function (e) {
btn.parentNode.removeChild(btn);
callback();
});
} else {
callback();
}
}
return Camera;
})();
+
(function (factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function (scope) {
'use strict';
// let self = this;
let proto;
let Module = scope.Module;
const HOST_URL = "https://imageml2.webduino.io";
let mobilenet;
let secondmodel;
let vid = 0;
let status;
let labels = [];
let currentClass = -1;
let currentConfidence = 0;
function loadJS(filePath) {
var req = new XMLHttpRequest();
req.open("GET", filePath, false); // 'false': synchronous.
req.send(null);
var headElement = document.getElementsByTagName("head")[0];
var newScriptElement = document.createElement("script");
newScriptElement.type = "text/javascript";
newScriptElement.text = req.responseText;
headElement.appendChild(newScriptElement);
}
async function getMobileNet() {
try {
return await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json');
} catch (e) {
console.warn(`Load ${HOST_URL} MobileNet...`);
return await tf.loadModel(HOST_URL + '/mobilenet/v1_0.25_224/model.json');
}
}
async function start(modelName, camSource, userId, _rotate) {
console.log("tfjs 0.13.4");
var rotate = _rotate;
//camSource = "http://192.168.0.168/jpg?0.5";
// Module.call(this);
loadJS('https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]');
// load models
try {
const _mobilenet = await getMobileNet()
const layer = _mobilenet.getLayer('conv_pw_13_relu');
mobilenet = tf.model({ inputs: _mobilenet.inputs, outputs: layer.output });
if (modelName.indexOf('https://') === 0) {
// modelName is full url
secondmodel = await tf.loadModel(modelName);
} else {
// modelName is just name, combine full url by userId
secondmodel = await tf.loadModel(HOST_URL + '/ml_models/' + ('00000000' + userId).slice(-8) + modelName + '/model.json');
}
} catch (e) {
alert('Load model error!');
}
if (camSource != '本機') {
var c1 = document.createElement('canvas');
c1.width = 224;
c1.height = 224;
document.body.appendChild(c1);
var cam = new Camera(camSource);
cam.setRotateCam(rotate ? 90 : 0);
cam.onCanvas(c1, function (c) {
vid = c.getContext('2d').getImageData(0, 0, 224, 224);
});
} else {
vid = document.createElement('video');
vid.width = 224;
vid.height = 224;
vid.autoplay = true;
document.body.appendChild(vid);
// start webcam
try {
navigator.mediaDevices.getUserMedia({
video: {
width: 224,
height: 224,
facingMode: "environment"
}
})
.then(stream => {
vid.srcObject = stream;
vid.play();
});
} catch (e) {
alert('WebCam is not available!');
}
}
// create status message
status = document.createElement('div');
status.id = 'status';
document.body.appendChild(status);
await proto.startDetect();
}
function imageml(modelName, camSource, userId, _rotate) {
setTimeout(async () => {
await start(modelName, camSource, userId, _rotate);
}, 1);
}
imageml.prototype = proto =
Object.create(Module.prototype, {
constructor: {
value: imageml
}
});
proto.onLabel = function (idx, callback) {
labels[idx] = callback;
}
proto.getClass = function () {
return currentClass;
}
proto.getConfidence = function () {
return parseInt(currentConfidence * 1000000) / 10000.0;
}
proto.startDetect = async function () {
if (vid != 0) {
const resultTensor = tf.tidy(() => {
const webcamImage = tf.fromPixels(vid);
const batchedImage = webcamImage.expandDims(0);
const img = batchedImage.toFloat().div(tf.scalar(127)).sub(tf.scalar(1));
const activation = mobilenet.predict(img).flatten().expandDims(0);
const predictions = secondmodel.predict(activation);
return predictions.as1D();
});
let classTensor = resultTensor.argMax();
let confidenceTensor = resultTensor.max();
currentClass = (await classTensor.data())[0];
currentConfidence = (await confidenceTensor.data())[0];
classTensor.dispose();
confidenceTensor.dispose();
resultTensor.dispose();
status.innerHTML = "辨識類別編號為:" + currentClass + ",信心水準:" + parseInt(currentConfidence * 1000000) / 10000.0 + " %";
if (typeof labels[currentClass] === "function") {
labels[currentClass](currentClass);
}
}
setTimeout(async () => { await proto.startDetect() }, 100);
}
scope.module.imageml = imageml;
}));