VNRecognizeTextRequest unable to recognise text scanned in landscape mode

279 views Asked by At

I'm using VNDocumentCameraViewController to scan document and VNRecognizeTextRequest to detect the text. It works only if the document is scanned in portrait mode. It landscape mode it fails. I could pass the orientation in VNImageRequestHandler but that would fail for portrait scans. Again, if I try to check the scanned image orientation it's always .up. So I can't even manipulate the image. Anybody has any idea how to fix this orientation issue? I'm using below code.

@IBAction func scanTapped(_ sender: UIButton) {
    allComponents = [Component]()
    let documentCameraViewController = VNDocumentCameraViewController()

    documentCameraViewController.delegate = self
    self.present(documentCameraViewController, animated: true, completion: nil)
}

func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
    let image = scan.imageOfPage(at: scan.pageCount-1)


    let handler = VNImageRequestHandler(cgImage: image.cgImage!, options: [:])
    do {
        try handler.perform([textRecognitionRequest])
    } catch {
        print(error)
    }
    controller.dismiss(animated: true)
}
1

There are 1 answers

0
Paki On

I had the same problem. It was solved when I've enabled landscape device orientation.