3
3
// This software is released under the MIT License.
4
4
// https://opensource.org/licenses/MIT
5
5
6
- const { imageClassifier } = ml5 ;
6
+ const {
7
+ imageClassifier
8
+ } = ml5 ;
9
+
10
+ const TM_URL = 'https://storage.googleapis.com/tm-models/WfgKPytY/model.json' ;
7
11
8
12
const DEFAULTS = {
9
13
learningRate : 0.0001 ,
@@ -16,67 +20,109 @@ const DEFAULTS = {
16
20
version : 2 ,
17
21
} ;
18
22
23
+ async function getImage ( ) {
24
+ const img = new Image ( ) ;
25
+ img . crossOrigin = true ;
26
+ img . src = 'https://cdn.jsdelivr.net/gh/ml5js/ml5-library@development/assets/bird.jpg' ;
27
+ await new Promise ( ( resolve ) => {
28
+ img . onload = resolve ;
29
+ } ) ;
30
+ return img ;
31
+ }
32
+
33
+ async function getCanvas ( ) {
34
+ const img = await getImage ( ) ;
35
+ const canvas = document . createElement ( 'canvas' ) ;
36
+ canvas . width = img . width ;
37
+ canvas . height = img . height ;
38
+ canvas . getContext ( '2d' ) . drawImage ( img , 0 , 0 ) ;
39
+ return canvas ;
40
+ }
41
+
19
42
describe ( 'imageClassifier' , ( ) => {
20
43
let classifier ;
21
44
22
- async function getImage ( ) {
23
- const img = new Image ( ) ;
24
- img . crossOrigin = true ;
25
- img . src = 'https://cdn.jsdelivr.net/gh/ml5js/ml5-library@development/assets/bird.jpg' ;
26
- await new Promise ( ( resolve ) => { img . onload = resolve ; } ) ;
27
- return img ;
28
- }
29
-
30
- async function getCanvas ( ) {
31
- const img = await getImage ( ) ;
32
- const canvas = document . createElement ( 'canvas' ) ;
33
- canvas . width = img . width ;
34
- canvas . height = img . height ;
35
- canvas . getContext ( '2d' ) . drawImage ( img , 0 , 0 ) ;
36
- return canvas ;
37
- }
38
-
39
- beforeEach ( async ( ) => {
40
- jasmine . DEFAULT_TIMEOUT_INTERVAL = 15000 ;
41
- classifier = await imageClassifier ( 'MobileNet' , undefined , { } ) ;
42
- } ) ;
45
+ /**
46
+ * Test imageClassifier with teachable machine
47
+ */
48
+ // Teachable machine model
49
+ describe ( 'with Teachable Machine model' , ( ) => {
50
+
51
+ beforeAll ( async ( ) => {
52
+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 15000 ;
53
+ classifier = await imageClassifier ( TM_URL , undefined , { } ) ;
54
+ } ) ;
55
+
56
+ describe ( 'instantiate' , ( ) => {
57
+ it ( 'Should create a classifier with all the defaults' , async ( ) => {
58
+ console . log ( classifier )
59
+ expect ( classifier . modelUrl ) . toBe ( TM_URL ) ;
60
+ } ) ;
61
+ } ) ;
43
62
44
- it ( 'Should create a classifier with all the defaults' , async ( ) => {
45
- expect ( classifier . version ) . toBe ( DEFAULTS . version ) ;
46
- expect ( classifier . alpha ) . toBe ( DEFAULTS . alpha ) ;
47
- expect ( classifier . topk ) . toBe ( DEFAULTS . topk ) ;
48
- expect ( classifier . ready ) . toBeTruthy ( ) ;
49
63
} ) ;
50
64
51
- describe ( 'classify' , ( ) => {
52
- it ( 'Should classify an image of a Robin' , async ( ) => {
53
- const img = await getImage ( ) ;
54
- await classifier . classify ( img )
55
- . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
56
- } ) ;
57
65
58
- it ( 'Should support p5 elements with an image on .elt' , async ( ) => {
59
- const img = await getImage ( ) ;
60
- await classifier . classify ( { elt : img } )
61
- . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
62
- } ) ;
63
66
64
- it ( 'Should support HTMLCanvasElement' , async ( ) => {
65
- const canvas = await getCanvas ( ) ;
66
- await classifier . classify ( canvas )
67
- . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
68
- } ) ;
67
+ /**
68
+ * Test imageClassifier with Mobilenet
69
+ */
70
+ describe ( 'imageClassifier with Mobilenet' , ( ) => {
69
71
70
- it ( 'Should support p5 elements with canvas on .canvas' , async ( ) => {
71
- const canvas = await getCanvas ( ) ;
72
- await classifier . classify ( { canvas } )
73
- . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
72
+ beforeAll ( async ( ) => {
73
+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 15000 ;
74
+ classifier = await imageClassifier ( 'MobileNet' , undefined , { } ) ;
74
75
} ) ;
75
76
76
- it ( 'Should support p5 elements with canvas on .elt' , async ( ) => {
77
- const canvas = await getCanvas ( ) ;
78
- await classifier . classify ( { elt : canvas } )
79
- . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
77
+ describe ( 'instantiate' , ( ) => {
78
+
79
+ it ( 'Should create a classifier with all the defaults' , async ( ) => {
80
+ expect ( classifier . version ) . toBe ( DEFAULTS . version ) ;
81
+ expect ( classifier . alpha ) . toBe ( DEFAULTS . alpha ) ;
82
+ expect ( classifier . topk ) . toBe ( DEFAULTS . topk ) ;
83
+ expect ( classifier . ready ) . toBeTruthy ( ) ;
84
+ } ) ;
85
+ } )
86
+
87
+ describe ( 'classify' , ( ) => {
88
+
89
+ it ( 'Should classify an image of a Robin' , async ( ) => {
90
+ const img = await getImage ( ) ;
91
+ await classifier . classify ( img )
92
+ . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
93
+ } ) ;
94
+
95
+ it ( 'Should support p5 elements with an image on .elt' , async ( ) => {
96
+ const img = await getImage ( ) ;
97
+ await classifier . classify ( {
98
+ elt : img
99
+ } )
100
+ . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
101
+ } ) ;
102
+
103
+ it ( 'Should support HTMLCanvasElement' , async ( ) => {
104
+ const canvas = await getCanvas ( ) ;
105
+ await classifier . classify ( canvas )
106
+ . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
107
+ } ) ;
108
+
109
+ it ( 'Should support p5 elements with canvas on .canvas' , async ( ) => {
110
+ const canvas = await getCanvas ( ) ;
111
+ await classifier . classify ( {
112
+ canvas
113
+ } )
114
+ . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
115
+ } ) ;
116
+
117
+ it ( 'Should support p5 elements with canvas on .elt' , async ( ) => {
118
+ const canvas = await getCanvas ( ) ;
119
+ await classifier . classify ( {
120
+ elt : canvas
121
+ } )
122
+ . then ( results => expect ( results [ 0 ] . label ) . toBe ( 'robin, American robin, Turdus migratorius' ) ) ;
123
+ } ) ;
80
124
} ) ;
125
+
81
126
} ) ;
82
- } ) ;
127
+
128
+ } )
0 commit comments