-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.js
63 lines (43 loc) · 1.5 KB
/
detect.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
const tf = require('@tensorflow/tfjs-node');
const cocossd = require('@tensorflow-models/coco-ssd');
const mobilenet = require('@tensorflow-models/mobilenet');
const toUint8Array = require('base64-to-uint8array');
export default class ObjectDetectors {
constructor(image, type) {
this.inputImage = image;
this.type = type;
}
async loadCocoSsdModal() {
const modal = await cocossd.load({
base: 'mobilenet_v2'
})
return modal;
}
async loadMobileNetModal() {
const modal = await mobilenet.load({
version: 1,
alpha: 0.25 | .50 | .75 | 1.0,
})
return modal;
}
getTensor3dObject(numOfChannels) {
const imageData = this.inputImage.replace('data:image/jpeg;base64','')
.replace('data:image/png;base64','');
const imageArray = toUint8Array(imageData);
const tensor3d = tf.node.decodeJpeg( imageArray, numOfChannels );
return tensor3d;
}
async process() {
let predictions = null;
const tensor3D = this.getTensor3dObject(3);
if(this.type === "imagenet") {
const model = await this.loadMobileNetModal();
predictions = await model.classify(tensor3D);
} else {
const model = await this.loadCocoSsdModal();
predictions = await model.detect(tensor3D);
}
tensor3D.dispose();
return {data: predictions, type: this.type};
}
}