Skip to content

Commit edf969d

Browse files
committed
Add tests to imageclassifier
fix video issue in imageclassifier
1 parent 511c70a commit edf969d

File tree

3 files changed

+40
-44
lines changed

3 files changed

+40
-44
lines changed

src/ImageClassifier/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ class ImageClassifier {
4646
await this.ready;
4747
await tf.nextFrame();
4848

49-
// Classify the image using the selected model
50-
this.addedListener = true;
51-
await new Promise(resolve => this.video.addEventListener('onloadstart', resolve));
49+
if (this.video) {
50+
this.addedListener = true;
51+
await new Promise(resolve => this.video.addEventListener('onloadstart', resolve));
52+
}
53+
5254
return this.model.classify(imgToPredict, numberOfClasses);
5355
}
5456

src/ImageClassifier/index_test.js

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
11
/* eslint new-cap: 0 */
22

3-
// const { ImageClassifier } = ml5;
4-
5-
// const DEFAULTS = {
6-
// learningRate: 0.0001,
7-
// hiddenUnits: 100,
8-
// epochs: 20,
9-
// numClasses: 2,
10-
// batchSize: 0.4,
11-
// };
12-
13-
describe('underlying Mobilenet', () => {
14-
// This is the core issue: Mobilenet itself cannot be initialized
15-
// in the karma / webpack / etc environment
16-
// it('Can initialize mobilenet', async () => {
17-
// await mobilenet.load();
18-
// });
19-
});
3+
const { tf, imageClassifier } = ml5;
4+
5+
const DEFAULTS = {
6+
learningRate: 0.0001,
7+
hiddenUnits: 100,
8+
epochs: 20,
9+
numClasses: 2,
10+
batchSize: 0.4,
11+
topk: 3,
12+
alpha: 1,
13+
version: 1,
14+
};
2015

2116
describe('Create an image classifier', () => {
22-
// let classifier;
23-
24-
25-
// beforeEach(async () => {
26-
// jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
27-
// classifier = await imageClassifier('MobileNet', undefined, {});
28-
// });
29-
30-
// it('Should create a classifier with all the defaults', async () => {
31-
// expect(classifier.version).toBe(MOBILENET_DEFAULTS.version);
32-
// expect(classifier.alpha).toBe(MOBILENET_DEFAULTS.alpha);
33-
// expect(classifier.topk).toBe(MOBILENET_DEFAULTS.topk);
34-
// expect(classifier.ready).toBeTruthy();
35-
// });
36-
37-
// it('Should classify an image', async () => {
38-
// const img = new Image();
39-
// img.crossOrigin = '';
40-
// img.src = 'https://ml5js.org/docs/assets/img/bird.jpg';
41-
// await new Promise((resolve) => { img.onload = resolve; });
42-
// const results = await classifier.predict(img);
43-
// expect(results[0].className).toBe('robin, American robin, Turdus migratorius');
44-
// });
17+
let classifier;
18+
19+
beforeEach(async () => {
20+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
21+
classifier = await imageClassifier('MobileNet', undefined, {});
22+
});
23+
24+
it('Should create a classifier with all the defaults', async () => {
25+
expect(classifier.version).toBe(DEFAULTS.version);
26+
expect(classifier.alpha).toBe(DEFAULTS.alpha);
27+
expect(classifier.topk).toBe(DEFAULTS.topk);
28+
expect(classifier.ready).toBeTruthy();
29+
});
30+
31+
it('Should classify an robin, American robin, Turdus migratorius', async () => {
32+
const img = new Image();
33+
img.crossOrigin = '';
34+
img.src = 'https://ml5js.org/docs/assets/img/bird.jpg';
35+
await new Promise((resolve) => { img.onload = resolve; });
36+
classifier.predict(img)
37+
.then(results => expect(results[0].className).toBe('robin, American robin, Turdus migratorius'));
38+
});
4539
});

src/Word2vec/index_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint no-loop-func: 0 */
2-
const { tf, word2vec } = ml5;
2+
const { word2vec } = ml5;
33

44
const URL = 'https://raw.githubusercontent.com/ml5js/ml5-data-and-training/master/models/wordvecs/common-english/wordvecs1000.json';
55

0 commit comments

Comments
 (0)