Text recognition from image with Vision
private func recognizeText(image: UIImage?) {
guard let cgImage = image?.cgImage else {
print("fatal error could not cgimage")
return
}
//Handler
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
//Request
let request = VNRecognizeTextRequest { [weak self] request, error in
guard let observations = request.results as? [VNRecognizedTextObservation], error == nil else{
return
}
let text = observations.compactMap({
$0.topCandidates(1).first?.string
}).joined(separator: ", ")
DispatchQueue.main.async {
self?.label.text = text
}
}
//Process Request
do{
try handler.perform([request])
}catch{
print(error.localizedDescription)
}
}
@objc func openCamera(sender: UIButton!){
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
// The Xcode simulator does not support taking pictures, so let's first check that the camera is indeed supported on the device before trying to present it.
if UIImagePickerController.isSourceTypeAvailable(.camera) {
print("Camera is available")
picker.sourceType = .camera
} else {
print("Camera is not available so we will use photo library instead")
picker.sourceType = .photoLibrary
}
self.present(picker, animated: true, completion: nil)
}