打印标签时不显示相机预览

时间:2018-02-21 18:08:04

标签: ios swift camera

我在打开相机时共享下面的代码,我打印标签并且没有出现标签。有什么问题?

但标签没有出现,并且它没有出现在我的程序中我的程序检测到对象,我需要在最后用标签打印它,但遗憾的是我无法打印标签

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    let captureSession = AVCaptureSession()
    captureSession.sessionPreset = .photo

    guard let captureDevice = AVCaptureDevice.default(for: .video)  else{ return }

    guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }

    captureSession.addInput(input)
    captureSession.startRunning()

    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    view.layer.addSublayer(previewLayer)
    previewLayer.frame = view.frame

    let dataOutput = AVCaptureVideoDataOutput()
    dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "CameraQueue"))
    captureSession.addOutput(dataOutput)
}

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,error) in
        guard let results = finishedReq.results as? [VNClassificationObservation] else{ return }

        guard let firstObservation = results.first else { return }
        print(firstObservation.identifier , firstObservation.confidence)
        DispatchQueue.main.async {
            self.label.text = "\(firstObservation.identifier) ,\(firstObservation.confidence)"
        }   
    }

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

1 个答案:

答案 0 :(得分:0)

将预览图层添加到视图的子视图后,您需要调用self.view.bringSubview(toFront: self.label)。添加预览图层时,它将位于现有子视图的顶部。