0

I'm trying to capture camera frames in realtime to be processed using Firebase ML KIT. I've successfully displayed the camera view but I can't seem to get the captureOutput delegate function to be called.

P.s I'm new to iOS development.

private func startLiveVideo() {

    self.session.sessionPreset = AVCaptureSession.Preset.photo
    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
    let deviceInput = try! AVCaptureDeviceInput(device: captureDevice!)

     self.session.addInput(deviceInput)

    let deviceOutput = AVCaptureVideoDataOutput()

    deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
    deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))

    self.session.addOutput(AVCaptureVideoDataOutput())
    let imageLayer = AVCaptureVideoPreviewLayer(session: session)


    imageLayer.frame = CGRect(x: 0, y: 0, width: self.imageView.frame.size.width + 100, height: self.imageView.frame.size.height)
    imageLayer.videoGravity = .resizeAspectFill
    imageView.layer.addSublayer(imageLayer)

    self.session.startRunning()

}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    print("Frame captured")
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

0

You add the delegate for

let deviceOutput = AVCaptureVideoDataOutput() 
deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))

but add another instance here

self.session.addOutput(AVCaptureVideoDataOutput())

so replace it with

self.session.addOutput(deviceOutput)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I just forgot to undo that before I copy the code, it was self.session.addOutput(deviceOutput) the whole time this's not changing anything. – Milad Alakarie Jun 18 '19 at 14:23
0

It worked just fine after converting to Swift 5.