0

I am building a document scanning app with OCR using VisionKit. However when I use any of my document scans as the input, the text just comes out gibberish. I tried converting the input image to jpeg as well as png, but still yields the same result. While debugging I tried taking a screenshot of one of my scans and then using that image as the input image. The OCR worked perfectly on that which is confusing me. Any insight into this issue would be appreciated.

let origImage = UIImage(data: scan.originalscan!)! // in jpeg

guard let cgImage = origImage?.cgImage else {
   fatalError("could not get cgimage")
}

let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
    
var textString = ""
    
    let request = VNRecognizeTextRequest { [weak self] request, error in
        guard let observations = request.results as? [VNRecognizedTextObservation],
            error == nil else {
            print("Failed!")
            //self?.copyTV.text = "Failed"
            return
        }
        for observation in observations {
            guard let topCandidate = observation.topCandidates(1).first else {
                print("no candidate");
                continue
            }
            textString += "\n\(topCandidate.string)"   
        }
        
        DispatchQueue.main.async {
            self?.copyTV.text = textString
        }
    }
    
    request.customWords = ["custOm"]
    request.recognitionLevel = .accurate
    request.recognitionLanguages = ["en_US"]
    request.usesLanguageCorrection = true
    
    do {
        try handler.perform([request])
    } catch {
        print(error)
    }
Tyler Rutt
  • 567
  • 2
  • 8
  • 24
  • Could be a problem with the image orientation – aheze Aug 23 '21 at 18:32
  • 1
    Hi Tyler, did you find a solution to this? Having the same issue where the ocr works on the screenshot of an image but not the image itself. – Krish Wadhwana Jun 30 '22 at 22:05
  • 1
    @KrishWadhwana I did. Specifically for me, I was saving the images to the device using jpeg compression, and then loading them back for use, for reading OCR data off of. It turns out I was using too much compression (file too small), and as soon I lowered the compression, it worked perfectly. – Tyler Rutt Jul 01 '22 at 17:15
  • 1
    @TylerRutt thanks for the response. Will try that out. – Krish Wadhwana Jul 17 '22 at 19:02

0 Answers0