Skip to content

Commit d305fd3

Browse files
committed
Change to name exports
1 parent dcac448 commit d305fd3

File tree

12 files changed

+134
-168
lines changed

12 files changed

+134
-168
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"keywords": [
2121
"ML5"
2222
],
23+
"sideEffects": [
24+
"src/index.js"
25+
],
2326
"author": "ml5",
2427
"license": "MIT",
2528
"repository": {

src/BodyPose/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import handleOptions from "../utils/handleOptions";
2727
import { handleModelName } from "../utils/handleOptions";
2828
import objectRenameKey from "../utils/objectRenameKey";
2929
import { isVideo } from "../utils/handleArguments";
30+
import p5Utils from "../utils/p5Utils";
3031

3132
/**
3233
* User provided options object for BodyPose with MoveNet model.
@@ -547,10 +548,7 @@ class BodyPose {
547548
* @param {function} callback - A callback function that is called once the model has been loaded.
548549
* @returns {BodyPose} A BodyPose instance.
549550
*/
550-
const bodyPose = (...inputs) => {
551+
export const bodyPose = p5Utils.maybeRegisterPreload((...inputs) => {
551552
const { string, options = {}, callback } = handleArguments(...inputs);
552-
const instance = new BodyPose(string, options, callback);
553-
return instance;
554-
};
555-
556-
export default bodyPose;
553+
return new BodyPose(string, options, callback);
554+
});

src/BodySegmentation/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import BODYPIX_PALETTE from "./BODYPIX_PALETTE";
1616
import { mediaReady } from "../utils/imageUtilities";
1717
import handleOptions from "../utils/handleOptions";
1818
import { handleModelName } from "../utils/handleOptions";
19+
import p5Utils from "../utils/p5Utils";
1920

2021
class BodySegmentation {
2122
/**
@@ -411,10 +412,7 @@ class BodySegmentation {
411412
* Factory function that returns a Facemesh instance
412413
* @returns {Object} A new bodySegmentation instance
413414
*/
414-
const bodySegmentation = (...inputs) => {
415+
export const bodySegmentation = p5Utils.maybeRegisterPreload((...inputs) => {
415416
const { string, options = {}, callback } = handleArguments(...inputs);
416-
const instance = new BodySegmentation(string, options, callback);
417-
return instance;
418-
};
419-
420-
export default bodySegmentation;
417+
return new BodySegmentation(string, options, callback);
418+
});

src/FaceMesh/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { mediaReady } from "../utils/imageUtilities";
2626
import handleOptions from "../utils/handleOptions";
2727
import { handleModelName } from "../utils/handleOptions";
2828
import { UV_COORDS } from "./uv_coords";
29+
import p5Utils from "../utils/p5Utils";
2930

3031
/**
3132
* User provided options object for FaceMesh. See config schema below for default and available values.
@@ -375,10 +376,7 @@ class FaceMesh {
375376
* @param {function} [callback] - A callback to be called when the model is ready.
376377
* @returns {Object} A new faceMesh instance.
377378
*/
378-
const faceMesh = (...inputs) => {
379+
export const faceMesh = p5Utils.maybeRegisterPreload((...inputs) => {
379380
const { string, options = {}, callback } = handleArguments(...inputs);
380-
const instance = new FaceMesh(string, options, callback);
381-
return instance;
382-
};
383-
384-
export default faceMesh;
381+
return new FaceMesh(string, options, callback);
382+
});

src/HandPose/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import handleOptions from "../utils/handleOptions";
2727
import { handleModelName } from "../utils/handleOptions";
2828
import { mediaReady } from "../utils/imageUtilities";
2929
import objectRenameKey from "../utils/objectRenameKey";
30+
import p5Utils from "../utils/p5Utils";
3031

3132
/**
3233
* User provided options object for HandPose. See config schema below for default and available values.
@@ -311,10 +312,7 @@ class HandPose {
311312
* @param {function} [callback] - A callback function that is called once the model has been loaded.
312313
* @returns {HandPose} A new handPose instance.
313314
*/
314-
const handPose = (...inputs) => {
315+
export const handPose = p5Utils.maybeRegisterPreload((...inputs) => {
315316
const { string, options = {}, callback } = handleArguments(...inputs);
316-
const instance = new HandPose(string, options, callback);
317-
return instance;
318-
};
319-
320-
export default handPose;
317+
return new HandPose(string, options, callback);
318+
});

src/ImageClassifier/index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import callCallback from "../utils/callcallback";
1818
import { imgToTensor, mediaReady } from "../utils/imageUtilities";
1919
import handleOptions from "../utils/handleOptions";
2020
import { handleModelName } from "../utils/handleOptions";
21+
import p5Utils from "../utils/p5Utils";
2122

2223
const IMAGE_SIZE = 224;
2324
const MODEL_OPTIONS = ["mobilenet", "darknet", "darknet-tiny", "doodlenet"];
@@ -248,7 +249,10 @@ class ImageClassifier {
248249
"image",
249250
"No input image provided. If you want to classify a video, use classifyStart."
250251
);
251-
return callCallback(this.classifyInternal(image, number || this.topk), callback);
252+
return callCallback(
253+
this.classifyInternal(image, number || this.topk),
254+
callback
255+
);
252256
}
253257

254258
/**
@@ -270,8 +274,11 @@ class ImageClassifier {
270274
const classifyFrame = async () => {
271275
await mediaReady(image, true);
272276
// call the callback function
273-
await callCallback(this.classifyInternal(image, number || this.topk), callback);
274-
277+
await callCallback(
278+
this.classifyInternal(image, number || this.topk),
279+
callback
280+
);
281+
275282
// call recursively for continuous classification
276283
if (!this.signalStop) {
277284
requestAnimationFrame(classifyFrame);
@@ -305,13 +312,10 @@ class ImageClassifier {
305312
}
306313
}
307314

308-
const imageClassifier = (modelName, optionsOrCallback, cb) => {
309-
const args = handleArguments(modelName, optionsOrCallback, cb);
310-
311-
const { string, options = {}, callback } = args;
312-
313-
const instance = new ImageClassifier(string, options, callback);
314-
return instance;
315-
};
316-
317-
export default imageClassifier;
315+
export const imageClassifier = p5Utils.maybeRegisterPreload(
316+
(modelName, optionsOrCallback, cb) => {
317+
const args = handleArguments(modelName, optionsOrCallback, cb);
318+
const { string, options = {}, callback } = args;
319+
return new ImageClassifier(string, options, callback);
320+
}
321+
);

src/NeuralNetwork/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import NeuralNetworkData from "./NeuralNetworkData";
1010

1111
import nnUtils from "./NeuralNetworkUtils";
1212
import NeuralNetworkVis from "./NeuralNetworkVis";
13+
import p5Utils from "../utils/p5Utils";
1314

1415
const DEFAULTS = {
1516
inputs: [],
@@ -1249,23 +1250,22 @@ class DiyNeuralNetwork {
12491250
}
12501251
}
12511252

1252-
const neuralNetwork = (inputsOrOptions, outputsOrCallback, callback) => {
1253-
let options;
1254-
let cb;
1255-
1256-
if (inputsOrOptions instanceof Object) {
1257-
options = inputsOrOptions;
1258-
cb = outputsOrCallback;
1259-
} else {
1260-
options = {
1261-
inputs: inputsOrOptions,
1262-
outputs: outputsOrCallback,
1263-
};
1264-
cb = callback;
1265-
}
1253+
export const neuralNetwork = p5Utils.maybeRegisterPreload(
1254+
(inputsOrOptions, outputsOrCallback, callback) => {
1255+
let options;
1256+
let cb;
12661257

1267-
const instance = new DiyNeuralNetwork(options, cb);
1268-
return instance;
1269-
};
1258+
if (inputsOrOptions instanceof Object) {
1259+
options = inputsOrOptions;
1260+
cb = outputsOrCallback;
1261+
} else {
1262+
options = {
1263+
inputs: inputsOrOptions,
1264+
outputs: outputsOrCallback,
1265+
};
1266+
cb = callback;
1267+
}
12701268

1271-
export default neuralNetwork;
1269+
return new DiyNeuralNetwork(options, cb);
1270+
}
1271+
);

src/Sentiment/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import callCallback from "../utils/callcallback";
33
import modelLoader from "../utils/modelLoader";
44
import handleArguments from "../utils/handleArguments";
55
import { handleModelName } from "../utils/handleOptions";
6+
import p5Utils from "../utils/p5Utils";
7+
68
/**
79
* Initializes the Sentiment demo.
810
*/
@@ -143,10 +145,7 @@ class Sentiment {
143145
}
144146
}
145147

