即时将图像添加到 AR 资源以进行图像识别 [英] Add image to AR Resources on the fly for image recognition

查看:25
本文介绍了即时将图像添加到 AR 资源以进行图像识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ARKit 1.5 引入了图像识别.在代码中,您必须像这样创建一组参考图像:

ARKit 1.5 introduces image recognition. In the code you have to create a Set of the reference images like this:

let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil)

然后可以识别集合中包含的图像.

and then images contained in the Set can be recognised.

我想知道是否可以将图像动态添加到这个 AR Resources 文件夹中.例如,用户可以拍照并将其发送到服务器,然后由 ARKit 识别.或者用户可以根据他的位置等接收一组图像.

I wanted to know if it is possible to add images on the fly to this AR Resources folder. For example a user could take a picture and send it to a server, it is then recognised by the ARKit. Or a user could receive a set of images based on his location, etc.

推荐答案

您不能在运行时修改默认文件夹的内容,尽管您可以即时创建图像.

You cannot amend the contents of the default folder at runtime, although you can create images on the fly.

举个例子,让我们将图像放入 Assets Folder(不是 ARResources 文件夹),在我的例子中称为moonTarget".

As an example let's put an image into the Assets Folder (not the ARResources one), which in my case is called 'moonTarget'.

然后我们可以创建一个在 viewDidLoad 等中调用的函数:

We could then create a function which we call in viewDidLoad etc:

/// Create ARReference Images From Somewhere Other Than The Default Folder
func loadDynamicImageReferences(){

    //1. Get The Image From The Folder
    guard let imageFromBundle = UIImage(named: "moonTarget"),
    //2. Convert It To A CIImage
    let imageToCIImage = CIImage(image: imageFromBundle),
    //3. Then Convert The CIImage To A CGImage
    let cgImage = convertCIImageToCGImage(inputImage: imageToCIImage)else { return }

    //4. Create An ARReference Image (Remembering Physical Width Is In Metres)
    let arImage = ARReferenceImage(cgImage, orientation: CGImagePropertyOrientation.up, physicalWidth: 0.2)

    //5. Name The Image
    arImage.name = "CGImage Test"

    //5. Set The ARWorldTrackingConfiguration Detection Images
    configuration.detectionImages = [arImage]
}


/// Converts A CIImage To A CGImage
///
/// - Parameter inputImage: CIImage
/// - Returns: CGImage
func convertCIImageToCGImage(inputImage: CIImage) -> CGImage? {
    let context = CIContext(options: nil)
    if let cgImage = context.createCGImage(inputImage, from: inputImage.extent) {
     return cgImage
    }
    return nil
}

然后我们可以在 ARSCNViewDelegate 中测试:

We can then test this in the ARSCNViewDelegate:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    //1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
    guard let currentImageAnchor = anchor as? ARImageAnchor else { return }

    let x = currentImageAnchor.transform
    print(x.columns.3.x, x.columns.3.y , x.columns.3.z)

    //2. Get The Targets Name
    let name = currentImageAnchor.referenceImage.name!

    //3. Get The Targets Width & Height In Meters
    let width = currentImageAnchor.referenceImage.physicalSize.width
    let height = currentImageAnchor.referenceImage.physicalSize.height

    print("""
    Image Name = \(name)
    Image Width = \(width)
    Image Height = \(height)
    """)

    //4. Create A Plane Geometry To Cover The ARImageAnchor
    let planeNode = SCNNode()
    let planeGeometry = SCNPlane(width: width, height: height)
    planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
    planeNode.opacity = 0.25
    planeNode.geometry = planeGeometry

    //5. Rotate The PlaneNode To Horizontal
    planeNode.eulerAngles.x = -.pi/2

    //The Node Is Centered In The Anchor (0,0,0)
    node.addChildNode(planeNode)

    //6. Create AN SCNBox
    let boxNode = SCNNode()
    let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

    //7. Create A Different Colour For Each Face
    let faceColours = [UIColor.red, UIColor.green, UIColor.blue, UIColor.cyan, UIColor.yellow, UIColor.gray]
    var faceMaterials = [SCNMaterial]()

    //8. Apply It To Each Face
    for face in 0 ..< 5{
        let material = SCNMaterial()
        material.diffuse.contents = faceColours[face]
        faceMaterials.append(material)
    }
    boxGeometry.materials = faceMaterials
    boxNode.geometry = boxGeometry

    //9. Set The Boxes Position To Be Placed On The Plane (node.x + box.height)
    boxNode.position = SCNVector3(0 , 0.05, 0)

    //10. Add The Box To The Node
    node.addChildNode(boxNode)   
}

如您所见,同样的内容也适用于实时供稿.

As you can see the same could also be applied from a live feed as well.

希望这有帮助...

正如@Karlis 所说,您还可以考虑使用 OnDemandResouces,然后将它们转换为 ARReferenceImage 的所需规格.

As @Karlis said you could also look at using OnDemandResouces and then converting them to desired specs of an ARReferenceImage.

更新:对于希望查看从服务器创建动态参考图像示例的任何人,请查看我创建的以下项目:动态参考图像示例代码

Update: For anyone looking to see an example of creating dynamic reference images from a Server please take a look at the following project I have created: Dynamic Reference Images Sample Code

这篇关于即时将图像添加到 AR 资源以进行图像识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