Search code examples
iosswiftxcodecamera

Image difference from VNDocumentCameraViewController and AVCaptureStillImageOutput?


Friends, please tell me what is the difference between images obtained from VNDocumentCameraViewController and AVCaptureStillImageOutput.

I use it for text recognition using VNRecognizeTextRequest, and when I get an image from VNDocumentCameraViewController, the text is recognized perfectly, and when I just take a photo from AVCaptureStillImageOutput, the text does not want to be recognized. Thanks a lot.


Solution

  • I had the same problem and in my case passing the orientation to VNImageRequestHandler solved it.

    You can get an UIImage.Orientation from an UIImage by using the imageOrientation property.

    VNImageRequestHandler expects a CGImagePropertyOrientation so you'll have to convert them with something like:

    func convert(orientation: UIImage.Orientation) -> CGImagePropertyOrientation {
        switch orientation {
        case .up: return .up
        case .upMirrored: return .upMirrored
        case .down: return .down
        case .downMirrored: return .downMirrored
        case .left: return .left
        case .leftMirrored: return .leftMirrored
        case .right: return .right
        case .rightMirrored: return .rightMirrored
        @unknown default: return .up
        }
    }
    

    Also AVCaptureStillImageOutput is deprecated you should use AVCapturePhotoOutput if you can.