如何在 Swift 中使用 UITextField 从图像中提取特定文本?

xco*_*e22 3 ocr ios swift apple-vision

我正在使用 Vision 框架,我希望能够使用 UITextField 来查找图片中的特定单词。例如,假设我在文本字段中输入“黑色”一词,我希望它在我的图片中检测到该词。我该怎么做呢?我使用 Vision 框架,我找到了如何检测文本,但停留在可以检测用户在文本字段中输入的单词的部分。

        func startTextDetection() {

       let textRequest = VNDetectTextRectanglesRequest(completionHandler: self.detectTextHandler)
       let request = VNRecognizeTextRequest(completionHandler: self.detectTextHandler)

        request.recognitionLevel = .fast
        textRequest.reportCharacterBoxes = true
        self.requests = [textRequest]

    }

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

        let result = observations.map({$0 as? VNTextObservation})

        DispatchQueue.main.async() {
            self.previewView.layer.sublayers?.removeSubrange(1...)
            for region in result {
                guard let rg = region else {
                    continue
                }

                self.highlightWord(box: rg)
                if let boxes = region?.characterBoxes {
                    for characterBox in boxes {
                        self.highlightLetters(box: characterBox)
                }
            }
        }
    }
}

     //when user presses search will search for text in pic. 
func textFieldShouldReturn(_ searchTextField: UITextField) -> Bool {
    searchTextField.resignFirstResponder()
    startTextDetection()

    return true
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您应该观看有关 Vision 框架的最新WWDC。基本上,从 iOS 13 开始,VNRecognizeTextRequest 返回文本以及图像中文本的边界框。代码可以是这样的:

func startTextDetection() {
    let request = VNRecognizeTextRequest(completionHandler: self.detectTextHandler)
    request.recognitionLevel = .fast
    self.requests = [request]
}

private func detectTextHandler(request: VNRequest, error: Error?) {
    guard let observations = request.results as? [VNRecognizedTextObservation] else {
        fatalError("Received invalid observations")
    }
    for lineObservation in observations {
        guard let textLine = lineObservation.topCandidates(1).first else {
            continue
        }

        let words = textLine.string.split{ $0.isWhitespace }.map{ String($0)}
        for word in words {
            if let wordRange = textLine.string.range(of: word) {
                if let rect = try? textLine.boundingBox(for: wordRange)?.boundingBox {
                     // here you can check if word == textField.text
                     // rect is in image coordinate space, normalized with origin in the bottom left corner
                }
            }
        }
   }
}


Run Code Online (Sandbox Code Playgroud)