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

How do I extract specific text from an image using a UITextField in Swift?

我正在使用 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({[=11=] 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
}

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

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{ [=10=].isWhitespace }.map{ String([=10=])}
        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
                }
            }
        }
   }
}