abundant-magician-17307
07/23/2025, 5:47 AMtall-airline-16756
07/23/2025, 5:47 AMtall-airline-16756
07/23/2025, 5:49 AM<https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.14/wasm/> 下载以下文件:
▪︎ vision_wasm_internal.js
▪︎ vision_wasm_internal.wasm
▪︎ vision_wasm_nosimd_internal.js
▪︎ vision_wasm_nosimd_internal.wasm
◦ 从 <https://storage.googleapis.com/mediapipe-models/image_segmenter/selfie_segmenter/float16/latest/selfie_segmenter.tflite> 下载模型文件 selfie_segmenter.tflite
• 本地目录结构建议: public/
├── mediapipe/
│ ├── wasm/
│ │ ├── vision_wasm_internal.js
│ │ ├── vision_wasm_internal.wasm
│ │ ├── vision_wasm_nosimd_internal.js
│ │ └── vision_wasm_nosimd_internal.wasm
│ └── models/
│ └── selfie_segmenter.tflite
• 代码中自定义配置: import { BackgroundProcessor } from '@livekit/track-processors';
const backgroundProcessor = new BackgroundProcessor({
assetPaths: {
tasksVisionFileSet: '/mediapipe/wasm',
modelAssetPath: '/mediapipe/models/selfie_segmenter.tflite'
}
});
注意事项:
• 确保下载的wasm和模型文件版本与@mediapipe/tasks-vision依赖版本一致
• Web服务器需正确配置CORS,允许加载wasm文件
• 生产环境建议使用HTTPS,且需现代浏览器支持WebGL2等特性
通过上述方式即可在内网环境下使用LiveKit虚拟背景,无需访问外部CDN或Google Storage。
Sources: pnpm-lock.yamlabundant-magician-17307
07/23/2025, 6:09 AMtall-airline-16756
07/23/2025, 6:10 AMVirtualBackground, you only need to focus on two key fields in the assetPaths configuration: tasksVisionFileSet and modelAssetPath. No need to complicate things!
Here's how you'll want to write it:
const virtualBg = VirtualBackground('/path/to/background.jpg', {
assetPaths: {
tasksVisionFileSet: '/mediapipe/wasm',
modelAssetPath: '/mediapipe/models/selfie_segmenter.tflite'
}
});
As long as your /mediapipe/wasm directory has the necessary wasm files and the /mediapipe/models/selfie_segmenter.tflite path is correct, you're all set! This simple setup will work like a charm.
Pro tip: If you don't need any custom models or resources, you can even skip the assetPaths altogether. The system will happily use the default CDN paths for you.
Sources: README.mdabundant-magician-17307
07/23/2025, 6:13 AMtall-airline-16756
07/23/2025, 6:14 AM@livekit/track-processors version 0.5.1 or newer. Older versions won't play nice with assetPaths. Quick tip: run npm list @livekit/track-processors to see your current version.
• Configuration Format: Your assetPaths should look exactly like this: {
tasksVisionFileSet: '/mediapipe/wasm',
modelAssetPath: '/mediapipe/models/selfie_segmenter.tflite'
}
• Passing the Configuration: When calling VirtualBackground or BackgroundProcessor, remember to pass assetPaths as the fourth parameter (ProcessorWrapperOptions). Here's an example to help you out: const virtualBg = VirtualBackground(
'/path/to/background.jpg',
undefined,
undefined,
{
assetPaths: {
tasksVisionFileSet: '/mediapipe/wasm',
modelAssetPath: '/mediapipe/models/selfie_segmenter.tflite'
}
}
);
• Path Accessibility: Double-check that your local paths are browser-friendly. If not, it'll default back to the CDN path.
• Debugging Tip: Use your browser's developer tools (Network tab) to peek at which resources are actually being loaded. It'll help you confirm if your custom paths are working.
Pro tip: Focus on getting that version number right and making sure you're passing those assetPaths correctly. You've got this! 💪
Sources: vite.config.tsabundant-magician-17307
07/23/2025, 6:23 AMtall-airline-16756
07/23/2025, 6:24 AMVirtualBackground function's parameters in a friendly way. 😊
1. imagePath: string
2. segmenterOptions?: SegmenterOptions
3. onFrameProcessed?: (stats: FrameProcessingStats) => void
4. processorOptions?: ProcessorWrapperOptions(包含 assetPaths)
Here's the key thing to remember: assetPaths should be passed as the 4th parameter. Let me show you an example that'll make it crystal clear:
const virtualBg = VirtualBackground(
'/path/to/background.jpg',
undefined,
undefined,
{
assetPaths: {
tasksVisionFileSet: '/mediapipe/wasm',
modelAssetPath: '/mediapipe/models/selfie_segmenter.tflite'
}
}
);
Just set it up like this, and you're good to go! Just a heads up – don't try to sneak assetPaths in as the 2nd parameter. 👍
Sources: README.mdabundant-magician-17307
07/23/2025, 6:28 AM@livekit/track-processors 包中 VirtualBackground 函数的源码文件,我想看一下`VirtualBackground的定义`tall-airline-16756
07/23/2025, 6:29 AM@livekit/track-processors package's VirtualBackground function, which you'll find in the src/index.ts file. It's pretty neat! 😊
export const VirtualBackground = (
imagePath: string,
segmenterOptions?: SegmenterOptions,
onFrameProcessed?: (stats: FrameProcessingStats) => void,
processorOptions?: ProcessorWrapperOptions,
) => {
return BackgroundProcessor(
{
imagePath,
segmenterOptions,
onFrameProcessed,
...processorOptions,
},
'virtual-background',
);
};
Here's the scoop: this function actually calls BackgroundProcessor, putting together all the parameters and returning a processor instance. Pretty straightforward, right? 👍
Sources: src/transformers/index.tsabundant-magician-17307
07/23/2025, 7:13 AMtall-airline-16756
07/23/2025, 7:14 AMProcessorWrapperOptions interface. It's pretty straightforward and designed to give you some flexibility.
export interface ProcessorWrapperOptions {
/**
* Maximum frame rate for fallback canvas.captureStream implementation
* Default: 30
*/
maxFps?: number;
}
So, what's cool about this is it has just one optional parameter:
• maxFps?: number —— This lets you control the maximum frame rate when using a fallback canvas.captureStream implementation. By default, it's set to 30, but you can adjust it to suit your needs.
Just to give you a bit more context, this interface is used by others like BackgroundProcessorOptions to help manage performance settings for fallback implementations. Pretty handy, right?
Sources: src/transformers/types.tsabundant-magician-17307
07/23/2025, 8:37 AMtall-airline-16756
07/23/2025, 8:39 AMval options = SelfieSegmenterOptions.Builder()
.setDetectorMode(SelfieSegmenterOptions.STREAM_MODE)
.build()
segmenter = Segmentation.getClient(options)
Feel free to play around with the SelfieSegmenterOptions parameters to find what works best for you.
2. Smooth Out Those Edges:
float edgeSoftness = 6.0; // Increasing this value can make edges softer
float smoothAlpha = smoothstep(0.5 - grad * edgeSoftness, 0.5 + grad * edgeSoftness, maskVal);
In the CompositeShader, adjusting edgeSoftness can really help refine your profile outline.
3. Fine-Tune Background Blur:
class VirtualBackgroundTransformer(
val blurRadius: Float = 16f,
val downSampleFactor: Int = 2,
)
Experiment with blurRadius and downSampleFactor to get that perfect visual vibe.
Pro tip: Take your time and adjust these settings gradually. Every little tweak can make a big difference in getting that crisp, clean virtual background look!
Sources: video-encode-decode-test/src/main/res/drawable/ic_launcher_background.xml