146-
const sentiment = (...inputs) => {
148+
export const sentiment = p5Utils.maybeRegisterPreload((...inputs) => {
147149
const { string, callback } = handleArguments(...inputs);
148-
const instance = new Sentiment(string, callback);
149-
return instance;
150-
};
151-
152-
export default sentiment;
150+
return new Sentiment(string, callback);
151+
});

src/SoundClassifier/index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as tf from "@tensorflow/tfjs";
1111
import handleArguments from "../utils/handleArguments";
1212
import * as speechCommands from "./speechcommands";
1313
import callCallback from "../utils/callcallback";
14+
import p5Utils from "../utils/p5Utils";
1415

1516
const MODEL_OPTIONS = ["speechcommands18w"];
1617
// exporting the sound classifier instance so that the stop/start flags regarding classification state are accessible to speechcommands.js to use
@@ -98,32 +99,31 @@ class SoundClassifier {
9899
/**
99100
* Used to stop the continuous classification of a video
100101
*/
101-
classifyStop() {
102+
classifyStop() {
102103
if (this.isClassifying) {
103104
this.signalStop = true;
104105
}
105106
this.prevCall = "stop";
106107
}
107108
}
108109

109-
const soundClassifier = (modelName, optionsOrCallback, cb) => {
110-
const {
111-
string,
112-
options = {},
113-
callback,
114-
} = handleArguments(modelName, optionsOrCallback, cb).require(
115-
"string",
116-
'Please specify a model to use. E.g: "SpeechCommands18w"'
117-
);
118-
119-
let model = string;
110+
export const soundClassifier = p5Utils.maybeRegisterPreload(
111+
(modelName, optionsOrCallback, cb) => {
112+
const {
113+
string,
114+
options = {},
115+
callback,
116+
} = handleArguments(modelName, optionsOrCallback, cb).require(
117+
"string",
118+
'Please specify a model to use. E.g: "SpeechCommands18w"'
119+
);
120+
121+
let model = string;
122+
123+
if (model.indexOf("http") === -1) {
124+
model = model.toLowerCase();
125+
}
120126

121-
if (model.indexOf("http") === -1) {
122-
model = model.toLowerCase();
127+
return new SoundClassifier(model, options, callback);
123128
}
124-
125-
instance = new SoundClassifier(model, options, callback);
126-
return instance;
127-
};
128-
129-
export default soundClassifier;
129+
);

src/index.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
import neuralNetwork from "./NeuralNetwork";
2-
import handPose from "./HandPose";
3-
import sentiment from "./Sentiment";
4-
import faceMesh from "./FaceMesh";
5-
import bodyPose from "./BodyPose";
6-
import imageClassifier from "./ImageClassifier";
7-
import soundClassifier from "./SoundClassifier";
1+
import { neuralNetwork } from "./NeuralNetwork";
2+
import { handPose } from "./HandPose";
3+
import { sentiment } from "./Sentiment";
4+
import { faceMesh } from "./FaceMesh";
5+
import { bodyPose } from "./BodyPose";
6+
import { imageClassifier } from "./ImageClassifier";
7+
import { soundClassifier } from "./SoundClassifier";
8+
import { bodySegmentation } from "./BodySegmentation";
89
import setBackend from "./utils/setBackend";
9-
import bodySegmentation from "./BodySegmentation";
1010
import communityStatement from "./utils/communityStatement";
1111
import * as tf from "@tensorflow/tfjs";
1212
import * as tfvis from "@tensorflow/tfjs-vis";
1313
import p5Utils from "./utils/p5Utils";
1414
import packageInfo from "../package.json";
1515

16-
const withPreload = {
16+
communityStatement();
17+
18+
const version = packageInfo.version;
19+
const setP5 = p5Utils.setP5.bind(p5Utils);
20+
21+
export {
1722
bodyPose,
1823
bodySegmentation,
1924
faceMesh,
@@ -22,18 +27,10 @@ const withPreload = {
2227
neuralNetwork,
2328
sentiment,
2429
soundClassifier,
25-
};
26-
27-
const ml5 = Object.assign({ p5Utils }, withPreload, {
30+
p5Utils,
2831
tf,
2932
tfvis,
3033
setBackend,
31-
version: packageInfo.version,
32-
setP5: p5Utils.setP5.bind(p5Utils),
33-
});
34-
35-
p5Utils.shouldPreload(ml5, Object.keys(withPreload));
36-
37-
communityStatement();
38-
39-
export default ml5;
34+
version,
35+
setP5,
36+
};

0 commit comments

Comments
 (0)