透過場景語意 API,開發人員可以提供機器學習模型的即時語意資訊,瞭解使用者周遭的場景。當您提供戶外場景的圖片時,API 會針對每個像素傳回一組實用的語意類別標籤,例如天空、建築物、樹木、道路、人行道、車輛、人物等。除了像素標籤之外,場景語意 API 還會為每個像素標籤提供信心值,並提供簡單易用的查詢方式,可查詢特定標籤在戶外場景中的盛行率。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003eThe Scene Semantics API provides real-time semantic information about an outdoor scene, labeling each pixel with categories like sky, building, or person.\u003c/p\u003e\n"],["\u003cp\u003eTo use the API, enable semantic mode in your ARCore session after checking for device compatibility.\u003c/p\u003e\n"],["\u003cp\u003eYou can access the semantic image, confidence image, and the fraction of pixels for a specific semantic label using the API's methods.\u003c/p\u003e\n"],["\u003cp\u003eThe semantic and confidence images are available after a few frames from the session's start and may not be available for all devices due to processing limitations.\u003c/p\u003e\n"],["\u003cp\u003eThe API offers an efficient way to understand the scene's composition, enabling developers to build immersive and interactive AR experiences.\u003c/p\u003e\n"]]],["The Scene Semantics API provides real-time semantic information for outdoor scenes. Developers can enable the API by checking device support and setting the semantic mode in an ARCore session. Once enabled, the semantic image, a pixel-by-pixel label map, and a confidence image, providing corresponding confidence values, can be acquired using `acquireSemanticImage()` and `acquireSemanticConfidenceImage()`. The fraction of pixels belonging to a specific label can be queried using `getSemanticLabelFraction()`. These outputs are typically available after 1-3 frames.\n"],null,["# Understand the user's environment on Android SDK (Kotlin/Java)\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/java/session-config) before proceeding.\n\nEnable Scene Semantics\n----------------------\n\nIn [a new ARCore session](/ar/develop/java/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\n### Java\n\n```java\nConfig config = session.getConfig();\n\n// Check whether the user's device supports the Scene Semantics API.\nboolean isSceneSemanticsSupported =\n session.isSemanticModeSupported(Config.SemanticMode.ENABLED);\nif (isSceneSemanticsSupported) {\n config.setSemanticMode(Config.SemanticMode.ENABLED);\n}\nsession.configure(config);\n```\n\n### Kotlin\n\n```kotlin\nval config = session.config\n\n// Check whether the user's device supports the Scene Semantics API.\nval isSceneSemanticsSupported = session.isSemanticModeSupported(Config.SemanticMode.ENABLED)\nif (isSceneSemanticsSupported) {\n config.semanticMode = Config.SemanticMode.ENABLED\n}\nsession.configure(config)\n```\n\nObtain the semantic image\n-------------------------\n\nOnce Scene Semantics is enabled, the semantic image can be retrieved. The semantic image is a [`ImageFormat.Y8`](/ar/reference/java/com/google/ar/core/ImageFormat#Y8) image, where each pixel corresponds to a semantic label defined by [`SemanticLabel`](/ar/reference/java/com/google/ar/core/SemanticLabel).\n\nUse [`Frame.acquireSemanticImage()`](/ar/reference/java/com/google/ar/core/Frame#acquireSemanticImage) to acquire the semantic image: \n\n### Java\n\n```java\n// Retrieve the semantic image for the current frame, if available.\ntry (Image semanticImage = frame.acquireSemanticImage()) {\n // Use the semantic image here.\n} catch (NotYetAvailableException e) {\n // No semantic image retrieved for this frame.\n // The output image may be missing for the first couple frames before the model has had a\n // chance to run yet.\n}\n```\n\n### Kotlin\n\n```kotlin\n// Retrieve the semantic image for the current frame, if available.\ntry {\n frame.acquireSemanticImage().use { semanticImage -\u003e\n // Use the semantic image here.\n }\n} catch (e: NotYetAvailableException) {\n // No semantic image retrieved for this frame.\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 [`ImageFormat.Y8`](/ar/reference/java/com/google/ar/core/ImageFormat#Y8) 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 [`Frame.acquireSemanticConfidenceImage()`](/ar/reference/java/com/google/ar/core/Frame#acquireSemanticConfidenceImage) to acquire the semantic confidence image: \n\n### Java\n\n```java\n// Retrieve the semantic confidence image for the current frame, if available.\ntry (Image semanticImage = frame.acquireSemanticConfidenceImage()) {\n // Use the semantic confidence image here.\n} catch (NotYetAvailableException e) {\n // No semantic confidence image retrieved for this frame.\n // The output image may be missing for the first couple frames before the model has had a\n // chance to run yet.\n}\n```\n\n### Kotlin\n\n```kotlin\n// Retrieve the semantic confidence image for the current frame, if available.\ntry {\n frame.acquireSemanticConfidenceImage().use { semanticConfidenceImage -\u003e\n // Use the semantic confidence image here.\n }\n} catch (e: NotYetAvailableException) {\n // No semantic confidence image retrieved for this frame.\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 [`Frame.getSemanticLabelFraction()`](/ar/reference/java/com/google/ar/core/Frame#getSemanticLabelFraction-queryLabel) to acquire the fraction for a given label: \n\n### Java\n\n```java\n// Retrieve the fraction of pixels for the semantic label sky in the current frame.\ntry {\n float outFraction = frame.getSemanticLabelFraction(SemanticLabel.SKY);\n // Use the semantic label fraction here.\n} catch (NotYetAvailableException e) {\n // No fraction of semantic labels was retrieved for this frame.\n}\n```\n\n### Kotlin\n\n```kotlin\n// Retrieve the fraction of pixels for the semantic label sky in the current frame.\ntry {\n val fraction = frame.getSemanticLabelFraction(SemanticLabel.SKY)\n // Use the semantic label fraction here.\n} catch (e: NotYetAvailableException) {\n // No fraction of semantic labels was retrieved for this frame.\n}\n```"]]