Skip to content

Commit cd4b799

Browse files
committed
feat: install mediapipe via npm and add fallback to CDN
- Install @mediapipe/face_detection package through npm - Copy mediapipe files into the output folder during build - Add fallback logic to load mediapipe from CDN if local files are missing
1 parent 38e328c commit cd4b799

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/scratch-gui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"watch": "webpack --watch"
5858
},
5959
"dependencies": {
60+
"@mediapipe/face_detection": "0.4.1646425229",
6061
"@microbit/microbit-universal-hex": "0.2.2",
6162
"@tensorflow-models/face-detection": "^1.0.3",
6263
"@tensorflow/tfjs": "^4.22.0",

packages/scratch-gui/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const baseConfig = new ScratchWebpackConfigBuilder(
8484
context: '../../node_modules/scratch-storage/dist/web',
8585
from: 'chunks/fetch-worker.*.{js,js.map}',
8686
noErrorOnMissing: true
87+
},
88+
{
89+
from: '../../node_modules/@mediapipe/face_detection',
90+
to: 'chunks/mediapipe/face_detection'
8791
}
8892
]
8993
}));

packages/scratch-vm/src/extensions/scratch3_face_sensing/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,26 @@ class Scratch3FaceSensingBlocks {
4141
const model = FaceDetection.SupportedModels.MediaPipeFaceDetector;
4242
const detectorConfig = {
4343
runtime: 'mediapipe',
44-
solutionPath: 'https://cdn.jsdelivr.net/npm/@mediapipe/face_detection',
44+
solutionPath: '/chunks/mediapipe/face_detection',
4545
maxFaces: 1
4646
};
4747

48-
FaceDetection.createDetector(model, detectorConfig).then(detector => {
49-
this.faceDetector = detector;
50-
if (this.runtime.ioDevices) {
51-
this._loop();
52-
}
53-
});
48+
FaceDetection.createDetector(model, detectorConfig)
49+
.catch(() => {
50+
const fallbackConfig = {
51+
runtime: 'mediapipe',
52+
solutionPath: 'https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]',
53+
maxFaces: 1
54+
};
55+
56+
return FaceDetection.createDetector(model, fallbackConfig);
57+
})
58+
.then(detector => {
59+
this.faceDetector = detector;
60+
if (this.runtime.ioDevices) {
61+
this._loop();
62+
}
63+
});
5464

5565
this.cachedSize = 100;
5666
this.cachedTilt = 90;

0 commit comments

Comments
 (0)