Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load models from custom urls #30

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

33 changes: 25 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as faceLandmarksDetection from '@tensorflow-models/face-landmarks-detection';
import * as tf from '@tensorflow/tfjs-core';
import '@tensorflow/tfjs-backend-webgl';
import * as faceLandmarksDetection from "@tensorflow-models/face-landmarks-detection";
import * as tf from "@tensorflow/tfjs-core";
import "@tensorflow/tfjs-backend-webgl";

let model, video, event, blinkRate;
const VIDEO_SIZE = 500;
Expand All @@ -17,12 +17,29 @@ function initBlinkRateCalculator() {
}, 10000);
}

const loadModel = async () => {
await tf.setBackend('webgl');
/**
* Load the machine learning model
* @param {Object | undefined | null} c - a configuration object, could be used to define custom model urls.
* Has properties:
* - `modelUrl` - custom facemesh model url or a `tf.io.IOHandler` object
* - `irisModelUrl` - custom iris model url or a `tf.io.IOHandler` object
* - `detectorModelUrl` - custom blazeface model url or a `tf.io.IOHandler` object
*/
const loadModel = async (c) => {
await tf.setBackend("webgl");

if (!c) {
c = {};
}

model = await faceLandmarksDetection.load(
faceLandmarksDetection.SupportedPackages.mediapipeFacemesh,
{ maxFaces: 1 }
{
maxFaces: 1,
modelUrl: c.modelUrl ? c.modelUrl : null,
irisModelUrl: c.irisModelUrl ? c.irisModelUrl : null,
detectorModelUrl: c.detectorModelUrl ? c.detectorModelUrl : null,
}
);
};

Expand All @@ -32,15 +49,15 @@ const setUpCamera = async (videoElement) => {

const defaultWebcam = mediaDevices.find(
(device) =>
device.kind === 'videoinput' && device.label.includes('Built-in')
device.kind === "videoinput" && device.label.includes("Built-in")
);

const cameraId = defaultWebcam ? defaultWebcam?.deviceId : undefined;

const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
facingMode: 'user',
facingMode: "user",
deviceId: cameraId,
width: VIDEO_SIZE,
height: VIDEO_SIZE,
Expand Down