forked from CodeLabClub/scratch3_knn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
915 lines (863 loc) · 29.1 KB
/
index.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
require('babel-polyfill');
const Runtime = require('../../engine/runtime');
const ArgumentType = require('../../extension-support/argument-type');
const BlockType = require('../../extension-support/block-type');
const Clone = require('../../util/clone');
const Cast = require('../../util/cast');
const Video = require('../../io/video');
const formatMessage = require('format-message');
import * as tf from '@tensorflow/tfjs';
import * as mobilenetModule from './mobilenet.js';
import * as knnClassifier from '@tensorflow-models/knn-classifier';
/**
* Sensor attribute video sensor block should report.
* @readonly
* @enum {string}
*/
const SensingAttribute = {
/** The amount of motion. */
MOTION: 'motion',
/** The direction of the motion. */
DIRECTION: 'direction'
};
/**
* Subject video sensor block should report for.
* @readonly
* @enum {string}
*/
const SensingSubject = {
/** The sensor traits of the whole stage. */
STAGE: 'Stage',
/** The senosr traits of the area overlapped by this sprite. */
SPRITE: 'this sprite'
};
/**
* States the video sensing activity can be set to.
* @readonly
* @enum {string}
*/
const VideoState = {
/** Video turned off. */
OFF: 'off',
/** Video turned on with default y axis mirroring. */
ON: 'on',
/** Video turned on without default y axis mirroring. */
ON_FLIPPED: 'on-flipped'
};
let typeArr = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10'
]
/**
* Class for the motion-related blocks in Scratch 3.0
* @param {Runtime} runtime - the runtime instantiating this block package.
* @constructor
*/
class Scratch3Knn {
constructor(runtime) {
this.knn = null
this.trainTypes = typeArr.map(item => {
return 'label' + item
})
this.knnInit()
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
/**
* The last millisecond epoch timestamp that the video stream was
* analyzed.
* @type {number}
*/
this._lastUpdate = null;
this.KNN_INTERVAL = 1000
if (this.runtime.ioDevices) {
// Clear target motion state values when the project starts.
this.runtime.on(Runtime.PROJECT_RUN_START, this.reset.bind(this));
// Kick off looping the analysis logic.
// this._loop();
// Configure the video device with values from a globally stored
// location.
this.setVideoTransparency({
TRANSPARENCY: 10
});
this.videoToggle({
VIDEO_STATE: this.globalVideoState
});
}
setInterval(async () => {
if (this.globalVideoState === VideoState.ON) {
await this.gotResult()
console.log('knn result:', this.trainResult)
}
}, this.KNN_INTERVAL)
}
/**
* After analyzing a frame the amount of milliseconds until another frame
* is analyzed.
* @type {number}
*/
static get INTERVAL() {
return 33;
}
/**
* Dimensions the video stream is analyzed at after its rendered to the
* sample canvas.
* @type {Array.<number>}
*/
static get DIMENSIONS() {
return [480, 360];
}
/**
* The key to load & store a target's motion-related state.
* @type {string}
*/
static get STATE_KEY() {
return 'Scratch.videoSensing';
}
/**
* The default motion-related state, to be used when a target has no existing motion state.
* @type {MotionState}
*/
static get DEFAULT_MOTION_STATE() {
return {
motionFrameNumber: 0,
motionAmount: 0,
motionDirection: 0
};
}
/**
* The transparency setting of the video preview stored in a value
* accessible by any object connected to the virtual machine.
* @type {number}
*/
get globalVideoTransparency() {
const stage = this.runtime.getTargetForStage();
if (stage) {
return stage.videoTransparency;
}
return 10;
}
set globalVideoTransparency(transparency) {
const stage = this.runtime.getTargetForStage();
if (stage) {
stage.videoTransparency = transparency;
}
return transparency;
}
/**
* The video state of the video preview stored in a value accessible by any
* object connected to the virtual machine.
* @type {number}
*/
get globalVideoState() {
const stage = this.runtime.getTargetForStage();
if (stage) {
return stage.videoState;
}
return VideoState.ON;
}
set globalVideoState(state) {
const stage = this.runtime.getTargetForStage();
if (stage) {
stage.videoState = state;
}
return state;
}
/**
* Reset the extension's data motion detection data. This will clear out
* for example old frames, so the first analyzed frame will not be compared
* against a frame from before reset was called.
*/
reset() {
const targets = this.runtime.targets;
for (let i = 0; i < targets.length; i++) {
const state = targets[i].getCustomState(Scratch3Knn .STATE_KEY);
if (state) {
state.motionAmount = 0;
state.motionDirection = 0;
}
}
}
/**
* Occasionally step a loop to sample the video, stamp it to the preview
* skin, and add a TypedArray copy of the canvas's pixel data.
* @private
*/
_loop() {
setTimeout(this._loop.bind(this), Math.max(this.runtime.currentStepTime, Scratch3Knn .INTERVAL));
// Add frame to detector
const time = Date.now();
if (this._lastUpdate === null) {
this._lastUpdate = time;
}
const offset = time - this._lastUpdate;
if (offset > Scratch3Knn .INTERVAL) {
const frame = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_IMAGE_DATA,
dimensions: Scratch3Knn .DIMENSIONS
});
if (frame) {
this._lastUpdate = time;
}
}
}
/**
* Create data for a menu in scratch-blocks format, consisting of an array
* of objects with text and value properties. The text is a translated
* string, and the value is one-indexed.
* @param {object[]} info - An array of info objects each having a name
* property.
* @return {array} - An array of objects with text and value properties.
* @private
*/
_buildMenu(info) {
return info.map((entry, index) => {
const obj = {};
obj.text = entry.name;
obj.value = entry.value || String(index + 1);
return obj;
});
}
/**
* @param {Target} target - collect motion state for this target.
* @returns {MotionState} the mutable motion state associated with that
* target. This will be created if necessary.
* @private
*/
_getMotionState(target) {
let motionState = target.getCustomState(Scratch3Knn .STATE_KEY);
if (!motionState) {
motionState = Clone.simple(Scratch3Knn .DEFAULT_MOTION_STATE);
target.setCustomState(Scratch3Knn .STATE_KEY, motionState);
}
return motionState;
}
static get SensingAttribute() {
return SensingAttribute;
}
/**
* An array of choices of whether a reporter should return the frame's
* motion amount or direction.
* @type {object[]} an array of objects
* @param {string} name - the translatable name to display in sensor
* attribute menu
* @param {string} value - the serializable value of the attribute
*/
get ATTRIBUTE_INFO() {
return [
{
name: 'motion',
value: SensingAttribute.MOTION
},
{
name: 'direction',
value: SensingAttribute.DIRECTION
}
];
}
static get SensingSubject() {
return SensingSubject;
}
/**
* An array of info about the subject choices.
* @type {object[]} an array of objects
* @param {string} name - the translatable name to display in the subject menu
* @param {string} value - the serializable value of the subject
*/
get SUBJECT_INFO() {
return [
{
name: 'stage',
value: SensingSubject.STAGE
},
{
name: 'sprite',
value: SensingSubject.SPRITE
}
];
}
/**
* States the video sensing activity can be set to.
* @readonly
* @enum {string}
*/
static get VideoState() {
return VideoState;
}
/**
* An array of info on video state options for the "turn video [STATE]" block.
* @type {object[]} an array of objects
* @param {string} name - the translatable name to display in the video state menu
* @param {string} value - the serializable value stored in the block
*/
get VIDEO_STATE_INFO () {
return [
{
name: formatMessage({
id: 'videoSensing.off',
default: 'off',
description: 'Option for the "turn video [STATE]" block'
}),
value: VideoState.OFF
},
{
name: formatMessage({
id: 'videoSensing.on',
default: 'on',
description: 'Option for the "turn video [STATE]" block'
}),
value: VideoState.ON
},
{
name: formatMessage({
id: 'videoSensing.onFlipped',
default: 'on flipped',
description: 'Option for the "turn video [STATE]" block that causes the video to be flipped' +
' horizontally (reversed as in a mirror)'
}),
value: VideoState.ON_FLIPPED
}
];
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo() {
return {
id: 'cxknn',
name: 'KNN Classifier',
blocks: [
{
opcode: 'videoToggle',
text: formatMessage({
id: 'videoSensing.videoToggle',
default: 'turn video [VIDEO_STATE]',
description: 'Controls display of the video preview layer'
}),
arguments: {
VIDEO_STATE: {
type: ArgumentType.NUMBER,
menu: 'VIDEO_STATE',
defaultValue: VideoState.ON
}
}
},
{
opcode: 'setVideoTransparency',
text: formatMessage({
id: 'videoSensing.setVideoTransparency',
default: 'set video transparency to [TRANSPARENCY]',
description: 'Controls transparency of the video preview layer'
}),
arguments: {
TRANSPARENCY: {
type: ArgumentType.NUMBER,
defaultValue: 10
}
}
},
{
opcode: 'isloaded',
blockType: BlockType.BOOLEAN,
text: formatMessage({
id: 'knn.isloaded',
default: 'is loaded',
description: 'knn is loaded'
})
},
{
opcode: 'trainA',
blockType: BlockType.COMMAND,
text: formatMessage({
id: 'knn.trainA',
default: 'Train 1 [STRING]',
description: 'Train A'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'trainB',
blockType: BlockType.COMMAND,
text: formatMessage({
id: 'knn.trainB',
default: 'Train 2 [STRING]',
description: 'Train B'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label2"
}
}
},
{
opcode: 'trainC',
blockType: BlockType.COMMAND,
text: formatMessage({
id: 'knn.trainC',
default: 'Train 3 [STRING]',
description: 'Train C'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label3"
}
}
},
{
opcode: 'train',
blockType: BlockType.COMMAND,
text: formatMessage({
id: 'knn.train',
default: 'Train label [type] [STRING]',
description: 'Train'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label4"
},
type: {
type: ArgumentType.STRING,
menu: 'typemenu',
defaultValue: "4"
}
}
},
{
opcode: 'addTrainType',
blockType: BlockType.COMMAND,
text: formatMessage({
id: 'knn.addTrainType',
default: 'add train type',
description: 'add train type'
})
},
{
opcode: 'resetTrain',
blockType: BlockType.COMMAND,
text: formatMessage({
id: 'knn.reset',
default: 'Reset [STRING]',
description: 'reset'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'Sample1',
blockType: BlockType.REPORTER,
text: formatMessage({
id: 'knn.sample',
default: 'Sample',
description: 'samples'
}) + '1',
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'Sample2',
blockType: BlockType.REPORTER,
text: formatMessage({
id: 'knn.sample',
default: 'Sample',
description: 'samples'
}) + '2',
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'Sample3',
blockType: BlockType.REPORTER,
text: formatMessage({
id: 'knn.sample',
default: 'Sample',
description: 'samples'
}) + '3',
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'Samples',
blockType: BlockType.REPORTER,
text: formatMessage({
id: 'knn.samples',
default: 'Samples [STRING]',
description: 'samples'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'getResult',
blockType: BlockType.REPORTER,
text: formatMessage({
id: 'knn.getResult',
default: 'Result',
description: 'getResult'
}),
arguments: {
}
},
{
opcode: 'getConfidence',
blockType: BlockType.REPORTER,
text: formatMessage({
id: 'knn.getConfidence',
default: 'getConfidence [STRING]',
description: 'getConfidence'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
},
{
opcode: 'whenGetResult',
blockType: BlockType.HAT,
text: formatMessage({
id: 'knn.whenGetResult',
default: 'when get [STRING]',
description: 'whenGetResult'
}),
arguments: {
STRING: {
type: ArgumentType.STRING,
defaultValue: "label1"
}
}
}
],
menus: {
ATTRIBUTE: {
acceptReporters: true,
items: this._buildMenu(this.ATTRIBUTE_INFO)
},
SUBJECT: {
acceptReporters: true,
items: this._buildMenu(this.SUBJECT_INFO)
},
VIDEO_STATE: {
acceptReporters: true,
items:this._buildMenu(this.VIDEO_STATE_INFO),
},
typemenu: {
acceptReporters: true,
items: '_typeArr'
}
}
};
}
_typeArr () {
return typeArr.slice(3).map(item => item.toString())
}
/**
* A scratch command block handle that configures the video state from
* passed arguments.
* @param {object} args - the block arguments
* @param {VideoState} args.VIDEO_STATE - the video state to set the device to
*/
videoToggle(args) {
const state = args.VIDEO_STATE;
this.globalVideoState = state;
if (state === VideoState.OFF) {
this.runtime.ioDevices.video.disableVideo();
} else {
this.runtime.ioDevices.video.enableVideo();
// Mirror if state is ON. Do not mirror if state is ON_FLIPPED.
this.runtime.ioDevices.video.mirror = state === VideoState.ON;
}
}
/**
* A scratch command block handle that configures the video preview's
* transparency from passed arguments.
* @param {object} args - the block arguments
* @param {number} args.TRANSPARENCY - the transparency to set the video
* preview to
*/
setVideoTransparency(args) {
const transparency = Cast.toNumber(args.TRANSPARENCY);
this.globalVideoTransparency = transparency;
this.runtime.ioDevices.video.setPreviewGhost(transparency);
}
clearClass(classIndex) {
this.classifier.clearClass(classIndex);
}
updateExampleCounts(args, util) {
let counts = this.classifier.getClassExampleCount();
this.runtime.emit('SAY', util.target, 'say', this.trainTypes.map((item, index) => {
return item + '样本数:' + (counts[index] || 0) + '\n'
}).join('\n'));
}
isloaded() {
return Boolean(this.mobilenet)
}
train(args, util) {
if (this.globalVideoState === VideoState.OFF) {
console.log('请先打开摄像头')
return
}
let index = typeArr.findIndex(item => item === args.type)
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, index);
this.trainTypes[index] = args.STRING
}
}
addTrainType() {
typeArr.push((typeArr.length + 1).toString())
this.trainTypes.push('label' + (this.trainTypes.length + 1).toString())
}
trainA(args, util) {
if (this.globalVideoState === VideoState.OFF) {
alert('请先打开摄像头')
return
}
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, 0);
this.trainTypes[0] = args.STRING
}
}
trainB(args, util) {
if (this.globalVideoState === VideoState.OFF) {
alert('请先打开摄像头')
return
}
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, 1);
this.trainTypes[1] = args.STRING
}
}
trainC(args, util) {
if (this.globalVideoState === VideoState.OFF) {
alert('请先打开摄像头')
return
}
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, 2);
this.trainTypes[2] = args.STRING
}
}
trainD(args, util) {
if (this.globalVideoState === VideoState.OFF) {
alert('请先打开摄像头')
return
}
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, 3);
this.trainTypes[3] = args.STRING
this.updateExampleCounts(args, util);
}
}
trainE(args, util) {
if (this.globalVideoState === VideoState.OFF) {
alert('请先打开摄像头')
return
}
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, 4);
this.trainTypes[4] = args.STRING
this.updateExampleCounts(args, util);
}
}
trainF(args, util) {
if (this.globalVideoState === VideoState.OFF) {
alert('请先打开摄像头')
return
}
let img = document.createElement('img')
img.src = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
}).toDataURL("image/png")
img.width = 480
img.height = 360
img.onload = () => {
const img0 = tf.fromPixels(img);
const logits0 = this.mobilenet.infer(img0, 'conv_preds');
this.classifier.addExample(logits0, 5);
this.trainTypes[5] = args.STRING
this.updateExampleCounts(args, util);
}
}
Samples(args, util) {
let counts = this.classifier.getClassExampleCount();
let index = this.trainTypes.indexOf(args.STRING)
return counts[index] || 0
}
Sample1(args, util) {
let counts = this.classifier.getClassExampleCount();
let index = 0
return counts[index] || 0
}
Sample2(args, util) {
let counts = this.classifier.getClassExampleCount();
let index = 1
return counts[index] || 0
}
Sample3(args, util) {
let counts = this.classifier.getClassExampleCount();
let index = 2
return counts[index] || 0
}
resetTrain(args, util) {
let counts = this.classifier.getClassExampleCount();
let index = this.trainTypes.indexOf(args.STRING)
if (!counts[index]) {
alert('该类别无训练数据')
return
}
if (index < 0) {
alert('未找到对应类别')
return
}
this.clearClass(index);
// this.updateExampleCounts(args, util);
}
getResult(args, util) {
return this.trainResult
}
getConfidence(args, util) {
let index = this.trainTypes.indexOf(args.STRING)
if (index === -1) {
return 0
}
return (this.trainConfidences && this.trainConfidences[index]) || 0
}
gotResult(args, util) {
return new Promise((resolve, reject) => {
let img = document.createElement('img')
let frame = this.runtime.ioDevices.video.getFrame({
format: Video.FORMAT_CANVAS,
dimensions: Scratch3Knn.DIMENSIONS
})
if (!Object.keys(this.classifier.getClassExampleCount()).length) {
resolve()
return
}
if (frame) {
img.src = frame.toDataURL("image/png")
} else {
resolve()
return
}
img.width = 480
img.height = 360
img.onload = async () => {
const x = tf.fromPixels(img);
const xlogits = this.mobilenet.infer(x, 'conv_preds');
console.log('Predictions:');
let res = await this.classifier.predictClass(xlogits);
console.log(this.classifier.getClassExampleCount(), res)
this.trainResult = this.trainTypes[res.classIndex] || 0
this.trainConfidences = res.confidences
resolve(this.trainResult)
}
})
}
whenGetResult(args, util) {
if (this.trainResult === undefined) {
return false
}
setTimeout(() => {
this.trainResult = undefined
}, 100)
return args.STRING === this.trainResult
}
async knnInit () {
this.classifier = knnClassifier.create();
this.mobilenet = await mobilenetModule.load();
}
}
module.exports = Scratch3Knn;