Recognizing|Recognizing Images in an AR Experience

Detect known 2D images in the user’s environment, and use their positions to place AR content.
检测用户环境中的已知2D图像,并使用它们的位置放置AR内容。
Overview Many AR experiences can be enhanced by using known features of the user’s environment to trigger the appearance of virtual content. For example, a museum app might show a virtual curator when the user points their device at a painting, or a board game might place virtual pieces when the player points their device at a game board. In iOS 11.3 and later, you can add such features to your AR experience by enabling image recognition in ARKit: Your app provides known 2D images, and ARKit tells you when and where those images are detected during an AR session.
This example app looks for any of the several reference images included in the app’s asset catalog. When ARKit detects one of those images, the app shows a message identifying the detected image and a brief animation showing its position in the scene.
概述 通过使用用户环境的已知特征来触发虚拟内容的出现,可以增强许多AR体验。 例如,当用户将他们的设备指向绘画时,博物馆应用可以显示虚拟馆长,或者当玩家将他们的设备指向游戏板时,棋盘游戏可以放置虚拟件。 在iOS 11.3和更高版本中,您可以通过在ARKit中启用图像识别功能将这些功能添加到AR体验中:您的应用程序提供已知的2D图像,ARKit会告诉您在AR会话期间何时何地检测到这些图像。
此示例应用程序查找应用程序资产目录中包含的任意几个参考图像。 当ARKit检测到其中一个图像时,该应用程序会显示一条消息,标识检测到的图像以及显示其在场景中的位置的简短动画。
Important
The images included with this sample are designed to fit the screen sizes of various Apple devices. To try the app using these images, choose an image that fits any spare device you have, and display the image full screen on that device. Then run the sample code project on a different device, and point its camera at the device displaying the image. Alternatively, you can add your own images; see the steps in Provide Your Own Reference Images, below.
重要
此示例中包含的图像旨在适合各种Apple设备的屏幕尺寸。 要尝试使用这些图像的应用程序,请选择适合您的任何备用设备的图像,并在该设备上全屏显示图像。 然后在其他设备上运行示例代码项目,并将其相机指向显示图像的设备。 或者,您可以添加自己的图像; 请参阅下面提供您自己的参考图片中的步骤。
Enable Image Detection Image recognition is an add-on feature for world-tracking AR sessions. (For more details on world tracking, see Building Your First AR Experience.)
To enable image detection:
1.Load one or more ARReferenceImage resources from your app’s asset catalog.
2.Create a world-tracking configuration and pass those reference images to its detectionImages property.
3.Use the runWithConfiguration:options: method to run a session with your configuration.
The code below shows how the sample app performs these steps when starting or restarting the AR experience.
启用图像检测 图像识别是世界追踪AR Session的附加功能。 (有关世界追踪的更多详情,请参阅创建您的第一次AR体验。)
要启用图像检测:
1.从应用的Asset目录中加载一个或多个ARReferenceImage资源。
2.创建一个世界跟踪配置,并将这些参考图像传递给它的detectionImages属性。
3.使用runWithConfiguration:options:方法与您的配置一起运行Session。
下面的代码显示了示例应用程序在启动或重新启动AR体验时如何执行这些步骤。


Recognizing|Recognizing Images in an AR Experience
文章图片
Visualize Image Detection Results When ARKit detects one of your reference images, the session automatically adds a corresponding ARImageAnchor to its list of anchors. To respond to an image being recognized, implement an appropriate ARSessionDelegate, ARSKViewDelegate, or ARSCNViewDelegate method that reports the new anchor being added to the session. (This example app uses the renderer:didAddNode:forAnchor: method for the code shown below.)
To use the detected image as a trigger for AR content, you’ll need to know its position and orientation, its size, and which reference image it is. The anchor’s inherited transformproperty provides position and orientation, and its referenceImage property tells you which ARReferenceImage object was detected. If your AR content depends on the extent of the image in the scene, you can then use the reference image’s physicalSize to set up your content, as shown in the code below.
可视化图像检测结果 当ARKit检测到一个参考图像时,Session会自动将相应的ARImageAnchor添加到其锚点列表中。 要响应正在识别的图像,请实施适当的ARSessionDelegate,ARSKViewDelegate或ARSCNViewDelegate方法,以报告添加到Session中的新锚点。 (这个示例应用程序对下面显示的代码使用渲染器:didAddNode:forAnchor:方法。)
要将检测到的图像用作AR内容的触发器,您需要知道它的位置和方向,大小以及它是哪个参考图像。 该锚点的继承transformproperty提供了位置和方向,其referenceImage属性告诉你哪个ARReferenceImage对象被检测到。 如果您的AR内容取决于场景中图像的范围,则可以使用参考图像的physicalSize设置您的内容,如下面的代码所示。


