Created
August 10, 2021 16:21
-
-
Save craigmarvelley/10cee505555c68767b8a0cb6a51c764a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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