> For the complete documentation index, see [llms.txt](https://modelplace.gitbook.io/mobile/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://modelplace.gitbook.io/mobile/api/background-removal-for-video-conferences.md).

# Background Removal for Video Conferences

**Define** selfie segmentation model variable

```swift
private var selfieSegmentationModel: SelfieSegmentationModel?
```

**Configure and Instantiate** the model with **`SelfieSegmentationModelBuilder`**

```objectivec
@interface SelfieSegmentationModelBuilder : NSObject

- (instancetype _Nonnull)init;

/**
 * \brief Creates a new instance of \a SelfieSegmentationModel.
 *
 * \param error Object containing error information if model instantiation fails.
 *
 * \returns Pointer to the new instance of \a SelfieSegmentationModel if instantiation
 * is successful, \a nil otherwise.
 *
 * \note Model instantiation is a blocking call which can take some time, therefore
 * this should be done on a separate serial dispatch queue.
 * That won't block the main queue which keeps the UI responsive.
 */
- (SelfieSegmentationModel* _Nullable)build:(NSError* _Nullable* _Nonnull)error;

@end
```

*Example:*

<pre class="language-swift"><code class="lang-swift"><strong>do {
</strong>    self.selfieSegmentationModel = try SelfieSegmentationModelBuilder()
        .build()
} catch {
    fatalError(
        "Failed to instantiate selfie segmentation model: \(error.localizedDescription)"
    )
}
</code></pre>

{% hint style="info" %}
*Model instantiation is a blocking call that can take some time, therefore this should be done on a separate serial dispatch queue. That won't block the main queue which keeps the UI responsive.*
{% endhint %}

**Schedule the task** with **`SelfieSegmentationModel.segment`** method when the model is instantiated

```swift
func captureOutput(_ output: AVCaptureOutput,
                   didOutput sampleBuffer: CMSampleBuffer,
                   from connection: AVCaptureConnection) {
    guard let imageBuffer = sampleBuffer.imageBuffer else {
        return
    }

    do {
        try selfieSegmentationModel!.segment(on: imageBuffer,
                                             at: sampleBuffer.outputPresentationTimeStamp)
    } catch {
        NSLog("Failed to submit selfie segmentation task: \(error.localizedDescription)")
    }
}
```

**`SelfieSegmentationModel`** returns its results through the **`SelfieSegmentationDelegate`**

```objectivec
@protocol SelfieSegmentationDelegate <NSObject>

/**
 * \brief Callback triggered whenever the \a SelfieSegmentationModel completes the
 * processing of the passed frame.
 *
 * \param model The \a SelfieSegmentationModel that acquired provided mask
 * \param mask The MaskData instance with representation of mask values
 */
@optional
- (void)selfieSegmentationModel:(SelfieSegmentationModel* _Nonnull)model
       didOutputMask:(MaskData* _Nonnull)mask;

@end

```

*Example:*

```swift
func selfieSegmentationModel(_ model: SelfieSegmentationModel,
                             didOutputMask mask: MaskData) {
    cameraPreviewView.draw(frame: lastCapturedFrame, mask: mask)
}
```

Each **`MaskData`** instance is represented with the following class

```objectivec
@interface MaskData : NSObject

 /**
 * \brief Width of the mask.
 */
@property (readonly, nonatomic) int width;

 /**
 * \brief Height of the mask.
 */
@property (readonly, nonatomic) int height;

 /**
 * \brief Creates a non-owning NSData view to the raw mask bytes.
 *
 * \returns Pointer to the instance of \a NSData, which represents the
 * raw binary mask values.
 */
- (NSData* _Nonnull) getDataView;

 /**
 * \brief Converts the mask to UIImage format.
 *
 * \returns Pointer to the instance of \a UIImage, where the foreground has zero values
 * and background has non-zero alpha values.
 *
 * \note The mask should be overlaid on the image. The background will be replaced
 * by black color in the final image.
 */
- (UIImage*) asUIImage;

 /**
 * \brief Applies blur filter to the image background using the mask.
 *
 * \param image The image in UIImage format, for which the mask was created.
 *
 * \returns Pointer to the instance of \a UIImage containing the submitted image
 * with the blurred background.
 */
- (UIImage*) applyBlur:(UIImage*)image;

@end
```
