Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import Foundation
  2. import Vision
  3. import VisionKit
  4.  
  5. @available(iOS 13.0, *)
  6. class TextRecognitionEngine {
  7.  
  8. private var requests = [VNRequest]()
  9. private let textRecognitionWorkQueue = DispatchQueue(label: "TextRecognitionQueue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem)
  10. private var resultingText = ""
  11.  
  12. init(recognitionLevel: VNRequestTextRecognitionLevel = .accurate, languageCorrection: Bool = true, customWords: [String] = []) {
  13. let textRecognitionRequest = VNRecognizeTextRequest { request, error in
  14. guard let observations = request.results as? [VNRecognizedTextObservation] else {
  15. print("The observations are of an unexpected type.")
  16. return
  17. }
  18. // Concatenate the recognised text from all the observations.
  19. for observation in observations {
  20. guard let candidate = observation.topCandidates(1).first else { continue }
  21. self.resultingText += candidate.string + "\n"
  22. }
  23. }
  24. textRecognitionRequest.recognitionLevel = recognitionLevel
  25. textRecognitionRequest.usesLanguageCorrection = languageCorrection
  26. textRecognitionRequest.customWords = customWords
  27. requests = [textRecognitionRequest]
  28. }
  29.  
  30. func process(_ scan: VNDocumentCameraScan, completion: @escaping ((String?) -> Void)) {
  31. let images = scan.images()
  32. let handlers = images.map { VNImageRequestHandler(cgImage: $0, options: [:]) }
  33. performRequest(on: handlers, completion: completion)
  34. }
  35.  
  36. func process(_ image: CGImage, completion: @escaping ((String?) -> Void)) {
  37. let requestHandler = VNImageRequestHandler(cgImage: image, options: [:])
  38. performRequest(on: [requestHandler], completion: completion)
  39. }
  40.  
  41. func process(_ url: URL, completion: @escaping ((String?) -> Void)) {
  42. let requestHandler = VNImageRequestHandler(url: url, options: [:])
  43. performRequest(on: [requestHandler], completion: completion)
  44. }
  45.  
  46. }
  47.  
  48.  
  49. // MARK: - Private
  50.  
  51. @available(iOS 13.0, *)
  52. private extension TextRecognitionEngine {
  53.  
  54. func performRequest(on handlers: [VNImageRequestHandler], completion: @escaping ((String?) -> Void)) {
  55. resultingText = ""
  56. textRecognitionWorkQueue.async {
  57. for handler in handlers {
  58. do {
  59. try handler.perform(self.requests)
  60. } catch {
  61. dump(error)
  62. }
  63. self.resultingText += "\n\n"
  64. }
  65. completion(self.resultingText)
  66. }
  67. }
  68.  
  69. }
  70.  
  71.  
  72. // MARK: - VNDocumentCameraScan
  73. @available(iOS 13.0, *)
  74. extension VNDocumentCameraScan {
  75.  
  76. func images() -> [CGImage] {
  77. var allImages = [UIImage]()
  78. for pageIndex in 0 ..< pageCount {
  79. allImages.append(imageOfPage(at: pageIndex))
  80. }
  81. return allImages.compactMap { $0.cgImage }
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement