透過場景語意 API,開發人員可以提供機器學習模型的即時語意資訊,瞭解使用者周遭的場景。當您提供戶外場景的圖片時,API 會針對每個像素傳回一組實用的語意類別標籤,例如天空、建築物、樹木、道路、人行道、車輛、人物等。除了像素標籤之外,場景語意 API 還會為每個像素標籤提供信心值,並提供簡單易用的查詢方式,可查詢特定標籤在戶外場景中的盛行率。
if(semanticManager.TryGetSemanticConfidenceTexture(outTexture2DsemanticConfidenceImage)){using(semanticConfidenceImage){// Use the semantic confidence image here.}}
[null,null,["上次更新時間: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);"]]