Last active
November 8, 2021 09:07
-
-
Save twinsant/974dbb9826641bbc4044e68197d00cc4 to your computer and use it in GitHub Desktop.
Text OcR
This file contains hidden or 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
| // Hacked from: https://stackoverflow.com/questions/44533148/converting-a-vision-vntextobservation-to-a-string | |
| // Ref: https://developer.apple.com/videos/play/wwdc2019/234/ | |
| import Vision | |
| import UIKit | |
| class OCRReader { | |
| func performOCR(recognitionLevel: VNRequestTextRecognitionLevel) { | |
| let image = #imageLiteral(resourceName: "Hello.JPG") | |
| let requestHandler = VNImageRequestHandler(cgImage: image.cgImage!, options: [:]) | |
| let request = VNRecognizeTextRequest { (request, error) in | |
| if let error = error { | |
| print(error) | |
| return | |
| } | |
| guard let observations = request.results as? [VNRecognizedTextObservation] else { return } | |
| for currentObservation in observations { | |
| let topCandidate = currentObservation.topCandidates(1) | |
| if let recognizedText = topCandidate.first { | |
| print("Result: ", recognizedText.string) | |
| } | |
| } | |
| } | |
| request.recognitionLevel = recognitionLevel | |
| try? requestHandler.perform([request]) | |
| } | |
| } | |
| let revision = VNRecognizeTextRequest.currentRevision | |
| let supported = try? VNRecognizeTextRequest.supportedRecognitionLanguages(for: VNRequestTextRecognitionLevel.fast, revision: 2) | |
| print(supported) | |
| print("Running...😆") | |
| let ocrReader = OCRReader() | |
| ocrReader.performOCR(recognitionLevel: .accurate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment