logo
On this page

Object Segmentation

2024-09-06

Feature Introduction

Object segmentation is a value-added capability provided by Express SDK. It uses AI algorithms to recognize content in video images and sets transparency information for each pixel. Among them, the pixels of the main subject will be set to "opaque", and the pixels outside the main subject will be set to "transparent". Developers can use the transparency information of these pixels to process the main subject and non-main subject parts in the image differently, thereby realizing different functions.

Warning
  • The current official website SDK does not include "Object Segmentation" related functions. If necessary, please contact ZEGOCLOUD Technical Support for special packaging and provide your AppID to enable relevant permissions.
  • The "Object Segmentation" function is a paid function. If you need to apply for experience or consult formal charging standards, please contact ZEGO business personnel.

Object Segmentation Objects

For users in different environments, ZEGO provides two segmentation capabilities: "Green Screen Background Segmentation" and "Arbitrary Background Segmentation".

Segmentation TypeGreen Screen Background SegmentationArbitrary Background Segmentation
Capability Description

When users set up a green screen by themselves, the main 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 the arbitrary background segmentation capability provided by ZEGO, the main subject in the image can be recognized without a green screen.

Suitable for online education, video conferences and other scenarios.

Illustration

Functional Scenarios

Based on the object segmentation capability, developers can implement business scenarios such as background blurring, virtual background, presentation mode, and multi-person real-time co-location interaction, creating more diverse interactive experiences.

Function PointBackground BlurringVirtual BackgroundBackground TransparencyObject Segmentation and Transmission
Function DescriptionBlur the image outside the main subject.Replace the image outside the main subject with custom images and colors.

Render the image of the main subject on other video content locally.

For example, on content such as screen sharing or currently playing videos, implement functions such as presentation mode.

Combine with the Alpha channel data transmission capability provided by Express SDK to transmit the segmented main subject in the image to the playing end, and render the main subject at the playing end to achieve the visual effect of multiple people in different places appearing in the same scene in real time
Illustration

Hardware Compatibility

PlatformHardware Requirements
macOSM series chips: Apple M1 and above
WindowsIntel Core i5 and above

Example Source Code

Please refer to Download Example Source Code to obtain the source code.

For related source code, please check the files in the "/ZegoExpressExample/Others/src/main/java/com/example/others/videoobjectsegmentation" directory.

Prerequisites

Before using the object segmentation function, please ensure:

Implementation Process

Warning
  • Enabling the object segmentation function will consume additional system resources. To ensure user experience, currently only supports enabling object segmentation for one channel of publishing stream image.
  • If there are custom pre-processed third-party filters, you need to ensure that the third-party filters support Alpha channel passthrough functionality.

Please note that developers can choose whether to implement the (Optional) steps in the above figure according to their business scenario needs. If implementation is needed, please refer to the specific instructions below.

Initialize and Login to Room

For the specific process of initialization and logging in to the room, please refer to "Create Engine" and "Login Room" in the Implement Video Call document.

Listen to Object Segmentation State Callback

Call the onVideoObjectSegmentationStateChanged interface to listen to the object segmentation state callback.

Warning

The object segmentation state callback depends on enabling preview or publishing stream. That is, to listen to the onVideoObjectSegmentationStateChanged callback, you need to call preview startPreview or publish stream startPublishingStream.

zgEngine.on('onVideoObjectSegmentationStateChanged', (res) =>{
   console.log('onVideoObjectSegmentationStateChanged: '+ res.state)
   if(res.state == zgDefines.ZegoObjectSegmentationState.On)
   {
       //Object segmentation enabled
   }
   else
   {
      //Object segmentation disabled
      //Abnormal shutdown, please check the error code
   }
})

Use Object Segmentation to Implement Different Business Functions

Warning

If developers need to update the object segmentation type or background processing type, they need to modify the configuration of ZegoObjectSegmentationConfig and call the enableVideoObjectSegmentation interface again to enable object segmentation, which can update the object segmentation effect; the update result will be notified to developers through the onVideoObjectSegmentationStateChanged callback.

