iOS 장치의 [비디오] 카메라에서 "라이브 스트림"을 가져 와서 백그라운드에서 발생하는 CoreML 이미지 분류 작업이 있습니다. 객체가 식별되고 다른 응용 프로그램 논리가 발생하면 일부 데이터로 UI 레이블을 업데이트하고 싶습니다.스위프트 4 : DispatchQueue.main (범위)의 액세스 변수
DispatchQueue.main.asyc(execute: { })
에 대한 설명 선이 내가 사용하고있는 변수에 어떻게 액세스 할 수 있는지 설명 할 수 있습니까? 나는 이것이 본질적으로 범위 문제라고 생각한다.
코드 내가 현재 사용하고 있습니다 : 문제를 일으키는 스위치 문 내부
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
processCameraBuffer(sampleBuffer: sampleBuffer)
}
func processCameraBuffer(sampleBuffer: CMSampleBuffer) {
let coreMLModel = Inceptionv3()
if let model = try? VNCoreMLModel(for: coreMLModel.model) {
let request = VNCoreMLRequest(model: model, completionHandler: { (request, error) in
if let results = request.results as? [VNClassificationObservation] {
var counter = 0
var otherVar = 0
for item in results[0...9] {
if item.identifier.contains("something") {
print("some app logic goes on here")
otherVar += 10 - counter
}
counter += 1
}
switch otherVar {
case _ where otherVar >= 10:
DispatchQueue.main.async(execute: {
let displayVarFormatted = String(format: "%.2f", otherVar/65 * 100)
self.labelPrediction.text = "\(counter): \(displayVarFormatted)%"
})
default:
DispatchQueue.main.async(execute: {
self.labelPrediction.text = "No result!"
})
}
}
})
if let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
do {
try handler.perform([request])
} catch {
print(error.localizedDescription)
}
}
}
}
그것 self.labelPrediction.text = ""
라인. 이 var는 항상 현재 0입니다.
언급 한 줄에 중단 점을 넣고 변수가 무엇인지 확인하십시오. 일반적으로 블록은 필요한 값을 캡처합니다. –