Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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