logo
Live Streaming
On this page

Subject Segmentation

2024-09-06

Feature Introduction

Subject 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 in the subject portion are set to "opaque", while pixels outside the subject portion are set to "transparent". Developers can use the transparency information of these pixels to process the subject and non-subject parts differently, thereby implementing various functions.

Warning
  • The current official website SDK does not include "subject segmentation" related features. If needed, please contact ZEGOCLOUD Technical Support for special packaging and provide your AppID to enable relevant permissions.
  • The "subject segmentation" feature is a paid feature. For trial applications or formal pricing inquiries, please contact ZEGO business personnel.

Subject Segmentation Objects

For users in different environments, ZEGOCLOUD provides two segmentation capabilities: "green screen background segmentation" and "arbitrary background segmentation".

Segmentation TypeGreen Screen Background SegmentationArbitrary Background Segmentation
Capability Description

When users have set up a green screen themselves, the subject in non-green screen areas 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 ZEGOCLOUD's arbitrary background segmentation capability, the subject in the frame can be recognized without a green screen.

Suitable for online education, video conferences, and other scenarios.

Illustration

Feature Scenarios

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

Feature PointBackground BlurVirtual BackgroundBackground TransparencySubject Segmentation and Transmission
Feature DescriptionBlur the area outside the subject.Replace the area outside the subject with custom images, videos, or colors.

Render the subject's image on top of other video content locally.

For example, implement presenter mode and other functions on top of screen sharing or playing video content.

Combine with the Alpha channel data transmission capability provided by Express SDK to transmit the segmented subject to the playing end for subject rendering, achieving the visual effect of multiple people in different locations appearing in the same scene in real time
Illustration

Hardware Compatibility

PlatformHardware Requirements
Android
  • Snapdragon chips:

    • Snapdragon 6 series: Snapdragon 675 and above
    • Snapdragon 7 series: Snapdragon 730 and above
    • Snapdragon 8 series: Snapdragon 835 and above
  • HiSilicon Kirin chips:

    • Kirin 8 series: Kirin 820 and above
    • Kirin 9 series: Kirin 980 and above
  • MediaTek chips:

    • Helio P series: Helio P60 and above
    • Dimensity series: Dimensity 820 and above
  • Samsung chips: Exynos 1080 and above
iOSA series chips: Apple A9 and above, e.g., iPhone 6s
macOSM series chips: Apple M1 and above
WindowsIntel Core i5 and above

Sample Source Code

Please refer to Download Sample Source Code to get the source code.

For related source code, please check the files in the "/ZegoExpressExample/Examples/Others/VideoObjectSegmentation" directory.

Prerequisites

Before using the subject segmentation feature, please ensure:

Implementation Flow

Warning
  • Enabling subject segmentation will consume additional system resources. To ensure user experience, currently only one channel's publishing stream can enable subject segmentation.
  • If using third-party filters for custom pre-processing, please ensure the third-party filters support Alpha channel pass-through functionality.

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

1 Initialize and login room

For the specific process of initialization and login room, please refer to "Create engine" and "Login room" in the video call documentation.

2 Make special settings for view

Before enabling subject segmentation, you need to set the view used for rendering, i.e., call setBackgroundColor to set the backgroundColor property to clearColor.

@property (weak, nonatomic) IBOutlet UIView *view;
[self.view setBackgroundColor:UIColor.clearColor];

3 (Optional) Implement custom rendering class

Developers can perform custom rendering based on actual situations. Note that the raw video data contains an Alpha channel, and the raw data has not undergone Alpha premultiplication processing. Taking rendering captured data by drawing UIImage as an example:

Warning

Do not render directly in the callback, as it will block the callback thread and cause rendering abnormalities.

