Skip to content

Commit 26cc285

Browse files
[blazeface] Build and serve demo with esbuild (#851)
* [blazeface] Use esbuild to build and serve the demo * [blazeface] Fix demo lint errors * [blazeface] Add missing mkdirp dep to demo * [blazeface] Remove extra yarn command from demo * [blazeface] Minify the demo
1 parent 45bf4cf commit 26cc285

File tree

5 files changed

+132
-6060
lines changed

5 files changed

+132
-6060
lines changed

blazeface/demo/index.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,29 @@ const stats = new Stats();
2828
stats.showPanel(0);
2929
document.body.prepend(stats.domElement);
3030

31-
let model, ctx, videoWidth, videoHeight, video, canvas;
31+
let model;
32+
let ctx;
33+
let videoWidth;
34+
let videoHeight;
35+
let video;
36+
let canvas;
3237

3338
const state = {
34-
backend: 'wasm'
39+
backend: 'wasm',
3540
};
3641

3742
const gui = new dat.GUI();
38-
gui.add(state, 'backend', ['wasm', 'webgl', 'cpu']).onChange(async backend => {
39-
await tf.setBackend(backend);
40-
});
43+
gui.add(state, 'backend', ['wasm', 'webgl', 'cpu'])
44+
.onChange(async (backend) => {
45+
await tf.setBackend(backend);
46+
});
4147

4248
async function setupCamera() {
4349
video = document.getElementById('video');
4450

4551
const stream = await navigator.mediaDevices.getUserMedia({
4652
'audio': false,
47-
'video': { facingMode: 'user' },
53+
'video': {facingMode: 'user'},
4854
});
4955
video.srcObject = stream;
5056

@@ -79,13 +85,13 @@ const renderPrediction = async () => {
7985
const start = predictions[i].topLeft;
8086
const end = predictions[i].bottomRight;
8187
const size = [end[0] - start[0], end[1] - start[1]];
82-
ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
88+
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
8389
ctx.fillRect(start[0], start[1], size[0], size[1]);
8490

8591
if (annotateBoxes) {
8692
const landmarks = predictions[i].landmarks;
8793

88-
ctx.fillStyle = "blue";
94+
ctx.fillStyle = 'blue';
8995
for (let j = 0; j < landmarks.length; j++) {
9096
const x = landmarks[j][0];
9197
const y = landmarks[j][1];
@@ -114,7 +120,7 @@ const setupPage = async () => {
114120
canvas.width = videoWidth;
115121
canvas.height = videoHeight;
116122
ctx = canvas.getContext('2d');
117-
ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
123+
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
118124

119125
model = await blazeface.load();
120126

blazeface/demo/package.json

+8-16
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,28 @@
99
"node": ">=8.9.0"
1010
},
1111
"dependencies": {
12-
"@tensorflow-models/blazeface": "link:../dist",
13-
"@tensorflow/tfjs-backend-wasm": "^3.1.0",
14-
"@tensorflow/tfjs-backend-webgl": "^3.1.0",
15-
"@tensorflow/tfjs-converter": "^3.1.0",
16-
"@tensorflow/tfjs-core": "3.1.0"
12+
"@tensorflow-models/blazeface": "link:../",
13+
"@tensorflow/tfjs-backend-wasm": "^3.11.0",
14+
"@tensorflow/tfjs-backend-webgl": "^3.11.0",
15+
"@tensorflow/tfjs-converter": "^3.11.0",
16+
"@tensorflow/tfjs-core": "^3.11.0"
1717
},
1818
"scripts": {
19-
"watch": "cross-env NODE_ENV=development parcel index.html --no-hmr --open --target browser",
20-
"build": "cross-env NODE_ENV=production parcel build index.html --no-minify --target browser --public-url ./",
19+
"watch": "esbuild index.js --bundle --target=es6 --servedir=. --serve --sourcemap --sources-content=true --preserve-symlinks",
20+
"build": "mkdirp dist && cp index.html dist && esbuild index.js --bundle --target=es6 --outfile=dist/index.js --sourcemap --sources-content=true --preserve-symlinks --minify",
2121
"lint": "eslint .",
22-
"link-local": "yalc link @tensorflow-models/blazeface",
2322
"build-model": "cd .. && yarn && yarn build-npm",
2423
"build-deps": "yarn build-model"
2524
},
2625
"browser": {
2726
"crypto": false
2827
},
2928
"devDependencies": {
30-
"@babel/core": "^7.7.5",
31-
"@babel/plugin-transform-runtime": "^7.7.6",
32-
"@babel/polyfill": "^7.10.4",
33-
"@babel/preset-env": "^7.7.6",
34-
"babel-preset-env": "^1.7.0",
3529
"clang-format": "~1.2.2",
36-
"cross-env": "^5.2.0",
3730
"dat.gui": "^0.7.2",
3831
"eslint": "^4.19.1",
3932
"eslint-config-google": "^0.9.1",
40-
"parcel-bundler": "~1.12.5",
41-
"yalc": "~1.0.0-pre.50"
33+
"mkdirp": "^1.0.4"
4234
},
4335
"resolutions": {
4436
"is-svg": "4.3.1"

0 commit comments

Comments
 (0)