You need to enable JavaScript to run this app.
导航

自定义虚拟背景

最近更新时间2023.12.19 14:08:31

首次发布时间2023.11.21 14:51:30

除了通过使用「特效包」,你还可以通过 session.virtualBackgroundController 来控制虚拟背景。但请注意,如果特效包中配置了虚拟背景,特效包中的虚拟背景效果会优先展示。

// 使用模糊背景效果
// Use "blur" virtual background effect.
await session.virtualBackgroundController.updateVirtualBackground({
    type: "blur"
});
// 设置模糊程度
// Set the blur radius.
session.virtualBackgroundController.blurSettings.radius = 20;

// 使用图片作为虚拟背景
// Use an image as virtual background.
await session.virtualBackgroundController.updateVirtualBackground({
    type: "image-url",
    url: imageURL
});

// 使用视频作为虚拟背景
// Use a video as virtual background.
await session.virtualBackgroundController.updateVirtualBackground({
    type: "video-url",
    url: videoURL
});

// 使用 RGBA 颜色作为虚拟背景 (3.5.0 版本开始可用)
// Use a rgba color as virtual background. (Available from 3.5.0)
await session.virtualBackgroundController.updateVirtualBackground({
    type: "color",
    r: 1,
    g: 1,
    b: 1,
    a: 1
});

// 清除虚拟背景
// Clear virtual background.
await session.virtualBackgroundController.updateVirtualBackground(null);

虚拟背景图片支持使用 jpeg 或 png 格式,图像最长边最好小于 2048 像素。
虚拟背景视频仅支持 mp4 格式,虚拟背景视频暂不支持播放声音。视频文件具体要求如下:

封装格式mp4
视频编码AVC/H.264
视频码率平均不大于 2000kbps
视频分辨率长边不大于 1920
视频帧率不大于 30fps
音频编码AAC-LC
音频码率平均不大于 128kbps
服务要求服务器需要支持 Range Requests

选择人像分割模型

从 3.2.0 版本开始可用
我们提供了多个人像分割算法模型:

  • accurate 更精确,但性能不如 fast 模型,默认模型。

  • fast 性能更好,但精度较差。

你可以通过 virtualBackgroundControllerupdatePortraitMattingModelType 来选择模型的使用:

await session.virtualBackgroundController.updatePortraitMattingModelType("accurate");

选择人像分割推理后端

从 3.2.0 版本开始可用
我们提供了多个人像分割推理后端:

  • cpu 利用 CPU 和 SIMD 加速进行计算。默认推理后端。

  • gpu 利用 Web GL / Web GPU 接口进行计算。

你可以通过 virtualBackgroundControllerupdatePreferredPortraitMattingInferenceBackend 来选择要使用的推理后端:

await session.virtualBackgroundController.updatePreferredPortraitMattingInferenceBackend("cpu");

我们推荐使用 CPU 推理后端,除非遭遇了明显的性能问题。