Basically Im using the vision framework to find keywords the user searches for in the UITextField but if there is more than one of the same words on a given line it only detects the first word. For example lets say I have an image and the text has "in in in in in in" only the first "in" will get detected and the rest won't. Only way it will detect the other words if they are on separate lines like on the right side of the image. Why does this happen?
Here is a pic explaining the problem and my code:
func detectTextHandler(request: VNRequest, error: Error?) {
guard let results = request.results as? [VNRecognizedTextObservation] else {
return
}
DispatchQueue.main.async {
self.previewView.layer.sublayers?.removeSubrange(1...)
for visionResult in results {
guard let candidate = visionResult.topCandidates(1).first else {
continue
}
let words = candidate.string.split{ $0.isWhitespace }.map{ String($0)}
for word in words {
if let wordRange = candidate.string.range(of: word, options: .caseInsensitive),
let boxObservation = try? candidate.boundingBox(for: wordRange) {
if self.searchTextField.text == word
{
print(word)
self.highlightLetters(box: boxObservation)//highlights words
}
}
}
}
}
}