arkit 检测坐标_在ARKit中使用视觉框架对象检测

arkit 检测坐标

In this short tutorial we’ll use Vision Framework to add object detection and classification capabilities to a bare-bones ARKit project. We’ll use an open source Core ML model to detect a remote control, get its bounding box center, transform its 2D image coordinates to 3D and then create an anchor which can be used for placing objects in an AR scene.

在这个简短的教程中,我们将使用Vision Framework将对象检测和分类功能添加到一个简单的ARKit项目中。 我们将使用开源的Core ML模型来检测遥控器,获取其边界框中心,将其2D图像坐标转换为3D,然后创建可用于在AR场景中放置对象的锚点。

Here’s a preview of what we’ll create:

这是我们将创建的内容的预览:

To get started you’ll need to create a new Augmented Reality App in Xcode: File > New > Project … and then choose “Augmented Reality App”.

首先,您需要在Xcode中创建一个新的增强现实应用程序:File> New> Project…,然后选择“ Augmented Reality App”。

Replace the code in ViewController.swift with the code below so we can get a clean start:

用下面的代码替换ViewController.swift中的代码,这样我们就可以轻松开始:

We’ll use a freely available open source Core ML model called YOLO which stands for “You Only Look Once”. It is a state-of-the-art, real-time object detection system. which can locate and classify 80 different types of objects.

我们将使用一个免费的开源核心ML模型,称为YOLO,它代表“您只看一次”。 它是最先进的实时对象检测系统。 可以定位和分类80种不同类型的对象。

The Core ML model can be downloaded from Apple’s Developer website: https://developer.apple.com/machine-learning/models/. Scroll down to “YOLOv3-Tiny”, click “View Models” and then download the file “YOLOv3TinyInt8LUT.mlmodel”.

可以从Apple的开发人员网站上下载Core ML模型: https : //developer.apple.com/machine-learning/models/ 。 向下滚动到“ YOLOv3-Tiny”,单击“查看模型”,然后下载文件“ YOLOv3TinyInt8LUT.mlmodel”。

Once downloaded, drag the .mlmodel file from Finder into Xcode and make sure it is added to the target.

下载后,将.mlmodel文件从Finder拖到Xcode中,并确保将其添加到目标中。

Object detection needs a camera image so we’ll hook into SCNSceneRendererDelegate’s renderer(_:willRenderScene:atTime:) method to query for an image and start the object detection process if the image is available.

对象检测需要相机图像,因此我们将使用SCNSceneRendererDelegaterenderer(_:willRenderScene:atTime:)方法来查询图像,并在图像可用时启动对象检测过程。

Using ARKit’s captured image we’ll create an image request and make it perform an object detection request:

使用ARKit捕获的图像,我们将创建一个图像请求,并使其执行对象检测请求:

Here the image imageRequestHandler performs an object detection request called objectDetectionRequest . This request needs to be created only once and can be defined in a lazy variable. Here, we create an instance of the YOLO model and create a CoreML request.

在这里,图像imageRequestHandler执行一个称为objectDetectionRequest的对象检测请求。 该请求仅需要创建一次,并且可以在惰性变量中定义。 在这里,我们创建YOLO模型的实例并创建CoreML请求。

Here, processDetections is VNCoreMLRequest’s completion handler. This is where we’ll get the recognized remote control object and its bounding box and then do all the necessary conversions to get 3D world coordinates which we can use to create an ARKit anchor.

在这里, processDetectionsVNCoreMLRequest的完成处理程序。 在这里,我们将获得公认的远程控制对象及其边界框,然后进行所有必要的转换,以获取可用于创建ARKit锚点的3D世界坐标。

First we need to go through all the observations, check the classification string to see if a remote control is detected and then filter out low confidence observations:

首先,我们需要检查所有观察值,检查分类字符串以查看是否检测到遥控器,然后过滤掉低置信度观察值:

Now that we are confident we’ve detected a remote control we can get its bounding box. The bounding box’s coordinates are normalized image coordinates. A few conversions have to be done to get the view coordinates:

