Vistion Kit: VNRecognizeTextRequest minimumTextHeight Has No Effect

311 Views Asked by At

I have a function to do VNRecognizeTextRequest on images from documentCameraViewController

Im passing in the minimumTextHeight to the function but no matter what value i set it to, either 0.9 or .1 it always finds the same amount of text (big and small). Im trying to limit the text so that I can only find headlines or large text.

Why does minimumTextHeight have no effect?

        fileprivate func recognizeText(from images: [CGImage], minimumTextHeight:Float) -> String {
            var entireRecognizedText = ""
            let recognizeTextRequest = VNRecognizeTextRequest { (request, error) in
                guard error == nil else { return }
                
                guard let observations = request.results as? [VNRecognizedTextObservation] else { return }
                
                let maximumRecognitionCandidates = 1
                for observation in observations {
                    guard let candidate = observation.topCandidates(maximumRecognitionCandidates).first else { continue }
                    entireRecognizedText += "\(candidate.string)\n"
                    // show location of text in frame
                    let stringRange = candidate.string.startIndex..<candidate.string.endIndex
                    let boxObservation = try? candidate.boundingBox(for: stringRange)
                    let boundingBox = boxObservation?.boundingBox ?? .zero
                    print(candidate.string, boundingBox.origin.x, boundingBox.origin.y)
                }
            }
            recognizeTextRequest.revision = VNRecognizeTextRequestRevision3
            recognizeTextRequest.recognitionLevel = .accurate
            recognizeTextRequest.minimumTextHeight = minimumTextHeight
            recognizeTextRequest.recognitionLanguages = ["en-US", "en-GB", "de-DE"]
            
            for image in images {
                let requestHandler = VNImageRequestHandler(cgImage: image, options: [:])
                
                try? requestHandler.perform([recognizeTextRequest])
            }
            
            return entireRecognizedText
        }
1

There are 1 best solutions below

0
On BEST ANSWER

Just encountered this issue myself. I've found that if you change recognitionLevel to .fast rather than .accurate is appears to use minimumTextHeight correctly. I've also found that the value is relative tot he size of your regionOfInterest if you're using one, not the whole captured image.