Last active
June 30, 2021 05:10
-
-
Save PJayRushton/eb83eef1b2c2e509636082e29228ace0 to your computer and use it in GitHub Desktop.
iOS 13 Vision Swift Text Detection
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
import Foundation | |
import Vision | |
import VisionKit | |
@available(iOS 13.0, *) | |
class TextRecognitionEngine { | |
private var requests = [VNRequest]() | |
private let textRecognitionWorkQueue = DispatchQueue(label: "TextRecognitionQueue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem) | |
private var resultingText = "" | |
init(recognitionLevel: VNRequestTextRecognitionLevel = .accurate, languageCorrection: Bool = true, customWords: [String] = []) { | |
let textRecognitionRequest = VNRecognizeTextRequest { request, error in | |
guard let observations = request.results as? [VNRecognizedTextObservation] else { | |
print("The observations are of an unexpected type.") | |
return | |
} | |
// Concatenate the recognised text from all the observations. | |
for observation in observations { | |
guard let candidate = observation.topCandidates(1).first else { continue } | |
self.resultingText += candidate.string + "\n" | |
} | |
} | |
textRecognitionRequest.recognitionLevel = recognitionLevel | |
textRecognitionRequest.usesLanguageCorrection = languageCorrection | |
textRecognitionRequest.customWords = customWords | |
requests = [textRecognitionRequest] | |
} | |
func process(_ scan: VNDocumentCameraScan, completion: @escaping ((String?) -> Void)) { | |
let images = scan.images() | |
let handlers = images.map { VNImageRequestHandler(cgImage: $0, options: [:]) } | |
performRequest(on: handlers, completion: completion) | |
} | |
func process(_ image: CGImage, completion: @escaping ((String?) -> Void)) { | |
let requestHandler = VNImageRequestHandler(cgImage: image, options: [:]) | |
performRequest(on: [requestHandler], completion: completion) | |
} | |
func process(_ url: URL, completion: @escaping ((String?) -> Void)) { | |
let requestHandler = VNImageRequestHandler(url: url, options: [:]) | |
performRequest(on: [requestHandler], completion: completion) | |
} | |
} | |
// MARK: - Private | |
@available(iOS 13.0, *) | |
private extension TextRecognitionEngine { | |
func performRequest(on handlers: [VNImageRequestHandler], completion: @escaping ((String?) -> Void)) { | |
resultingText = "" | |
textRecognitionWorkQueue.async { | |
for handler in handlers { | |
do { | |
try handler.perform(self.requests) | |
} catch { | |
dump(error) | |
} | |
self.resultingText += "\n\n" | |
} | |
completion(self.resultingText) | |
} | |
} | |
} | |
// MARK: - VNDocumentCameraScan | |
@available(iOS 13.0, *) | |
extension VNDocumentCameraScan { | |
func images() -> [CGImage] { | |
var allImages = [UIImage]() | |
for pageIndex in 0 ..< pageCount { | |
allImages.append(imageOfPage(at: pageIndex)) | |
} | |
return allImages.compactMap { $0.cgImage } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment