Object Segmentation
Feature Overview
Object Segmentation is a value-added capability provided by Express SDK. It uses AI algorithms to recognize content in video frames and sets transparency information for each pixel. Pixels of the main subject are set to "opaque", while pixels outside the main subject are set to "transparent". Developers can use the transparency information of these pixels to process the subject and non-subject parts of the image differently, thereby implementing different features.
- The current official SDK does not include "Object Segmentation" related features. If needed, please contact ZEGOCLOUD Technical Support for a special build and provide your AppID to enable relevant permissions.
- "Object Segmentation" is a paid feature. For trial or formal pricing inquiries, please contact ZEGOCLOUD sales personnel.
Object Segmentation Types
For users in different environments, ZEGO provides two segmentation capabilities: "Green Screen Background Segmentation" and "Arbitrary Background Segmentation".
| Segmentation Type | Green Screen Background Segmentation | Arbitrary Background Segmentation |
|---|---|---|
| Capability Description | When users set up a green screen themselves, the subject in the non-green screen area can be retained. Suitable for e-commerce live streaming, online exams, and other scenarios. | Most users do not have the conditions to set up a green screen. Through ZEGO's arbitrary background segmentation capability, the subject in the image can be recognized without a green screen. Suitable for online education, video conferences, and other scenarios. |
| Illustration | ![]() | ![]() |
Feature Scenarios
Based on the object segmentation capability, developers can implement business scenarios such as background blurring, virtual backgrounds, presenter mode, and multi-user real-time interaction, creating more diverse interactive experiences.
| Feature | Background Blur | Virtual Background | Transparent Background | Subject Segmentation and Transmission |
|---|---|---|---|---|
| Feature Description | Blur the area outside the subject. | Replace the area outside the subject with custom images, videos, or colors. | Render the subject's image over other video content locally. For example, implement features such as presenter mode over screen sharing or playing video content. | Combined with the Alpha channel data transmission capability provided by Express SDK, the segmented subject is transmitted to the playing end and rendered at the playing end, achieving the visual effect of multiple people in different locations appearing in the same scene in real time |
| Illustration | ![]() | ![]() | ![]() | ![]() |
Hardware Compatibility
| Platform | Hardware Requirements |
|---|---|
| Android |
|
| iOS | A Series Chips: Apple A9 and above, e.g., iPhone 6s |
| macOS | M Series Chips: Apple M1 and above |
| Windows | Intel Core i5 and above |
Please note, when using Flutter SDK on the above platforms, hardware compatibility is as above.
Sample Source Code
Please refer to Download Sample Source Code to get the source code.
For related source code, please check files in the "lib/topics/OtherFunctions/screen_sharing" directory.
Prerequisites
Before using the object segmentation feature, ensure:
-
You have contacted ZEGOCLOUD Technical Support for a special build.
-
You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information.
-
You have integrated ZEGO Express SDK in your project and implemented basic audio and video publishing and playing features. For details, please refer to Quick Start - Integrating SDK and Quick Start - Implementing Video Call.
Implementation
- Enabling object segmentation will consume additional system resources. To ensure user experience, currently only one channel's publishing stream can have object segmentation enabled.
- If using third-party filters through custom preprocessing, ensure that the third-party filters support Alpha channel passthrough.
Please note, developers can choose whether to implement the (Optional) steps in the above diagram according to their business scenario needs. If implementation is needed, please refer to the specific instructions below.
Initialization and Room Login
For the specific process of initialization and room login, please refer to "Create Engine" and "Login Room" in the Implementing Video Call documentation.
Listen for Object Segmentation State Callback
Call the onVideoObjectSegmentationStateChanged interface to listen for object segmentation state callbacks.
The object segmentation state callback depends on enabling preview or publishing. That is, to listen to the onVideoObjectSegmentationStateChanged callback, you need to call preview startPreview or publishing startPublishingStream.
ZegoExpressEngine.onVideoObjectSegmentationStateChanged = (state, channel, errorCode) {
ZegoLog().addLog(
'Video object segmentation state changed. state: $state, errorCode: $errorCode, channel: $channel');
};Use Object Segmentation to Implement Different Business Features
If developers need to update the object segmentation type or background processing type, they need to modify the ZegoObjectSegmentationConfig configuration and call the enableVideoObjectSegmentation interface again to enable object segmentation to update the object segmentation effect. The update result will be notified to developers through the onVideoObjectSegmentationStateChanged callback.
Background Blur
Call the enableVideoObjectSegmentation interface to enable object segmentation and set the background processing type to "Blur".
ZegoObjectSegmentationConfig config = ZegoObjectSegmentationConfig.defaultConfig();
config.objectSegmentationType = ZegoObjectSegmentationType.AnyBackground;//Select the object segmentation type to enable based on actual situation
config.backgroundConfig.processType = ZegoBackgroundProcessType.Blur;//Set background processing method to blur
config.backgroundConfig.blurLevel = ZegoBackgroundBlurLevel.Medium;//Set background blur level to medium
engine.enableVideoObjectSegmentation(enable, config, ZegoPublishChannel.Main);//Enable object segmentationVirtual Background
Virtual background supports two types of materials:
- Images. Currently supports "PNG" and "JPEG" image formats, i.e., image files with ".png", ".jpg", and ".jpeg" extensions.
- Videos, with the following restrictions:
- Video format: MP4, FLV, MKV, AVI.
- Video source: Local video.
- Video playback method: Loop playback.
- Resolution: Maximum not exceeding 4096 px, recommended within 1920 px.
- Video duration: Maximum not exceeding 30 seconds, recommended within 15 seconds.
- Video size: Maximum not exceeding 50 MB, recommended within 10 MB.
When using this feature, developers should pay attention to the aspect ratio of custom images and video materials, otherwise parts exceeding the view will be cropped.
Call the enableVideoObjectSegmentation interface to enable object segmentation and set the background processing type to "Image" or "Video".
ZegoObjectSegmentationConfig config = ZegoObjectSegmentationConfig.defaultConfig();
config.objectSegmentationType = ZegoObjectSegmentationType.AnyBackground;//Select the object segmentation type to enable based on actual situation
//Set background processing method to Image
config.backgroundConfig.processType = ZegoBackgroundProcessType.Image;
config.backgroundConfig.imageURL = "<image_path>";//Set background image path
engine.enableVideoObjectSegmentation(enable, config, ZegoPublishChannel.Main);//Enable object segmentation
//Set background processing method to Video
config.backgroundConfig.processType = ZegoBackgroundProcessType.Video;
config.backgroundConfig.videoURL = "<video_path>";//Set background video path
engine.enableVideoObjectSegmentation(enable, config, ZegoPublishChannel.Main);//Enable object segmentationTransparent Background
If developers need to implement business functions similar to "presenter mode", they need to mix the "subject image" with "video source content to be mixed" into one video stream on the business side.
Call the enableVideoObjectSegmentation interface to enable object segmentation and set the background processing type to "Transparent".
ZegoObjectSegmentationConfig config = ZegoObjectSegmentationConfig.defaultConfig();
config.objectSegmentationType = ZegoObjectSegmentationType.AnyBackground;//Select the object segmentation type to enable based on actual situation
config.backgroundConfig.processType = ZegoBackgroundProcessType.Transparent;//Set background processing method to transparent
engine.enableVideoObjectSegmentation(enable, config, ZegoPublishChannel.Main);//Enable object segmentation(Optional) Use Alpha Channel to Transmit Segmented Subject
If the publishing end needs to transmit the segmented subject image to the playing end through the Alpha channel and render the subject at the playing end, you need to first call the enableAlphaChannelVideoEncoder interface to set the encoder to support the alpha channel, then call the startPublishingStream interface to publish the stream to transmit it smoothly to the playing end.
Currently only supports alpha channel data arranged below RGB or YUV data.
-
Enable Alpha channel data transmission:
ZegoAlphaLayoutType layoutType = ZegoAlphaLayoutType.Bottom; // Alpha channel data arranged below RGB or YUV data engine.enableAlphaChannelVideoEncoder(true, layoutType, ZegoPublishChannel.Main); // Enable encoder to support alpha channel -
Disable Alpha channel data transmission:
ZegoAlphaLayoutType layoutType = ZegoAlphaLayoutType.Bottom; // Alpha channel data arranged below RGB or YUV data engine.enableAlphaChannelVideoEncoder(false, layoutType, ZegoPublishChannel.Main); // Disable encoder to support alpha channel
Start Preview and Publishing
After enabling the object segmentation feature through the enableVideoObjectSegmentation interface, you can preview.
Developers can also enable preview first, then enable object segmentation. This document introduces enabling object segmentation first, then previewing.
ZegoCanvas canvas = ZegoCanvas(previewView);
canvas.alphaBlend = true;//Enable internal rendering Alpha blending. After enabling, it supports Alpha blending between the segmented subject and background layer
engine.startPreview(canvas);
engine.startPublishingStream(streamID);(Optional) Set Alpha Channel Rendering at Playing End and Start Playing
Alpha channel rendering needs to be enabled at the playing end only when the publishing end has enabled Alpha channel transmission.
ZegoCanvas canvas = ZegoCanvas(playView);
canvas.alphaBlend = true;//Enable internal rendering Alpha blending. After enabling, it supports Alpha blending between the segmented subject and background layer
engine.startPlayingStream(streamID, canvas);Disable Object Segmentation
Call the enableVideoObjectSegmentation interface to disable object segmentation.
ZegoObjectSegmentationType objectType = ZegoObjectSegmentationType.AnyBackground;//Select the object segmentation type to disable based on actual situation
engine.enableVideoObjectSegmentation(false, objectType, ZegoPublishChannel.Main);//Disable object segmentationDestroy Engine
Call the destroyEngine interface to destroy the engine.
ZegoExpressEngine.destroyEngine();





