-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
83 lines (71 loc) · 2.17 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const DEFAULT_SETTINGS = {
blurAmount: 3,
edgeBlurAmount: 10,
};
let model, video, canvas, settings, segmentation, lastSegmentation;
const init = async () => {
model = await bodyPix.load({
outputStride: 16,
multiplier: 0.5,
});
video = await prepareWebcam();
video.height = video.videoHeight;
video.width = video.videoWidth;
video.play();
canvas = document.getElementById("output");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
settings = getSettings();
await doFrame();
};
const getSettings = () => {
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
return Object.assign(DEFAULT_SETTINGS, params);
};
const prepareWebcam = async () => {
const video = document.getElementById("video");
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
facingMode: "user",
width: { ideal: 1280 },
height: { ideal: 720 },
frameRate: { ideal: 30, max: 30 },
},
});
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
};
});
};
const doFrame = async (now, metadata) => {
if (!metadata || (!lastSegmentation || lastSegmentation < metadata.presentationTime - 500)) {
if (metadata) {
lastSegmentation = metadata.presentationTime;
}
segmentation = await model.segmentPerson(video, {
flipHorizontal: false,
internalResolution: 0.5,
segmentationThreshold: 0.7,
maxDetections: 1,
});
}
const backgroundBlurAmount = parseInt(settings.blurAmount);
const edgeBlurAmount = parseInt(settings.edgeBlurAmount);
const flipHorizontal = false;
bodyPix.drawBokehEffect(
canvas,
video,
segmentation,
backgroundBlurAmount,
edgeBlurAmount,
flipHorizontal
);
video.requestVideoFrameCallback(doFrame);
};
(async () => {
tf.setBackend("webgl").then(async () => await init());
})();