现在,我们有信心检测到一个遥控器,可以得到它的边界框。 边界框的坐标是归一化的图像坐标。 必须进行一些转换才能获取视图坐标:

We’ll now use the bounding box center as the coordinate we’ll want to convert to 3D space. It might not be the actual center of the detected ‘real world’ object but that goes beyond the scope of this tutorial.

现在,我们将边界框中心用作要转换为3D空间的坐标。 它可能不是检测到的“真实世界”对象的实际中心,但是超出了本教程的范围。

To get the 3D world coordinate we can use the view-space center point to perform a hit test. If we specify featurePoint as the hit test result type, ARKit finds the feature point nearest to the hit-test ray. If we get a result we can use its worldTransfrom property to create an ARKit anchor and add it to the session:

要获取3D世界坐标,我们可以使用视图空间中心点进行点击测试。 如果我们将featurePoint指定为命中测试结果类型,则ARKit会找到最接近命中测试射线的特征点。 如果得到结果,则可以使用其worldTransfrom属性创建一个ARKit锚并将其添加到会话中:

Adding this anchor to the session will invoke ARSCNViewDelegate ‘s renderer(_,didAdd:,for:) function in which we can add 3D content to the scene. In this case we’ll add a simple red sphere and attach it to the anchor:

将这个锚点添加到会话中将调用ARSCNViewDelegaterenderer(_,didAdd:,for:)函数,我们可以在其中向场景添加3D内容。 在这种情况下,我们将添加一个简单的红色球体并将其附加到锚点:

The red sphere will now be placed on the detected remote control and will persist like any other ARKit object.

现在,红色球体将放置在检测到的遥控器上,并且将像其他任何ARKit对象一样持续存在。

Again, this might not be the most accurate solution to place virtual content over real world content. However, it shows a few really useful techniques that might come in handy in your own ARKit projects.

同样,这可能不是将虚拟内容放置在现实世界内容上的最准确解决方案。 但是,它显示了一些非常有用的技术,这些技术可能会在您自己的ARKit项目中派上用场。

The Xcode project can be found here: https://github.com/MasDennis/ARKitVisionObjectDetection

可以在这里找到Xcode项目: https : //github.com/MasDennis/ARKitVisionObjectDetection

翻译自: https://medium.com/@Rozengain/using-vision-framework-object-detection-in-arkit-c0b5366f465d

arkit 检测坐标

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程介绍 本套课程从技术理念到项目实践,教大家系统掌握ARKit技术开发,随心打造iOS端AR增强现实应用。由一开始的开发环境搭建,到Unity ARKit Plugin、ARFoundation ARKit等不同时期技术的讲解。从平面模型放置、识别图片、手势交互、3D物体识别、面部AR贴纸、光照估计、环境探针、多人AR技术,甚至包含ARKit3.0的动作捕捉技术等。除了以上课程内容,更加入了随着技术更新与时俱进更新的ARKit连载技术教学内容。课程收益 轻松理解ARKit的技术原理,从零到一创建自己的第一个AR项目。学习UnityARKit Plugin经典技术,了解ARKit的常见概念和原理。学会在 ARFoundation 使用ARKit,进行企业级AR项目开发。学会如何在AR项目里播放模型动画,通过触屏手势交互实现模型的旋转和缩放。 了解ARKit的图片识别,掌握AR卡片、AR书籍的实现方法。 学会使用面部增强技术,实现热门短视频应用的AR贴纸效果,实现面部表情追踪。学会使用ARKit物体检测技术,实现3D物体识别,可以广泛应用于工业、建筑、古董、玩具手办、艺展览等不同场景。学习ARKit的经典交互案例,优化AR项目的用户体验。熟练使用环境纹理、光照估计技术,让AR内容随当前现实场景自动变化调节,看起来更加真实。基于WorldMap、CollaborativeSession AR技术,实现AR场景的持久化及多人AR体验效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值