借助 Scene Semantics API,开发者可以通过提供基于机器学习模型的实时语义信息来了解用户周围的场景。给定一张户外场景图片,该 API 会针对一组实用的语义类别(例如天空、建筑物、树木、道路、人行道、车辆、人等)为每个像素返回一个标签。除了像素标签之外,Scene Semantics API 还会为每个像素标签提供置信度值,并提供一种简单易用的方式来查询给定标签在户外场景中的普遍性。
[null,null,["最后更新时间 (UTC):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```"]]