- (void)onCapturedVideoFrameRawData:(unsigned char * _Nonnull *)data dataLength:(unsigned int *)dataLength param:(ZegoVideoFrameParam *)param flipMode:(ZegoVideoFlipMode)flipMode channel:(ZegoPublishChannel)channel {
    if (param.format == ZegoVideoFrameFormatBGRA32) {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, data[0], dataLength[0], NULL);

        CGImageRef cgImageFromBytes = CGImageCreate((int)param.size.width, (int)param.size.height, 8, 32, param.strides[0], colorSpace, kCGImageByteOrder32Little|kCGImageAlphaFirst, dataProvider, NULL, NO, kCGRenderingIntentDefault);

        UIImage *image = [UIImage imageWithCGImage:cgImageFromBytes];

        // Draw image to rendering view
        dispatch_async(dispatch_get_main_queue(), ^{
            self.previewView.image = image;
            CGImageRelease(cgImageFromBytes);
        });
    }
}

4 (Optional) Enable custom rendering

Call the enableCustomVideoRender interface to set engine advanced configuration and enable custom rendering, and also call the setCustomVideoRenderHandler interface to set custom video rendering callback. For details, please refer to Custom Video Rendering.

Warning

When using custom rendering for subject segmentation, bufferType only supports the ZegoVideoBufferType.RAW_DATA type.

ZegoCustomVideoRenderConfig *renderConfig = [[ZegoCustomVideoRenderConfig alloc] init];
renderConfig.bufferType = ZegoVideoBufferTypeRawData;// Select RAW_DATA type video frame data
renderConfig.frameFormatSeries = ZegoVideoFrameFormatSeriesRGB;// Select RGB color series data format
[[ZegoExpressEngine sharedEngine] enableCustomVideoRender:YES config:renderConfig];// Start custom video rendering
[[ZegoExpressEngine sharedEngine] setCustomVideoRenderHandler:self];// Set custom video rendering callback

5 Listen for subject segmentation state callback

Call the onVideoObjectSegmentationStateChanged interface to listen for subject segmentation state callbacks.

Warning

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

-(void)onVideoObjectSegmentationStateChanged:(ZegoObjectSegmentationState)state channel:(ZegoPublishChannel)channel errorCode:(int)errorCode{
    if(state == ZegoObjectSegmentationStateOn){
        //Subject segmentation enabled
    }else{
        //Subject segmentation disabled
        //Abnormal shutdown, please check the error code
    }
}

6 Use subject segmentation to implement different business functions

Warning

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

Background Blur

Call the enableVideoObjectSegmentation interface to enable subject segmentation and set the background processing type to "blur".

ZegoObjectSegmentationConfig *config = [[ZegoObjectSegmentationConfig alloc]init];
config.objectSegmentationType = ZegoObjectSegmentationTypeAnyBackground;// Select the subject segmentation type to enable based on actual situation
config.backgroundConfig.processType = ZegoBackgroundProcessTypeBlur;// Set background processing method to blur
config.backgroundConfig.blurLevel = ZegoBackgroundBlurLevelMedium;// Set background blur level to medium
[[ZegoExpressEngine sharedEngine]enableVideoObjectSegmentation:YES config:config channel:ZegoPublishChannelMain];// Enable subject segmentation

Virtual 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.
Warning

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 subject segmentation and set the background processing type to "image" or "video".

ZegoObjectSegmentationConfig *config = [[ZegoObjectSegmentationConfig alloc]init];
config.objectSegmentationType = ZegoObjectSegmentationTypeAnyBackground;// Select the subject segmentation type to enable based on actual situation

//Set background processing method to image
config.backgroundConfig.processType = ZegoBackgroundProcessTypeImage;
config.backgroundConfig.imageUrl= @"<image_path>";// Set background image path
[[ZegoExpressEngine sharedEngine]enableVideoObjectSegmentation:YES config:config channel:ZegoPublishChannelMain];// Enable subject segmentation

//Set background processing method to video
config.backgroundConfig.processType = ZegoBackgroundProcessTypeVideo;
config.backgroundConfig.videoURL= @"<video_path>";// Set background video path
[[ZegoExpressEngine sharedEngine]enableVideoObjectSegmentation:YES config:config channel:ZegoPublishChannelMain];// Enable subject segmentation

Transparent Background

Warning

If developers need to implement business functions similar to "presenter mode", they need to mix the "subject image" with the "video source content to be mixed" into one video stream on the business side.

