Skip to content

Instantly share code, notes, and snippets.

@craigmarvelley
Created August 10, 2021 16:21
Show Gist options
  • Save craigmarvelley/10cee505555c68767b8a0cb6a51c764a to your computer and use it in GitHub Desktop.
Save craigmarvelley/10cee505555c68767b8a0cb6a51c764a to your computer and use it in GitHub Desktop.
class DocumentOCRRequestProxy: OCRRequestProxy {
private lazy var ocrRequest: VNRecognizeTextRequest = VNRecognizeTextRequest(completionHandler: ocrRequestHandler)
private let requestHandler: VNImageRequestHandler
private weak var ocrRequestDelegate: OCRRequestDelegate?
private let cgImage: CGImage
init(cgImage: CGImage) {
self.cgImage = cgImage
requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:])
ocrRequest.recognitionLevel = .accurate
ocrRequest.recognitionLanguages = ["en-US", "en-GB"]
ocrRequest.usesLanguageCorrection = true
ocrRequest.revision = VNRecognizeTextRequestRevision1
ocrRequest.progressHandler = progressUpdateHandler
}
private func progressUpdateHandler(request: VNRequest, fractionCompleted: Double, error: Error?) {
ocrRequestDelegate?.progressed(fractionCompleted: fractionCompleted)
}
func performOCR() {
do {
try requestHandler.perform([self.ocrRequest])
} catch {
ocrRequestDelegate?.ocrCompleted(result: .failure(OCRError.failedToPerformOCR))
}
}
func setOCRRequestDelegate(_ delegate: OCRRequestDelegate) {
self.ocrRequestDelegate = delegate
}
private func ocrRequestHandler(request: VNRequest, error: Error?) {
....
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment