# Adding Scene Segmentation Source: https://www.nianticspatial.com/docs/nsdk/how-to/ar/adding_semantics/?platform=swift Niantic Spatial SDK (NSDK) exposes its scene segmentation functionality on native iOS through the **`NSDKSceneSegmentationSession`** API. Each time `NSDKSession.update()` is called, an active `NSDKSceneSegmentationSession` produces buffers containing semantic channel and confidence levels. ## Read Semantics Data Use the `NSDKSceneSegmentationSession` publishers to read semantics data reactively: - **`$confidenceResult`** - A `@Published` property that emits the confidence map for the active `confidenceChannel` each frame. Set `confidenceChannel` to the desired `SceneSegmentationChannels` value before subscribing. For a list of supported channels in NSDK, see the [scene segmentation feature page](https://www.nianticspatial.com/docs/nsdk/features/semantics/). - **`$packedChannels`** - A `@Published` property that emits a multi-channel image where each pixel contains which semantic channels are detected. Packed channels provide an efficient way to access multiple semantic classifications in a single image. --- ## Creating and Configuring a Scene Segmentation Session ```swift let nsdk = NSDKSession() // Enable scene segmentation functionality for this session let sceneSegmentationSession = nsdk.acquireSceneSegmentationSession() // Configure and start scene segmentation // The default configuration sets the frequency of inference to 10 fps let config = NSDKSceneSegmentationSession.Configuration() do { try sceneSegmentationSession.configure(with: config) sceneSegmentationSession.start() } catch { print("Failed to configure the scene segmentation session (error: \(error)") } ``` Scene segmentation takes some time to start up while downloading and decrypting the model file (see [model preloading](https://www.nianticspatial.com/docs/nsdk/features/model_preloading_semantics/) to speed this up). Once the feature is started, set the desired channel and subscribe to `$confidenceResult` to receive confidence data reactively: ```swift // Set the channel to observe sceneSegmentationSession.confidenceChannel = .ground sceneSegmentationSession.$confidenceResult .compactMap { state -> SceneSegmentationResult? in if case .success(let result) = state { return result } else { return nil } } .receive(on: DispatchQueue.main) .sink { result in // Process the latest confidence data for 'ground' // result.image, result.frameId, result.intrinsics, etc. } .store(in: &cancellables) ``` ## More Information Other NSDK AR features benefit from using scene segmentation data as a filter. For example, - [Occlusion suppression](https://www.nianticspatial.com/docs/nsdk/how-to/ar/adding_occlusion/) to selectively occlude portions of the camera view. - [Mesh filtering](https://www.nianticspatial.com/docs/nsdk/how-to/ar/meshing/semantic_mesh_filtering/) to include and/or exclude specific semantic channels from the mesh. For a full walkthrough of how to add scene segmentation to your project, see [How to Query Scene Segmentation and Highlight Semantic Channels](https://www.nianticspatial.com/docs/nsdk/how-to/ar/query_semantics_real_objects/).