Stop detecting letters in Apple Vision, iOS, Swift

1k views Asked by At

I am detecting text with Vision and it is working ok I have it use a completion when something detected and call a function, but the text is still being detected.

How can I stop the text detection

To start it i am using:

func startTextDetection() {
    let textRequest = VNDetectTextRectanglesRequest(completionHandler: self.detectTextHandler)
    textRequest.reportCharacterBoxes = true
    self.requests = [textRequest]

}

// I tried this to stop it but it only hides the boxes around the text.
//func stopTextDetection() {
//  let textRequest = VNDetectTextRectanglesRequest(completionHandler: //self.detectTextHandler)
//  textRequest.reportCharacterBoxes = false
//  self.requests = [textRequest]
//}

func detectTextHandler(request: VNRequest, error: Error?) {
    guard let observations = request.results else {
        print("no result")
        return
    }
1

There are 1 answers

0
Tony Merritt On

I was able to figure this out by adding the following functions and call it when detection is reached.

func stopTextDetection() {
    let textRequest = VNDetectTextRectanglesRequest(completionHandler: self.detectTextHandlerStop)
    textRequest.reportCharacterBoxes = false
    self.requests = [textRequest]


}

func detectTextHandlerStop(request: VNRequest, error: Error?) {
    guard request.completionHandler != nil else {
        print("no result")
        return
    }
}