@@ -41,7 +41,7 @@ class Mobilenet {
41
41
this . mapStringToIndex = [ ] ;
42
42
this . usageType = null ;
43
43
this . ready = callCallback ( this . loadModel ( ) , callback ) ;
44
- this . then = this . ready . then ;
44
+ // this.then = this.ready.then;
45
45
}
46
46
47
47
async loadModel ( ) {
@@ -56,12 +56,18 @@ class Mobilenet {
56
56
57
57
classification ( video , callback ) {
58
58
this . usageType = 'classifier' ;
59
- return callCallback ( this . loadVideo ( video ) , callback ) ;
59
+ if ( video ) {
60
+ callCallback ( this . loadVideo ( video ) , callback ) ;
61
+ }
62
+ return this ;
60
63
}
61
64
62
65
regression ( video , callback ) {
63
66
this . usageType = 'regressor' ;
64
- return callCallback ( this . loadVideo ( video ) , callback ) ;
67
+ if ( video ) {
68
+ callCallback ( this . loadVideo ( video ) , callback ) ;
69
+ }
70
+ return this ;
65
71
}
66
72
67
73
async loadVideo ( video ) {
@@ -70,7 +76,7 @@ class Mobilenet {
70
76
if ( video instanceof HTMLVideoElement ) {
71
77
inputVideo = video ;
72
78
} else if ( typeof video === 'object' && video . elt instanceof HTMLVideoElement ) {
73
- inputVideo = video . elt ;
79
+ inputVideo = video . elt ; // p5.js video element
74
80
}
75
81
76
82
if ( inputVideo ) {
@@ -231,10 +237,10 @@ class Mobilenet {
231
237
}
232
238
233
239
async classifyInternal ( imgToPredict ) {
234
- if ( this . usageType = == 'classifier' ) {
240
+ if ( this . usageType ! == 'classifier' ) {
235
241
throw new Error ( 'Mobilenet Feature Extraction has not been set to be a classifier.' ) ;
236
242
}
237
-
243
+ await tf . nextFrame ( ) ;
238
244
this . isPredicting = true ;
239
245
const predictedClass = tf . tidy ( ( ) => {
240
246
const processedImg = imgToTensor ( imgToPredict ) ;
@@ -243,7 +249,6 @@ class Mobilenet {
243
249
return predictions . as1D ( ) . argMax ( ) ;
244
250
} ) ;
245
251
let classId = ( await predictedClass . data ( ) ) [ 0 ] ;
246
- await tf . nextFrame ( ) ;
247
252
if ( this . mapStringToIndex . length > 0 ) {
248
253
classId = this . mapStringToIndex [ classId ] ;
249
254
}
@@ -273,7 +278,7 @@ class Mobilenet {
273
278
if ( this . usageType !== 'regressor' ) {
274
279
throw new Error ( 'Mobilenet Feature Extraction has not been set to be a regressor.' ) ;
275
280
}
276
-
281
+ await tf . nextFrame ( ) ;
277
282
this . isPredicting = true ;
278
283
const predictedClass = tf . tidy ( ( ) => {
279
284
const processedImg = imgToTensor ( imgToPredict ) ;
@@ -283,7 +288,6 @@ class Mobilenet {
283
288
} ) ;
284
289
const prediction = await predictedClass . data ( ) ;
285
290
predictedClass . dispose ( ) ;
286
- await tf . nextFrame ( ) ;
287
291
return prediction [ 0 ] ;
288
292
}
289
293
0 commit comments