AVCaptureVideo not showing label

空扰寡人 提交于 2019-12-11 16:00:10

问题


I am trying to retain the objects from the console to be shown in as a label(classifierText). The warning of "UILabel.text must be used from main thread only" appears. What seems to the problem as to why the items are being shown as the label?

var previewLayer: AVCaptureVideoPreviewLayer!

let classifierText: UILabel = {
    let classifier = UILabel()
    classifier.translatesAutoresizingMaskIntoConstraints = false
    classifier.textColor = .black
    classifier.font = UIFont(name: "Times-New-Roman", size: 10)
    classifier.textAlignment = .center
    return classifier
}()

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    guard let model =  try? VNCoreMLModel(for: Resnet50().model) else { return }
    let request = VNCoreMLRequest(model: model) { (finishedReq, err) in
    let results = finishedReq.results as?  [VNClassificationObservation]
    let firstObservation = results!.first
    self.classifierText.text = "\(firstObservation!.identifier as String)"

回答1:


The method captureOutput(sampleBuffer, etc) does not run on the main thread. So you're trying to change the label from a thread that is not the main thread.

The solution is to schedule the work on the main thread, like so:

DispatchQueue.main.async {
    self.classifierText.text = "\(firstObservation!.identifier as String)"
}


来源:https://stackoverflow.com/questions/46305582/avcapturevideo-not-showing-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!