【问题标题】:Append multiple VNCoreMLModel ARKit and CoreML追加多个 VNCoreMLModel ARKit 和 CoreML
【发布时间】:2023-03-13 22:12:01
【问题描述】:

我是菜鸟,我真的不知道如何将多个 CoreML 模型用于 VNCoreMLRequest。 下面的代码只是使用一个模型,但我还想附加另一个模型(下面示例中的 visionModel2)。谁能帮我?谢谢!

private func performVisionRequest(pixelBuffer: CVPixelBuffer){
    let visionModel = try! VNCoreMLModel(for: self.iFaceModel.model)
    let visionModel2 = try! VNCoreMLModel(for: self.ageModel.model)
    let request = VNCoreMLRequest(model: visionModel){ request, error in

        if error != nil {
            return
        }

        guard let observations = request.results else {
            return
        }

        let observation = observations.first as! VNClassificationObservation

        print("Name \(observation.identifier) and confidence is \(observation.confidence)")


        DispatchQueue.main.async {
            if observation.confidence.isLess(than: 0.04) { 
                self.displayPredictions(text: "Not recognized")
                print("Hidden")
            }else {
                self.displayPredictions(text: observation.identifier)
            }
        }
    }

【问题讨论】:

    标签: swift append arkit coreml


    【解决方案1】:

    要使用多个 ML 模型评估图像,您需要执行多个请求。例如:

    let faceModelRequest = VNCoreMLRequest(model: visionModel)
    let ageModelRequest = VNCoreMLRequest(model: visionModel2)
    
    let handler = VNImageRequestHandler( /* my image and options */ )
    handler.perform([faceModelRequest, ageModelRequest])
    
    guard let faceResults = faceModelRequest.results as? [VNClassificationObservation],
        let ageResults = ageModelRequest.results as? [VNClassificationObservation]
    else { /*handle errors from each request */ }
    

    (是的,您可以在不使用完成处理程序的情况下运行 Vision 请求,然后从多个请求中收集结果。不过,可能需要检查请求上的 prefersBackgroundProcessing 并自己将所有内容分派到后台队列。)

    之后,您可能希望将两个请求的结果一起迭代。下面是一个方便的方法,你可以使用 Swift 标准库序列函数来实现,但它假设两个模型以相同的顺序返回关于相同面孔的信息:

    for (faceObservation, ageObservation) in zip (faceResults, ageResults) {
        print(“face \(faceObservation.classification) confidence \(faceObservation.confidence)”)
        print(“age \(ageObservation.classification) confidence \(ageObservation.confidence)”)
    
        // whatever else you want to do with results...
    }
    

    免责声明:在 StackExchange iOS 应用程序中编写的代码,未经测试。但这至少是您可能正在寻找的内容的草图——根据需要进行调整。

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 2017-12-12
      • 2019-03-15
      • 2019-08-17
      • 2020-03-10
      • 2020-04-07
      • 2016-07-06
      • 2020-05-06
      • 2021-03-30
      相关资源
      最近更新 更多