Image recognition and CMSampleBufferGetImageBuffer

I'm new to Swift and trying to make sense of it all. I'm trying to figure out how to create a function that stops the image observations once the model matches with a pre-defined word.

Any suggestions on how set this up?

Thanks


func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        //        print("Camera was able to capture a frame:", Date())

        guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }


        guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else { return }
        let request = VNCoreMLRequest(model: model) { (finishedReq, err) in


            guard let results = finishedReq.results as? [VNClassificationObservation], let matchedResult = results.first else { return }

            guard let firstObservation = results.first else { return }

            print(firstObservation.identifier, firstObservation.confidence)
            
            DispatchQueue.main.async {
                self.confidenceLabel.text = "\(firstObservation.identifier) \(firstObservation.confidence * 100)"
                
                            if matchedResult.identifier.contains(self.words.word) {
                                print("Image matched") } else { return}
                            self.matchLabel.text = String?("matched")
                }
            }

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