Skip to content

Instantly share code, notes, and snippets.

@codePrincess
Created August 3, 2017 14:53
Show Gist options
  • Save codePrincess/4cea0818217188972e94d5902949da44 to your computer and use it in GitHub Desktop.
Save codePrincess/4cea0818217188972e94d5902949da44 to your computer and use it in GitHub Desktop.
get text on image for handwriting
func fetchOCRText () {
let manager = CognitiveServices()
let ocrImage = image!.crop(rect: ocrImageRect!)
manager.retrieveTextOnImage(ocrImage) {
operationURL, error in
if #available(iOS 10.0, *) {
guard let _ = operationURL else {
print("Seems like the network call failed - did you enter the Computer Vision Key in CognitiveServices.swift in line 69? :)")
return
}
let when = DispatchTime.now() + 2
DispatchQueue.main.asyncAfter(deadline: when) {
manager.retrieveResultForOcrOperation(operationURL!, completion: {
results, error -> (Void) in
if let theResult = results {
var ocrText = ""
for result in theResult {
ocrText = "\(ocrText) \(result)"
}
self.addLabelForOCR(text: ocrText)
} else {
self.addLabelForOCR(text: "No text for writing")
}
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment