0

所以我正在尝试在应用程序中使用 Vision + CoreML,是的,可以肯定的是,它使用 Apple 的 Resnet50 模型可以按预期工作。但是,我希望仅在用户点击按钮时使用 Vision。我相信这是可以检测对象是什么的功能:

private func setupVision() {
    guard let model = try? VNCoreMLModel(for: visionClassifier.model) else { return }
    request = VNCoreMLRequest(model: model) { (finishedReq, err) in

        // Get the results list and the first observation
        guard let results = finishedReq.results as? [VNClassificationObservation] else { return }
        guard let firstObservation = results.first else { return }
        
        // Format string output
        let name: String = firstObservation.identifier
        let conf: String = "Confidence: \(firstObservation.confidence * 100)"
        
        // Return the results from the background thread to the main thread
        DispatchQueue.main.async {
            self.identifier = name
            self.confidence = conf
        }
    }
}

所以你可能认为只是在你的视图中调用那个函数,然而,这个函数是在.onAppear()方法上调用的:

  func prepareCapture() {
        setupSession()
        setupVision()
        startSession()
    }

如果我取出 setupVision() 函数,我的应用程序将在这一行崩溃:

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

   
    // The captured video frame is stored in a CVPixelBuffer object
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    
    // Use a VNImageRequestHandler to perform the request
    try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
    //This line causes a crash
    
}

我可以做些什么来实现快照和扫描功能?我是否应该允许用户拍照(不向他们展示)并处理其余部分,就像使用 CoreML 和 ImagePicker 时一样?

任何帮助都会很棒!

4

0 回答 0