Camera Processor Virtual Background v0.9.11
Simple, Easy-to-use Background Masking Using Camera-Processor.
Framework
This package uses the camera-processor framework.
Preparation (For MLKit Model)
You need to host the MLKit Selfie Segmentation Model's .tflite file (here) somewhere on your server.
Since this package uses tflite-helper, you'll also need to do the steps described in that package's Preparation section.
Usage
import CameraProcessor from 'camera-processor';
import {
SegmentationAnalyzer,
VirtualBackgroundRenderer,
SEGMENTATION_BACKEND,
RENDER_PIPELINE,
VIRTUAL_BACKGROUND_TYPE
} from '@camera-processor/virtual-background';
const camera_processor = new CameraProcessor();
camera_processor.setCameraStream(camera_stream);
camera_processor.start();
const segmentation_analyzer = camera_processor.addAnalyzer(
'segmentation',
new SegmentationAnalyzer(SEGMENTATION_BACKEND.MLKit)
);
const background_renderer = camera_processor.addRenderer(new VirtualBackgroundRenderer(RENDER_PIPELINE._2D));
const image = new Image();
image.src = '...some image source';
background_renderer.setBackground(VIRTUAL_BACKGROUND_TYPE.Image, image);
segmentation_analyzer.loadModel({
modelPath: './tflite/models/selfie_segmentation.tflite',
modulePath: './tflite/'
});
const output_stream = camera_processor.getOutputStream();
Segmentation Analyzer
const segmentation_analyzer = new SegmentationAnalyzer(SEGMENTATION_BACKEND.MLKit);
segmentation_analyzer.loadModel({
modelPath: './tflite/models/selfie_segmentation.tflite',
modulePath: './tflite/'
});
segmentation_analyzer.setSegmentationSettings({});
segmentation_analyzer.setBackend(SEGMENTATION_BACKEND.BodyPix);
Virtual Background Renderer
const background_renderer = new VirtualBackgroundRenderer(RENDER_PIPELINE._2D);
background_renderer.setBackground(VIRTUAL_BACKGROUND_TYPE.Filter, 'blur(20px)');
background_renderer.setPipeline(RENDER_PIPELINE.WebGL);
background_renderer.setRenderSettings({ contourFilter: 'blur(8px)' });
TODO
- Cache image scaling in the _2DRenderPipeline and support more image options.
- Finish implementing the WebGLRenderPipeline. (which I probably won't do for a long time, since it's very compilcated and the performance gain isn't big either)