Skip to content

Commit e6f5277

Browse files
shiffmanjoeyklee
authored andcommitted
model URLs should not be turned into lower case (#650)
* only lowercase if not a URL * same fix for sound classifier
1 parent 8c023d9 commit e6f5277

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/ImageClassifier/index.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,46 @@ class ImageClassifier {
7676
*/
7777
async loadModel(modelUrl) {
7878
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 });
8080

8181
return this;
8282
}
8383

8484
async loadModelFrom(path = null) {
8585
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+
});
119119
// end of the Oct 2, 2019 fix
120120
this.model = await tf.loadLayersModel(path);
121121
return this.model;
@@ -206,7 +206,7 @@ class ImageClassifier {
206206
|| inputNumOrCallback.elt instanceof HTMLCanvasElement
207207
|| inputNumOrCallback.elt instanceof ImageData)
208208
) {
209-
imgToPredict = inputNumOrCallback.elt; // Handle p5.js image
209+
imgToPredict = inputNumOrCallback.elt; // Handle p5.js image
210210
} else if (typeof inputNumOrCallback === 'object' && inputNumOrCallback.canvas instanceof HTMLCanvasElement) {
211211
imgToPredict = inputNumOrCallback.canvas; // Handle p5.js image
212212
} else if (!(this.video instanceof HTMLVideoElement)) {
@@ -242,15 +242,15 @@ class ImageClassifier {
242242
}
243243

244244
const imageClassifier = (modelName, videoOrOptionsOrCallback, optionsOrCallback, cb) => {
245-
let model;
246245
let video;
247246
let options = {};
248247
let callback = cb;
249248

250-
if (typeof modelName === 'string') {
251-
model = modelName.toLowerCase();
252-
} else {
249+
let model = modelName;
250+
if (typeof model !== 'string') {
253251
throw new Error('Please specify a model to use. E.g: "MobileNet"');
252+
} else if (model.indexOf('http') === -1) {
253+
model = modelName.toLowerCase();
254254
}
255255

256256
if (videoOrOptionsOrCallback instanceof HTMLVideoElement) {

src/SoundClassifier/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SoundClassifier {
5959

6060
/**
6161
* Classifies the audio from microphone and takes a callback to handle the results
62-
* @param {function | number} numOrCallback -
62+
* @param {function | number} numOrCallback -
6363
* takes any of the following params
6464
* @param {function} cb - a callback function that handles the results of the function.
6565
* @return {function} a promise or the results of a given callback, cb.
@@ -82,14 +82,14 @@ class SoundClassifier {
8282
}
8383

8484
const soundClassifier = (modelName, optionsOrCallback, cb) => {
85-
let model;
8685
let options = {};
8786
let callback = cb;
8887

89-
if (typeof modelName === 'string') {
90-
model = modelName.toLowerCase();
91-
} else {
88+
let model = modelName;
89+
if (typeof model !== 'string') {
9290
throw new Error('Please specify a model to use. E.g: "SpeechCommands18w"');
91+
} else if (model.indexOf('http') === -1) {
92+
model = modelName.toLowerCase();
9393
}
9494

9595
if (typeof optionsOrCallback === 'object') {

0 commit comments

Comments
 (0)