当前位置: 首页 > article >正文

IOS 关于ARKi使用

ARSCNView使用

var sceneView: ARSCNView = ARSCNView(frame: UIScreen.main.bounds)
 let configuartion = ARWorldTrackingConfiguration()
    //会话session ARSessionDelegate 会话状态更新相关代理方法
        sceneView.session.delegate = self
        //ARSCNViewDelegate 节点场景管理相关代理方法
        sceneView.delegate = self
        
        // 自动对焦开启
        configuartion.isAutoFocusEnabled = true
        //平面检测方向
        configuartion.planeDetection = .horizontal
        //光线估计
        configuartion.isLightEstimationEnabled = true
        /*
         使用提供的配置运行会话,
         resetTracking 移动到新环境重新扫描追踪
         */
        sceneView.session.run(configuartion, options: [.resetTracking,.removeExistingAnchors])
         //调试参数
        sceneView.debugOptions = [SCNDebugOptions.showFeaturePoints]

1 加载立体模型
3d模型网站
https://sketchfab.com/3d-models/coin-d41feb2c10ed4c06ad4b8134ccaba516 自己上去找模型

在这里插入图片描述
加载这个模型到场景中

sceneView.scene = SCNScene(named: "ship.scn", inDirectory: "models.scnassets/ship") ?? SCNScene()

2 识别图片

     var images :[UIImage] = []
        for i in 1...19{
            let image = UIImage(named: "AR\(i)")
            images.append(image!)
        }
        configuartion.detectionImages = loadedImagesFromDirectoryContents(images)
  
  
  //其实可以直接加载图片文件的,这样图片直接转ARSet更直接一点
   func loadedImagesFromDirectoryContents(_ images: [UIImage]) -> Set<ARReferenceImage>{

        var index = 0
        var customReferenceSet = Set<ARReferenceImage>()

        images.forEach { (downloadedImage) in

            //1. Convert The UIImage To A CGImage
            guard let cgImage = downloadedImage.cgImage else { return }

            //2. Get The Width Of The Image
            let imageWidth = CGFloat(cgImage.width)

            //3. Create A Custom AR Reference Image With A Unique Name
            let customARReferenceImage = ARReferenceImage(cgImage, orientation: CGImagePropertyOrientation.up, physicalWidth: imageWidth)
            customARReferenceImage.name = "MyCustomARImage\(index)"

            //4. Insert The Reference Image Into Our Set
            customReferenceSet.insert(customARReferenceImage)

            print("ARReference Image == \(customARReferenceImage)")

            index += 1


        }


        //5. Return The Set
        return customReferenceSet

    }
    
    
    
    //在ARSCNViewDelegate方法里
    
     func renderer(_ renderer: any SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
     
        if let imageAnchor = anchor as? ARImageAnchor{
            let image = imageAnchor.referenceImage

            if let imagename = image.name, imagename.hasPrefix("AR"){
               print("识别出来图片")
            }
        }
        
        //加载3d数据识别出来立体物体,具体如下
         if let objectAnchor = anchor as? ARObjectAnchor {
              print("Detected object: \(objectAnchor.referenceObject.name ?? "Unknown")")
              
             }
     }      
        

视觉算法 Vision识别