Call the enableVideoObjectSegmentation interface to enable subject segmentation and set the background processing type to "transparent".

ZegoObjectSegmentationConfig *config = [[ZegoObjectSegmentationConfig alloc]init];
config.objectSegmentationType = ZegoObjectSegmentationTypeAnyBackground;// Select the subject segmentation type to enable based on actual situation
config.backgroundConfig.processType = ZegoBackgroundProcessTypeTransparent;// Set background processing method to transparent
[[ZegoExpressEngine sharedEngine]enableVideoObjectSegmentation:YES config:config channel:ZegoPublishChannelMain];// Enable subject segmentation

7 (Optional) Use Alpha channel to transmit the segmented subject

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

Warning

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

  • Enable Alpha channel data transmission:

    ZegoAlphaLayoutType layoutType = ZegoAlphaLayoutTypeBottom; // Transparency channel data arranged below RGB or YUV data
    [[ZegoExpressEngine sharedEngine]enableAlphaChannelVideoEncoder:YES alphaLayout:layoutType channel:ZegoPublishChannelMain]; // Set encoder to support transparency channel
  • Disable Alpha channel data transmission:

    ZegoAlphaLayoutType layoutType = ZegoAlphaLayoutTypeBottom; // Transparency channel data arranged below RGB or YUV data
    [[ZegoExpressEngine sharedEngine]enableAlphaChannelVideoEncoder:NO alphaLayout:layoutType channel:ZegoPublishChannelMain]; // Set encoder to support transparency channel

8 Start preview and publish stream

After enabling the subject segmentation feature through the enableVideoObjectSegmentation interface, you can preview.

Note

Developers can also enable preview first, then enable subject segmentation. This article introduces enabling subject segmentation first, then previewing.

  • Method 1: Use internal rendering

    If using internal rendering, before enabling preview, please set alphaBlend to YES.

    ZegoCanvas *previewCanvas = [ZegoCanvas canvasWithView:self.previewView];
    previewCanvas.alphaBlend = YES;// Enable internal rendering Alpha blending, after enabling supports Alpha blending of the segmented subject with background layer
    [[ZegoExpressEngine sharedEngine]startPreview:previewCanvas];
    [[ZegoExpressEngine sharedEngine] startPublishingStream:streamID];
  • Method 2: Use custom rendering

    If using custom rendering, you can directly preview and publish stream.

    [[ZegoExpressEngine sharedEngine] startPreview:nil];
    [[ZegoExpressEngine sharedEngine] startPublishingStream:streamID];

9 (Optional) Set Alpha channel rendering at the playing end and play stream

Warning

Only when the publishing end has enabled Alpha channel transmission does the playing end need to enable Alpha channel rendering.

  • Method 1: Use internal rendering

    If using internal rendering, before calling the startPlayingStream interface to start playing stream at the playing end, you need to set the alphaBlend property of ZegoCanvas to YES.

    ZegoCanvas *playCanvas = [ZegoCanvas canvasWithView:self.playView1];
    playCanvas.alphaBlend = YES;// Enable internal rendering Alpha blending, after enabling supports Alpha blending of the segmented subject with background layer
    [[ZegoExpressEngine sharedEngine]startPlayingStream:streamID  canvas:playCanvas];
  • Method 2: Use custom rendering

    If using custom rendering, you can directly play stream.

    [[ZegoExpressEngine sharedEngine]startPlayingStream:streamID canvas:nil];

10 Disable subject segmentation

Call the enableVideoObjectSegmentation interface to disable subject segmentation.

ZegoObjectSegmentationType objectType = ZegoObjectSegmentationTypeAnyBackground;// Select the subject segmentation type to disable based on actual situation
[[ZegoExpressEngine sharedEngine]enableVideoObjectSegmentation:NO type:objectType channel:ZegoPublishChannelMain];// Disable subject segmentation

11 Destroy engine

Call the destroyEngine interface to destroy the engine.

[ZegoExpressEngine destroyEngine:nil];

Previous

Super Resolution

Next

H.265