@@ -76,46 +76,46 @@ class ImageClassifier {
76
76
*/
77
77
async loadModel ( modelUrl ) {
78
78
if ( modelUrl ) this . model = await this . loadModelFrom ( modelUrl ) ;
79
- else this . model = await this . modelToUse . load ( { version : this . version , alpha : this . alpha } ) ;
79
+ else this . model = await this . modelToUse . load ( { version : this . version , alpha : this . alpha } ) ;
80
80
81
81
return this ;
82
82
}
83
83
84
84
async loadModelFrom ( path = null ) {
85
85
fetch ( path )
86
- . then ( r => r . json ( ) )
87
- . then ( ( r ) => {
88
- if ( r . ml5Specs ) {
89
- this . mapStringToIndex = r . ml5Specs . mapStringToIndex ;
90
- }
91
- } )
92
- // When loading model generated by Teachable Machine 2.0, the r.ml5Specs is missing,
93
- // which is causing imageClassifier failing to display lables.
94
- // In this case, labels are stored in path/./metadata.json
95
- // Therefore, I'm fetching the metadata and feeding the labels into this.mapStringToIndex
96
- // by Yang Yang, [email protected] , Oct 2, 2019
97
- . then ( ( ) => {
98
- if ( this . mapStringToIndex . length === 0 ) {
99
- const split = path . split ( "/" ) ;
100
- const prefix = split . slice ( 0 , split . length - 1 ) . join ( "/" ) ;
101
- const metadataUrl = `${ prefix } /metadata.json` ;
102
- fetch ( metadataUrl )
103
- . then ( ( res ) => {
104
- if ( ! res . ok ) {
105
- console . log ( "Tried to fetch metadata.json, but it seems to be missing." ) ;
106
- throw Error ( res . statusText ) ;
107
- }
108
- return res ;
109
- } )
110
- . then ( metadataJson => metadataJson . json ( ) )
111
- . then ( ( metadataJson ) => {
112
- if ( metadataJson . labels ) {
113
- this . mapStringToIndex = metadataJson . labels ;
114
- }
115
- } )
116
- . catch ( ( ) => console . log ( "Error when loading metadata.json" ) ) ;
117
- }
118
- } ) ;
86
+ . then ( r => r . json ( ) )
87
+ . then ( ( r ) => {
88
+ if ( r . ml5Specs ) {
89
+ this . mapStringToIndex = r . ml5Specs . mapStringToIndex ;
90
+ }
91
+ } )
92
+ // When loading model generated by Teachable Machine 2.0, the r.ml5Specs is missing,
93
+ // which is causing imageClassifier failing to display lables.
94
+ // In this case, labels are stored in path/./metadata.json
95
+ // Therefore, I'm fetching the metadata and feeding the labels into this.mapStringToIndex
96
+ // by Yang Yang, [email protected] , Oct 2, 2019
97
+ . then ( ( ) => {
98
+ if ( this . mapStringToIndex . length === 0 ) {
99
+ const split = path . split ( "/" ) ;
100
+ const prefix = split . slice ( 0 , split . length - 1 ) . join ( "/" ) ;
101
+ const metadataUrl = `${ prefix } /metadata.json` ;
102
+ fetch ( metadataUrl )
103
+ . then ( ( res ) => {
104
+ if ( ! res . ok ) {
105
+ console . log ( "Tried to fetch metadata.json, but it seems to be missing." ) ;
106
+ throw Error ( res . statusText ) ;
107
+ }
108
+ return res ;
109
+ } )
110
+ . then ( metadataJson => metadataJson . json ( ) )
111
+ . then ( ( metadataJson ) => {
112
+ if ( metadataJson . labels ) {
113
+ this . mapStringToIndex = metadataJson . labels ;
114
+ }
115
+ } )
116
+ . catch ( ( ) => console . log ( "Error when loading metadata.json" ) ) ;
117
+ }
118
+ } ) ;
119
119
// end of the Oct 2, 2019 fix
120
120
this . model = await tf . loadLayersModel ( path ) ;
121
121
return this . model ;
@@ -206,7 +206,7 @@ class ImageClassifier {
206
206
|| inputNumOrCallback . elt instanceof HTMLCanvasElement
207
207
|| inputNumOrCallback . elt instanceof ImageData )
208
208
) {
209
- imgToPredict = inputNumOrCallback . elt ; // Handle p5.js image
209
+ imgToPredict = inputNumOrCallback . elt ; // Handle p5.js image
210
210
} else if ( typeof inputNumOrCallback === 'object' && inputNumOrCallback . canvas instanceof HTMLCanvasElement ) {
211
211
imgToPredict = inputNumOrCallback . canvas ; // Handle p5.js image
212
212
} else if ( ! ( this . video instanceof HTMLVideoElement ) ) {
@@ -242,15 +242,15 @@ class ImageClassifier {
242
242
}
243
243
244
244
const imageClassifier = ( modelName , videoOrOptionsOrCallback , optionsOrCallback , cb ) => {
245
- let model ;
246
245
let video ;
247
246
let options = { } ;
248
247
let callback = cb ;
249
248
250
- if ( typeof modelName === 'string' ) {
251
- model = modelName . toLowerCase ( ) ;
252
- } else {
249
+ let model = modelName ;
250
+ if ( typeof model !== 'string' ) {
253
251
throw new Error ( 'Please specify a model to use. E.g: "MobileNet"' ) ;
252
+ } else if ( model . indexOf ( 'http' ) === - 1 ) {
253
+ model = modelName . toLowerCase ( ) ;
254
254
}
255
255
256
256
if ( videoOrOptionsOrCallback instanceof HTMLVideoElement ) {
0 commit comments