Recognizing|Recognizing Images in an AR Experience
文章图片
Provide Your Own Reference Images To use your own images for detection (in this sample or in your own project), you’ll need to add them to your asset catalog in Xcode.
Open your project’s asset catalog, then use the Add button (+) to add a new AR resource group.
Drag image files from the Finder into the newly created resource group.
For each image, use the inspector to describe the physical size of the image as you’d expect to find it in the user’s real-world environment, and optionally include a descriptive name for your own use.
提供您自己的参考图像 要使用自己的图像进行检测(在本示例中或在您自己的项目中),您需要将它们添加到Xcode中的Asset目录中。
打开项目的Asset目录,然后使用添加按钮(+)添加新的AR资源组。
将Finder中的图像文件拖到新创建的资源组中。
对于每幅图像,请使用检查员来描述图像的物理尺寸,正如您期望在用户的真实环境中找到它的一样,并且可以选择包含一个供您自己使用的描述性名称。
Note
Put all the images you want to look for in the same session into a resource group. Use separate resource groups to hold sets of images for use in separate sessions. For example, an art museum app might use separate sessions (and thus separate resource groups) for recognizing paintings in different wings of the museum.
注意
将您想要在同一个会话中查找的所有图像放入资源组中。 使用单独的资源组来保存各组图像以便在单独的会话中使用。 例如,美术馆的应用程序可能会使用单独的Session(并因此分开资源组)来识别博物馆不同侧翼的绘画。
Be aware of image detection capabilities. Choose, design, and configure reference images for optimal reliability and performance:
Enter the physical size of the image in Xcode as accurately as possible. ARKit relies on this information to determine the distance of the image from the camera. Entering an incorrect physical size will result in an ARImageAnchor that’s the wrong distance from the camera.
When you add reference images to your asset catalog in Xcode, pay attention to the quality estimation warnings Xcode provides. Images with high contrast work best for image detection.
Use only images on flat surfaces for detection. If an image to be detected is on a nonplanar surface, like a label on a wine bottle, ARKit might not recognize it at all, or might create an image anchor at the wrong location.
Consider how your image appears under different lighting conditions. If an image is printed on glossy paper or displayed on a device screen, reflections on those surfaces can interfere with detection.
了解图像检测功能。选择,设计和配置参考图像以获得最佳的可靠性和性能:
尽可能准确地在Xcode中输入图像的物理尺寸。 ARKit依靠此信息来确定图像距相机的距离。输入不正确的物理尺寸将导致ARImageAnchor与相机距离不正确。
将参考图像添加到Xcode中的Asset目录时,请注意Xcode提供的质量估算警告。具有高对比度的图像最适合图像检测。
只使用平面上的图像进行检测。如果要检测的图像位于非平面表面上,例如酒瓶上的标签,则ARKit可能根本无法识别它,或者可能会在错误的位置创建图像定位点。
考虑你的图像在不同的照明条件下如何显示。如果图像打印在光面纸上或显示在设备屏幕上,那么这些表面上的反射会干扰检测。
Apply Best Practices This example app simply visualizes where ARKit detects each reference image in the user’s environment, but your app can do much more. Follow the tips below to design AR experiences that use image detection well.
Use detected images to set a frame of reference for the AR scene. Instead of requiring the user to choose a place for virtual content, or arbitrarily placing content in the user’s environment, use detected images to anchor the virtual scene. You can even use multiple detected images. For example, an app for a retail store could make a virtual character appear to emerge from a store’s front door by recognizing posters placed on either side of the door and then calculating a position for the character directly between the posters.
应用最佳实践 这个示例应用程序简单地可视化ARKit在用户环境中检测每个参考图像的位置,但您的应用程序可以做更多。 请按照以下提示设计良好地使用图像检测的AR体验。
使用检测到的图像为AR场景设置参考框架。 而不是要求用户选择虚拟内容的地方,或任意放置内容在用户的环境中,使用检测到的图像来锚定虚拟场景。 你甚至可以使用多个检测到的图像。 例如,一家零售商店的应用程序可以通过识别放置在门两侧的海报,然后直接在海报之间计算角色的位置,使商店的前门出现虚拟角色。
Note
Use the ARSession setWorldOrigin: method to redefine the world coordinate system so that you can place all anchors and other content relative to the reference point you choose.
注意
使用ARSession setWorldOrigin:方法重新定义世界坐标系,以便您可以将所有锚点和其他内容相对于您选择的参考点放置。
Design your AR experience to use detected images as a starting point for virtual content.ARKit doesn’t track changes to the position or orientation of each detected image. If you try to place virtual content that stays attached to a detected image, that content may not appear to stay in place correctly. Instead, use detected images as a frame of reference for starting a dynamic scene. For example, your app might recognize theater posters for a sci-fi film and then have virtual spaceships appear to emerge from the posters and fly around the environment.
Consider when to allow detection of each image to trigger (or repeat) AR interactions. ARKit adds an image anchor to a session exactly once for each reference image in the session configuration’s detectionImages array. If your AR experience adds virtual content to the scene when an image is detected, that action will by default happen only once. To allow the user to experience that content again without restarting your app, call the session’s removeAnchor: method to remove the corresponding ARImageAnchor. After the anchor is removed, ARKit will add a new anchor the next time it detects the image.
For example, in the case described above, where spaceships appear to fly out of a movie poster, you might not want an extra copy of that animation to appear while the first one is still playing. Wait until the animation ends to remove the anchor, so that the user can trigger it again by pointing their device at the image.
设计您的AR体验,将检测到的图像用作虚拟内容的起点。 ARKit不会跟踪每个检测图像的位置或方向的更改。如果您尝试放置保留附加到检测到的图像的虚拟内容,该内容可能无法正确保留。相反,使用检测到的图像作为开始动态场景的参考框架。例如,您的应用可能会为科幻电影识别影院海报,然后虚拟宇宙飞船似乎会从海报中浮现出来并在环境中飞行。
考虑何时允许检测每个图像以触发(或重复)AR交互。 ARKit为Session配置的detectionImages数组中的每个参考图像精确地添加一次图像定位点。如果您的AR体验在检测到图像时将虚拟内容添加到场景中,则该操作默认只会发生一次。要允许用户在不重新启动应用程序的情况下再次体验该内容,请调用会话的removeAnchor:方法来删除相应的ARImageAnchor。锚点被移除后,ARKit会在下一次检测到图像时添加新的锚点。
【Recognizing|Recognizing Images in an AR Experience】例如,在上面描述的情况下,当飞船出现在电影海报中时,您可能不希望在第一个播放时播放该动画的额外副本。等待动画结束以移除锚点,以便用户可以通过将其设备指向图像来再次触发它。

    推荐阅读