I am trying to use BodyPix/Tensorflow to blur the background fo my webcam. I'm trying to follow this guide. https://github.com/vinooniv/video-bg-blur . It obviously works for them since they have a live example.
Here's my code:
ngOnInit(): void {
const videoElement = document.getElementById('face-blur-video') as HTMLVideoElement
const canvas = document.getElementById('blur-canvas') as HTMLCanvasElement;
videoElement.onplaying = () => {
canvas.height = videoElement.height
canvas.width = videoElement.width
}
const context = canvas.getContext('2d');
context.canvas.height = 320;
context.canvas.height = 480;
navigator.mediaDevices.getUserMedia({video: true, audio: true})
.then(stream => {
videoElement.srcObject = stream;
videoElement.play();
const options: ModelConfig = {
multiplier: .75,
outputStride: 16,
quantBytes: 4,
architecture: 'MobileNetV1'
}
tfjs.getBackend();
BodyPix.load(options).then(bp => this.perform(bp))
})
async perform(net: BodyPix.BodyPix) {
while(true) {
const videoElement = document.getElementById('face-blur-video') as HTMLVideoElement
const canvas = document.getElementById('blur-canvas') as HTMLCanvasElement;
const segmentation = await net.segmentPerson(videoElement)
const backgroundBlurAmount = 6
const edgeBlurAmount = 2;
const flipHorizontal = true;
BodyPix.drawBokehEffect(canvas,videoElement,segmentation,backgroundBlurAmount,edgeBlurAmount,flipHorizontal)
}
However, when the application runs I get the following error:
core.js:6162 ERROR Error: Uncaught (in promise): InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.
Error: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.
at drawWithCompositing (body-pix.esm.js:17)
at Module.drawBokehEffect (body-pix.esm.js:17)
at FaceBlurComponent.<anonymous> (face-blur.component.ts:56)
at Generator.next (<anonymous>)
at fulfilled (tslib.es6.js:73)
at ZoneDelegate.invoke (zone-evergreen.js:372)
at Object.onInvoke (core.js:28510)
at ZoneDelegate.invoke (zone-evergreen.js:371)
at Zone.run (zone-evergreen.js:134)
at zone-evergreen.js:1276
at resolvePromise (zone-evergreen.js:1213)
at zone-evergreen.js:1120
at zone-evergreen.js:1136
at ZoneDelegate.invoke (zone-evergreen.js:372)
at Object.onInvoke (core.js:28510)
at ZoneDelegate.invoke (zone-evergreen.js:371)
at Zone.run (zone-evergreen.js:134)
at zone-evergreen.js:1276
at ZoneDelegate.invokeTask (zone-evergreen.js:406)
at Object.onInvokeTask (core.js:28497)
I have set the width and height in every way possible. Why is TensorFlow detecting the canvas height/width as zero?