tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.38k stars 1.92k forks source link

[BodyPix] Feature request: Rendering utils for removing background #2736

Closed leaysgur closed 2 years ago

leaysgur commented 4 years ago

Hi team.

Now I'm working on implementing "Virtual background" filter for video chat.

And of course, there are several ways to achieve it.

It would be helpful if there is the API like removeBackground().

How do you think? If it is appreciated, I'd like to submit PR for that.

Memo: implementation details will be.. ```ts export function removeBackground( canvas: HTMLCanvasElement, image: ImageType, multiPersonSegmentation: SemanticPersonSegmentation|PersonSegmentation[], edgeBlurAmount = 3, flipHorizontal = false) { const [height, width] = getInputSize(image); canvas.width = width; canvas.height = height; const personMask = createPersonMask(multiPersonSegmentation, edgeBlurAmount); const ctx = canvas.getContext('2d'); ctx.save(); if (flipHorizontal) { flipCanvasHorizontal(canvas); } // draw the original image on the final canvas ctx.drawImage(image, 0, 0, width, height); // "destination-in" - "The existing canvas content is kept where both the // new shape and existing canvas content overlap. Everything else is made // transparent." // crop what's not the person using the mask from the original image drawWithCompositing(ctx, personMask, 'destination-in'); ctx.restore(); } ```
rthadur commented 2 years ago

Will be closing this issue as we will be using latest body segmentation model for same purpose. Please check latest body segmentation model for latest bodypix model demo https://github.com/tensorflow/tfjs-models/tree/master/body-segmentation