Background Blurring

Call the enableVideoObjectSegmentation interface to enable object segmentation and set the background processing type to "Blur".

// Set background processing method to blur
// Set background blur level to medium
var config = {objectSegmentationType: zgDefines.ZegoObjectSegmentationType.AnyBackground, backgroundConfig: {processType: zgDefines.ZegoBackgroundProcessType.Blur, blurLevel: zgDefines.ZegoBackgroundBlurLevel.Medium}};
//Enable object segmentation
zgEngine.enableVideoObjectSegmentation(true, config, zgDefines.ZegoPublishChannel.Main)

Virtual Background

Warning
  • When using this function, developers should pay attention to the aspect ratio of custom images, otherwise parts of the image exceeding the view will be cropped.
  • Currently supports two image formats: "PNG" and "JPEG", that is, image files with three suffixes: ".png", ".jpg", and ".jpeg".

Call the enableVideoObjectSegmentation interface to enable object segmentation and set the background processing type to "Image".

// Set background processing method to image
var config = {objectSegmentationType: zgDefines.ZegoObjectSegmentationType.AnyBackground, backgroundConfig: {processType: zgDefines.ZegoBackgroundProcessType.Image, imageURL: "Set background image path"}};
//Enable object segmentation
zgEngine.enableVideoObjectSegmentation(true, config, zgDefines.ZegoPublishChannel.Main)

Transparent Background

Warning

If developers need to implement business functions similar to "presentation mode", they need to mix the "main subject image" and "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".

// Set background processing method to transparent
var config = {objectSegmentationType: zgDefines.ZegoObjectSegmentationType.AnyBackground, backgroundConfig: {processType: zgDefines.ZegoBackgroundProcessType.Transparent}};
//Enable object segmentation
zgEngine.enableVideoObjectSegmentation(true, config, zgDefines.ZegoPublishChannel.Main)

(Optional) Use Alpha Channel to Transmit Segmented Main Subject

If the publishing end needs to transmit the segmented main subject image to the playing end through the Alpha channel and render the main subject at the playing end, you need to first call the enableAlphaChannelVideoEncoder interface to set the encoder to support transparent channels, and then call the startPublishingStream interface to publish the stream, so that it can be smoothly transmitted to the playing end.

Warning

Currently only supports transparent channel data arranged below RGB or YUV data.

  • Enable Alpha channel data transmission:

    // Transparent channel data arranged below RGB or YUV data
    // Set encoder to support transparent channels
    zgEngine.enableAlphaChannelVideoEncoder(true, zgDefines.ZegoAlphaLayoutType.Bottom, zgDefines.ZegoPublishChannel.Main)
  • Disable Alpha channel data transmission:

    // Transparent channel data arranged below RGB or YUV data
    // Set encoder to support transparent channels
    zgEngine.enableAlphaChannelVideoEncoder(false, zgDefines.ZegoAlphaLayoutType.Bottom, zgDefines.ZegoPublishChannel.Main)

Start Preview and Publishing Stream

After enabling the object segmentation function through the enableVideoObjectSegmentation interface, you can preview.

Note

Developers can also enable preview first and then enable object segmentation. This article takes enabling object segmentation first and then previewing as an example.

zgEngine.startPreview();
zgEngine.startPublishingStream(streamID);

Play Stream

Start playing stream through the startPlayingStream interface.

zgEngine.startPlayingStream(StreamID, {
    canvas: localCanvas
});

Disable Object Segmentation

Call the enableVideoObjectSegmentation interface to disable object segmentation.

// Disable object segmentation
zgEngine.enableVideoObjectSegmentation(false, {}, zgDefines.ZegoPublishChannel.Main)

Destroy Engine

Call the destroyEngine interface to destroy the engine.

zgEngine.destroyEngine();

Previous

Screen Sharing

Next

Multi-Stream Mixing