Skip to content

Commit

Permalink
setup options handling in util
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyuan-linn committed Jan 7, 2024
1 parent 5734120 commit 11bdbfc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/Handpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as tf from "@tensorflow/tfjs";
import * as handPoseDetection from "@tensorflow-models/hand-pose-detection";
import callCallback from "../utils/callcallback";
import handleArguments from "../utils/handleArguments";
import handleOptions from "../utils/handleOptions";
import { mediaReady } from "../utils/imageUtilities";

class Handpose {
Expand Down Expand Up @@ -61,16 +62,33 @@ class Handpose {
async loadModel() {
const pipeline = handPoseDetection.SupportedModels.MediaPipeHands;
//filter out model config options
const modelConfig = {
maxHands: this.config?.maxHands ?? 2,
runtime: this.config?.runtime ?? "mediapipe",
modelType: this.config?.modelType ?? "full",
solutionPath:
this.config?.solutionPath ??
"https://cdn.jsdelivr.net/npm/@mediapipe/hands",
detectorModelUrl: this.config?.detectorModelUrl,
landmarkModelUrl: this.config?.landmarkModelUrl,
};
const modelConfig = handleOptions(this.config, {
maxHands: {
type: "number",
default: 2,
},
runtime: {
type: "string",
default: "mediapipe",
},
modelType: {
type: "string",
default: "full",
},
solutionPath: {
type: "string",
default: "https://cdn.jsdelivr.net/npm/@mediapipe/hands",
},
detectorModelUrl: {
type: "string",
default: undefined,
},
landmarkModelUrl: {
type: "string",
default: undefined,
},
});
console.log(modelConfig);
this.runtimeConfig = {
flipHorizontal: this.config?.flipHorizontal ?? false,
};
Expand Down
44 changes: 44 additions & 0 deletions src/utils/handleOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This function takes in an object of options and an object of molds.
* Filters the options based on the moldObject and returns the filtered options.
* Logs out friendly warnings if the user options are not of the correct type or value.
*
* @param {object} optionsObject - options provided by the user
* @param {object} moldObject - an object defining how the user option should be filtered
* @returns {object} - filtered options
*/
function handleOptions(optionsObject, moldObject) {
const options = {};

for (const key in moldObject) {
const userValue = optionsObject[key];
const type = moldObject[key].type;
const defaultValue = moldObject[key].default;

if (userValue === undefined) {
options[key] = defaultValue;
} else if (typeof userValue !== type) {
console.warn(
`The value of ${key} is not of type ${type}. Using default value ${defaultValue} instead.`
);
options[key] = defaultValue;
} else {
switch (type) {
case "string":
break;
case "number":
break;
case "boolean":
break;
case "object":
break;
default:
throw new Error(`Unknown type ${type} for ${key} in moldObject.`);
}
}
}

return options;
}

export default handleOptions;

0 comments on commit 11bdbfc

Please sign in to comment.