gpt4 book ai didi

swift - AVCaptureVideo 不显示标签

转载 作者:行者123 更新时间:2023-11-28 12:16:38 30 4
gpt4 key购买 nike

我正在尝试保留控制台中的对象,以将其显示为标签(分类器文本)。出现“UILabel.text must be used from main thread only”的警告。为什么项目显示为标签似乎有什么问题?

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)"

最佳答案

captureOutput(sampleBuffer, etc) 方法不在主线程上运行。所以你试图从一个不是主线程的线程更改标签。

解决方案是在主线程上安排工作,像这样:

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

关于swift - AVCaptureVideo 不显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46305582/

30 4 0