停止在Apple Vision,iOS,Swift中检测字母

时间:2017-09-05 20:47:32

标签: ios swift text-recognition apple-vision

我正在使用Vision检测文本并且它正常工作我在检测到某些内容时使用完成并调用函数,但文本仍在检测中。

如何停止文本检测

启动它我正在使用:

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 个答案:

答案 0 :(得分:0)

我能够通过添加以下功能来解决这个问题,并在达到检测时调用它。

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
    }
}