借助 Scene Semantics API,开发者可以通过提供基于机器学习模型的实时语义信息来了解用户周围的场景。给定一张户外场景图片,该 API 会针对一组实用的语义类别(例如天空、建筑物、树木、道路、人行道、车辆、人等)为每个像素返回一个标签。除了像素标签之外,Scene Semantics API 还会为每个像素标签提供置信度值,并提供一种简单易用的方式来查询给定标签在户外场景中的普遍性。
if(semanticManager.TryGetSemanticConfidenceTexture(outTexture2DsemanticConfidenceImage)){using(semanticConfidenceImage){// Use the semantic confidence image here.}}
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Scene Semantics API provides real-time understanding of a user's surroundings by assigning semantic labels, like "building" or "tree", to each pixel of an outdoor scene.\u003c/p\u003e\n"],["\u003cp\u003eIt offers confidence values for each pixel label to indicate the certainty of the assigned label.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can efficiently query the prevalence of a specific semantic label, like "sky", within the scene.\u003c/p\u003e\n"],["\u003cp\u003eBefore using, ensure your device supports the Scene Semantics API and enable it in your ARCore session settings.\u003c/p\u003e\n"],["\u003cp\u003eAccess the semantic information through the \u003ccode\u003eArSemanticManager\u003c/code\u003e by retrieving the semantic image, the confidence image, or querying label fractions.\u003c/p\u003e\n"]]],["The Scene Semantics API provides real-time semantic understanding of outdoor scenes through pixel labeling. To use it, first, enable \"Semantics Mode\" in ARCore configurations. Then, retrieve the semantic image using `TryGetSemanticTexture()`, which assigns a label to each pixel. Acquire the corresponding confidence image with `TryGetSemanticConfidenceTexture()` for pixel confidence values. Finally, query the fraction of a specific label within the scene using `GetSemanticLabelFraction()`. The API requires 1-3 frames to start outputting images.\n"],null,["# Understand the user's environment on Unity's AR Foundation\n\nLearn how to use the [Scene Semantics API](/ar/develop/scene-semantics) in your own apps.\n\nThe Scene Semantics API enables developers to understand the scene surrounding the user, by providing ML model-based, real-time semantic information. Given an image of an outdoor scene, the API returns a label for each pixel across a set of useful semantic classes, such a sky, building, tree, road, sidewalk, vehicle, person, and more. In addition to pixel labels, the Scene Semantics API also offers confidence values for each pixel label and an easy-to-use way to query the prevalence of a given label in an outdoor scene.\n\nYour browser does not support the video tag.\n\nFrom left to right, examples of an input image, the semantic image of pixel labels, and the corresponding confidence image:\n\nPrerequisites\n-------------\n\nMake sure that you understand [fundamental AR concepts](/ar/develop/fundamentals)\nand how to [configure an ARCore session](/ar/develop/unity-arf/session-config) before proceeding.\n\nEnable Scene Semantics\n----------------------\n\nIn [a new ARCore session](/ar/develop/unity-arf/session-config), check whether a user's device supports the Scene Semantics API. Not all ARCore-compatible devices support the Scene Semantics API due to processing power constraints.\n\nTo save resources, Scene Semantics is disabled by default on ARCore. Enable semantic mode to have your app use the Scene Semantics API.\n\nIn your **ARCoreExtensionsConfig**, set the Semantics Mode to Enabled.\n\nIf using iOS, Semantics must also be enabled in the project settings:\n\n1. Navigate to **Edit** \\\u003e **Project Settings** \\\u003e **XR Plug-In Management** \\\u003e **ARCore Extensions**.\n2. Under **Optional Features** , select **Semantics on iOS**.\n\nObtain the semantic image\n-------------------------\n\nOnce Scene Semantics is enabled, the semantic image can be retrieved. The semantic image is a [`TextureFormat.R8`](https://docs.unity3d.com/ScriptReference/TextureFormat.R8.html) image, where each pixel corresponds to a semantic label defined by [`SemanticLabel`](/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#semanticlabel).\n\nUse [`ArSemanticManager.TryGetSemanticTexture()`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARSemanticManager#trygetsemantictexture) to acquire the semantic image: \n\n if (semanticManager.TryGetSemanticTexture(out Texture2D semanticImage))\n {\n using (semanticImage)\n {\n // Use the semantic image here.\n }\n }\n\nOutput semantic images should be available after about 1-3 frames from the start of the session, depending on the device.\n\nObtain the confidence image\n---------------------------\n\nIn addition to the semantic image, which provides a label for each pixel, the API also provides a confidence image of corresponding pixel confidence values. The confidence image is a [`TextureFormat.Alpha8`](https://docs.unity3d.com/ScriptReference/TextureFormat.Alpha8.html) image, where each pixel corresponds to a value in the range `[0, 255]`, corresponding to the probability associated with the semantic label for each pixel.\n\nUse [`ArSemanticManager.TryGetSemanticConfidenceTexture()`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARSemanticManager#trygetsemanticconfidencetexture) to acquire the semantic confidence image: \n\n if (semanticManager.TryGetSemanticConfidenceTexture(out Texture2D semanticConfidenceImage))\n {\n using (semanticConfidenceImage)\n {\n // Use the semantic confidence image here.\n }\n }\n\nOutput confidence images should be available after about 1-3 frames from the start of the session, depending on the device.\n\nQuery the fraction of pixels for a semantic label\n-------------------------------------------------\n\nYou can also query the fraction of pixels in the current frame that belong to a particular class, such as sky. This query is more efficient than returning the semantic image and performing a pixel-wise search for a specific label. The returned fraction is a float value in the range `[0.0, 1.0]`.\n\nUse [`ArSemanticManager.GetSemanticLabelFraction()`](/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARSemanticManager#getsemanticlabelfraction) to acquire the fraction for a given label: \n\n var fraction = semanticManager.GetSemanticLabelFraction(SemanticLabel.SKY);"]]