Trouble understanding ImageOrientation in Swift

4 weeks ago 30
ARTICLE AD BOX

I am fairly new to swift (SwiftUI) and making a simple face and card detection app using Vision. Basically it’s just a camera preview layer and upon face/card detection, it crops the image and displays it on the view. I am having some problems understanding how image orientation works.

This is my understanding, correct me if I’m wrong:

In iOS devices, when we hold the camera in portrait orientation, the native sensor still captures the image in landscape left (so we have to rotate the image 90 deg right for intended orientation) for the back camera. For front camera, it still captures in landscape left but additionally it is mirrored too.

Suppose this sample image from web

enter image description here

I tried two approaches:

I-) In the VNImageRequestHandler, I set the orientation to .right so the captured buffers would rotate to right but they didn’t. I thought this would work.

enter image description here

II-) In the camera configuration, I set the videoOrientation to AVCaptureVideoOrientation.portrait, this corrected the capture orientation of the buffers.

Now acc to my understanding, if I set the orientation to .up(default) in VNImageRequestHandler, it should work right? The captured buffer was in correct orientation but the crop was not applying correctly.

enter image description here

But when I set the orientation to .downMirrored in request handler, it worked correctly, the buffers were in correct orientation and the crop was good too.

enter image description here

Why does .downMirrored (upside down and horizontal flip) work and not the Ist approach?

Read Entire Article