- Introduction
- Class Diagram
- Camera Capturing
- Camera Preview Rendering
- Applying Virtual Background
- Demo
This project leverages Apple's Vision person segmentation to replace the background in real-time on iOS devices. Using an on-device deep learning model, it accurately detects and segments the human figure, allowing users to apply custom virtual backgrounds. The soft segmentation mask blends the person into the background with natural-looking edges. Optimized for performance, it ensures smooth processing on mobile devices.
classDiagram
class CameraController {
+SessionSetupResult
-devicePosition: AVCaptureDevice.Position
-captureSession: AVCaptureSession
-sessionQueue: DispatchQueue
-dataOutputQueue: DispatchQueue
-cameraVideoDataOutput: AVCaptureVideoDataOutput
-rotatedPixelBufferPool: CVPixelBufferPool?
-cameraDeviceInput: AVCaptureDeviceInput?
-cameraProcessor: CameraProcessor?
-setupResult: SessionSetupResult
-isConfigured: Bool
-isSessionRunning: Bool
-isRenderingEnabled: Bool
-lastFpsTimestamp: TimeInterval
-frameCount: Int
+fpsDelegate: FpsDelegate?
+cameraPreview: CameraPreview?
+init(cameraProcessor: CameraProcessor?)
+configure()
+startRunning(completion: (SessionSetupResult) -> Void)
+stopRunning()
+restartSession()
+enableRendering()
+disableRendering()
+applyBackgroundImage(image: CGImage)
}
class PHPickerViewControllerDelegate {
<<protocol>>
+picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
}
class FpsDelegate {
<<protocol>>
+didUpdateFps(fps: Double)
}
class CameraPreview {
<<class>>
+pixelBuffer: CVPixelBuffer?
}
class CameraProcessor {
<<protocol>>
+process(framePixelBuffer: CVPixelBuffer) CVPixelBuffer?
+applyBackgroundImage(image: CGImage)
}
class CameraVirtualBackgroundProcessor {
-backgroundTextureLock: NSLock
-backgroundTexture: MTLTexture?
-outputPixelBufferPool: CVPixelBufferPool
-textureCache: CVMetalTextureCache
-commandQueue: MTLCommandQueue
-computePipelineState: MTLComputePipelineState
-device: MTLDevice
-segmentationRequest: VNGeneratePersonSegmentationRequest
-sequenceRequestHandler: VNSequenceRequestHandler
-videoSize: CGSize
-textureLoader: MTKTextureLoader
+init?()
+process(framePixelBuffer: CVPixelBuffer) CVPixelBuffer?
+applyBackgroundImage(image: CGImage)
-makeSegmentationRequest() VNGeneratePersonSegmentationRequest
-warmUpSegmentation()
-render(pixelBuffer: CVPixelBuffer, maskPixelBuffer: CVPixelBuffer, backgroundTexture: MTLTexture, into: CVPixelBuffer) Bool
-loadTexture(image: CGImage) MTLTexture?
-makeMetalTexture(from: CVPixelBuffer, pixelFormat: MTLPixelFormat, usage: MTLTextureUsage) CVMetalTexture?
}
class CameraViewController {
-cameraController: CameraController!
-selectedAssetIdentifier: String?
-cameraPreview: CameraPreview
-imagePickerButton: UIButton
-fpsLabel: UILabel
-cameraUnavailableLabel: UILabel
+viewDidLoad()
+viewWillAppear(animated: Bool)
+viewWillDisappear(animated: Bool)
+initialize()
+configureView()
+requestCameraPermission(completion: @escaping (Bool) -> Void)
+showCameraDisabledAlert()
+presentPicker(filter: PHPickerFilter?)
+didEnterBackground(notification: NSNotification)
+willEnterForeground(notification: NSNotification)
+sessionWasInterrupted(notification: NSNotification)
+sessionInterruptionEnded(notification: NSNotification)
+sessionRuntimeError(notification: NSNotification)
+presentPickerForImages(_ sender: Any)
+addObservers()
+removeObservers()
+didUpdateFps(fps: Double)
+picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
}
CameraController --> FpsDelegate
CameraController --> CameraPreview
CameraController *-- CameraProcessor
CameraVirtualBackgroundProcessor ..|> CameraProcessor
CameraViewController ..|> PHPickerViewControllerDelegate
CameraViewController ..|> FpsDelegate
CameraViewController *-- CameraController
CameraViewController *-- CameraPreview
Camera capturing for iOS utilizes the AVFoundation framework to manage and configure the camera hardware, capture video and audio data, and process the captured data. The CameraController class is responsible for setting up capture session (AVCaptureSession) it's the core component that manages the flow of data from the input devices (camera and microphone) to the outputs (video and audio data).
In this implementation, the controller focuses solely on video capture. It starts with setting up the capture session on a background queue (sessionQueue), configures the session preset, adds the camera input, and sets up the video data output. Configuration happens only once, even if the view appears multiple times. The startRunning and stopRunning methods start and stop the capture session on the sessionQueue. Because startRunning runs on the same queue as configuration, it always reports the final setup result, which is delivered to its completion handler on the main queue.
If an iOS version ignores the requested portrait rotation and delivers landscape frames, the controller rotates them to portrait with vImage, using a reusable pixel buffer pool, before passing them on.
Each time a new video frame is captured, the captureOutput method is triggered, processing the frame and updating the frames per second (FPS) in real time.
The CameraPreview class is a custom MetalKit view that renders video frames captured by the camera. It manages the Metal device, command queue, render pipeline state, and texture cache. The class handles the creation of Metal textures from pixel buffers, sets up the necessary transformations for rendering, and encodes the rendering commands. The draw method is responsible for rendering the video frames on the screen, and the setupTransform method scales the frame to fill the view while preserving its aspect ratio, recalculating whenever the texture dimensions or view bounds change.
The PassThrough.metal file contains the vertex and fragment shaders used by the CameraPreview class to render video frames. The vertex shader processes the vertex positions and texture coordinates, while the fragment shader samples the texture to determine the color of each pixel. The CameraPreview class sets up the render pipeline state with these shaders, prepares the vertex and texture coordinate buffers, and issues draw commands to render the video frames using Metal. This setup allows for efficient and high-performance rendering of video frames captured by the camera.
The CameraVirtualBackgroundProcessor class is responsible for processing video frames to apply a virtual background using Vision and Metal. It implements the CameraProcessor protocol, which defines methods for processing video frames and applying a background image. The implementation uses Vision's VNGeneratePersonSegmentationRequest to separate the foreground (a person) from the background in video frames. Vision runs Apple's on-device person segmentation model and returns a soft mask, which a compute shader uses to blend the camera frame with a virtual background image. Frames, masks, and output buffers are shared with the GPU without copying, making it suitable for applications such as virtual backgrounds in video conferencing or live streaming.
Here’s how it works:
-
Initialization: This initializer sets up a Metal-based image processing pipeline. It creates a
Metaldevice and command queue, loads themixercompute shader from theMixer.metalfile, and sets up a compute pipeline. A texture cache is initialized to wrap pixel buffers asMetaltextures without copying their memory, and a pixel buffer pool is created so output frames reuse memory instead of being allocated on every frame. Finally, it warms up person segmentation on a background queue by running it once on a blank frame, so Vision loads its model before the first real frame without delaying app launch. -
Configuring Person Segmentation: The segmentation request uses the
.balancedquality level, which Apple recommends for live video (.accurateis intended for still images and is too slow for real-time use), and outputs a single-channel 8-bit mask (kCVPixelFormatType_OneComponent8). Unlike a general semantic segmentation model, which assigns every pixel one of many class labels, Vision's person segmentation is specialized for people and produces a matte: each pixel holds a value from0(background) to1(person), with intermediate values along soft edges such as hair. The same request is reused for every frame and performed through aVNSequenceRequestHandler, which keeps state between frames and produces a more temporally stable mask than one-off requests. -
Generating the Segmentation Mask: The
processmethod checks ifbackgroundTextureis available. If it isn't, it returnsframePixelBuffer, which represents the frame captured by the camera, unchanged. Otherwise, it performs the segmentation request directly on the camera frame (Vision handles any scaling internally) and reads the resulting mask from the request's results as aCVPixelBuffer. -
Rendering the Frame: The
rendermethod wraps the camera frame, the segmentation mask, and an output pixel buffer taken from the pool asMetaltextures, dispatches themixercompute kernel, and waits for the GPU to finish. The background texture is guarded by a lock, because it's set from the main thread when the user picks an image while frames are processed on the camera's data output queue. -
Blending the Frame: The compute shader in
Mixer.metalsamples the mask at each pixel's center to get the person coverage. Because the mask is lower resolution than the frame, it's sampled with linear filtering, which keeps the edges soft instead of blocky. The camera pixel and the background are then blended by that coverage, so pixels fully covered by a person keep the camera image, background pixels show the virtual background, and pixels along the edge, such as hair, mix the two. -
Displaying the Frame: Because the shader writes directly into the output pixel buffer, no copy is needed afterwards. The
processmethod returns that pixel buffer for displaying on the screen using theCameraPreviewclass, the process of which was explained in the Camera Preview Rendering section.
Note that Vision person segmentation requires a physical device; it doesn't work properly in the iOS Simulator.