//获取当前会话帧数据
    func getCurrentFrameImage(from session: ARSession) -> UIImage? {
        guard let currentFrame = session.currentFrame else {
            print("当前没有 ARFrame")
            return nil
        }
        let pixelBuffer = currentFrame.capturedImage
        let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
        let context = CIContext()
        guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
            return nil
        }
        return UIImage(cgImage: cgImage)
    }
    
    
     func matchImages(image1: UIImage, image2: UIImage, completion: @escaping (Bool) -> Void) {
        guard let cgImage1 = image1.cgImage, let cgImage2 = image2.cgImage else {
            completion(false)
            return
        }
        let request1 = VNGenerateImageFeaturePrintRequest()
        let request2 = VNGenerateImageFeaturePrintRequest()
        
        let request3 = VNDetectRectanglesRequest()
            request3.minimumConfidence = 0.8
            request3.minimumAspectRatio = 0.1
            request3.maximumAspectRatio = 1.0
            request3.quadratureTolerance = 10
        
        let handler1 = VNImageRequestHandler(cgImage: cgImage1, options: [:])
        let handler2 = VNImageRequestHandler(cgImage: cgImage2, options: [:])
        let handler3 = VNImageRequestHandler(cgImage: cgImage1, options: [:])

        do {
            try handler1.perform([request1])
            try handler2.perform([request2])
            try handler3.perform([request3])

            guard let featurePrint1 = request1.results?.first as? VNFeaturePrintObservation,
                  let featurePrint2 = request2.results?.first as? VNFeaturePrintObservation else {
                completion(false)
                return
            }
            
            if let observations = request3.results as? [VNRectangleObservation], !observations.isEmpty{
                // 假设目标区域是第一个匹配的矩形
                let boundingBox = observations.first?.boundingBox
                print("boundRext: \(boundingBox)")
            }

            var distance: Float = 0
            try featurePrint1.computeDistance(&distance, to: featurePrint2)
            print("匹配值 \(distance)")
            completion(distance < 0.8) // 设定相似度阈值
        } catch {
            print("匹配失败:\(error)")
            completion(false)
        }
    }
    
    //当前数据和所有图片集对比
    func handleFrame(session: ARSession,frame: ARFrame){
        let i = frame.timestamp - (self.currentFrmae?.timestamp ?? 0)
//        print("time i\(i)")
        if i < 1{
            return
        }
        self.currentFrmae = frame
        guard let frameImg = getCurrentFrameImage(from: session) else{return}
        var isMatched:Bool = false
        for img in self.images{
//            if compareImages(image1: frameImg, image2: img) == 1{
//                isMatched = true
//                break
//            }
            let im1 = frameImg
            let im2 = img
            matchImages(image1: frameImg, image2: img) { [weak self]isok in
                if isok{
                    DispatchQueue.main.async {
                        self?.showToast()
                    }
                }
            }
        }
    }

http://www.kler.cn/a/458622.html

相关文章:

  • 安装教程:慧集通集成平台(DataLinkX)智能体客户端安装操作(Linux/windows/mac)
  • Springboot3.x整合swagger3
  • Python列表解析式是否支持 else
  • 数据中台与数据治理服务方案[50页PPT]
  • 微信小程序调用 WebAssembly 烹饪指南
  • Nature+Science=ONNs(光学神经网络)
  • 通过Cephadm工具搭建Ceph分布式存储以及通过文件系统形式进行挂载的步骤
  • Day58 图论part08
  • HarmonyOS NEXT应用开发实战:免费练手的网络API接口分享
  • 手机租赁平台开发全攻略打造高效便捷的租赁服务系统
  • 【Java 数据结构】面试题 02.02. 返回倒数第 k 个节点
  • 计算机网络 (7)物理层下面的传输媒体
  • 宝塔-firefox(Docker应用)-构建自己的Web浏览器
  • PyQt实战——使用python提取JSON数据(十)
  • 树形查询转成TreeNode[],添加新节点
  • MongoDB 管理工具
  • C# 使用Newtonsoft.Json
  • 数据库的创建使用与查找
  • 【集合】——LinkedList
  • 机器算法之逻辑回归(Logistic Regression)详解
  • 【Leetcode 热题 100】208. 实现 Trie (前缀树)
  • LeetCode 876:链表的中间节点
  • 典型常见的基于知识蒸馏的目标检测方法总结三
  • Langchain Chat Model 和 Chat Prompt Template
  • 【Axios】如何在Vue中使用Axios请求拦截器
  • Flutter DragTarget拖拽控件详解