gpt4 book ai didi

ios - 在 Swift 中调用多个请求

转载 作者:行者123 更新时间:2023-11-30 11:44:31 30 4
gpt4 key购买 nike

使用 CoreML 并尝试使用相机作为图像识别的源来执行两个模型。但是,我似乎无法允许 VNCoreMLRequest 同时运行两个模型。关于如何根据此请求运行两个模型有什么建议吗?

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

var fitness_identifer = ""
var fitness_confidence = 0

guard let model_one = try? VNCoreMLModel(for: imagenet_ut().model) else { return }
guard let model_two = try? VNCoreMLModel(for: ut_legs2().model) else { return }

let request = VNCoreMLRequest(model: [model_one, model_two]) { (finishedRequest, error) in
guard let results = finishedRequest.results as? [VNClassificationObservation] else { return }
guard let Observation = results.first else { return }

DispatchQueue.main.async(execute: {
fitness_identifer = Observation.identifier
fitness_confidence = Int(Observation.confidence * 100)

self.label.text = "\(Int(fitness_confidence))% it's a \(fitness_identifer)"
})
}

guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }

// executes request
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}

当我尝试将两个模型添加为数组时(当我只有一个可以工作的模型时),这是错误:

Contextual type 'VNCoreMLModel' cannot be used with array literal

最佳答案

为什么不在 AsyncGroup 中运行两个单独的请求:

let request1 = VNCoreMLRequest(model: model_one) { (finishedRequest, error) in
//...
}

let request2 = VNCoreMLRequest(model: model_two) { (finishedRequest, error) in
//...
}

//...
let group = AsyncGroup()
group.background {
// Run on background queue
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request1])
}
group.background {
// Run on background queue
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request2])
}
group.wait()
// Both operations completed here

关于ios - 在 Swift 中调用多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48979592/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com