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)
}