-
Notifications
You must be signed in to change notification settings - Fork 1
/
change-background-in-video-script.js
72 lines (62 loc) · 2.35 KB
/
change-background-in-video-script.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
let video = document.getElementById("webcam");
let webcamCanvas = document.getElementById("canvas");
let webcamCanvasCtx = webcamCanvas.getContext('2d');
//In Memory Canvas used for model prediction
var tempCanvas = document.createElement('canvas');
var tempCanvasCtx = tempCanvas.getContext('2d');
let previousSegmentationComplete = true;
let segmentationProperties = {
segmentationThreshold: 0.7,
internalResolution: 'full'
}
var model;
bodyPix.load().then(function (loadedModel) {
model = loadedModel;
});
function main() {
if(navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({video: true})
.then(stream => {
video.srcObject = stream;
})
.catch(e => {
console.log("Error occurred while getting the video stream");
});
}
video.onloadedmetadata = () => {
webcamCanvas.width = video.videoWidth;
webcamCanvas.height = video.videoHeight;
tempCanvas.width = video.videoWidth;
tempCanvas.height = video.videoHeight;
};
video.addEventListener("loadeddata", segmentPersons);
}
function segmentPersons() {
tempCanvasCtx.drawImage(video, 0, 0);
if (previousSegmentationComplete) {
previousSegmentationComplete = false;
// Now classify the canvas image we have available.
model.segmentPerson(tempCanvas, segmentationProperties)
.then(segmentation => {
processSegmentation(segmentation);
previousSegmentationComplete = true;
});
}
//Call this function repeatedly to perform segmentation on all frames of the video.
window.requestAnimationFrame(segmentPersons);
}
function processSegmentation(segmentation) {
var imgData = tempCanvasCtx.getImageData(0, 0, webcamCanvas.width, webcamCanvas.height);
//Loop through the pixels in the image
for(let i = 0; i < imgData.data.length; i+=4) {
let pixelIndex = i/4;
//Make the pixel transparent if it does not belong to a person using the body-pix model's output data array.
//This removes all pixels corresponding to the background.
if(segmentation.data[pixelIndex] == 0) {
imgData.data[i + 3] = 0;
}
}
//Draw the updated image on the canvas
webcamCanvasCtx.putImageData(imgData, 0, 0);
}
main();