Skip to content

How to ensure correct image orientation? #203

@cdeil

Description

@cdeil

I saw getCorrectOrientationUIImage here:

/// Corrects the orientation of an image to ensure proper processing by the YOLO model.
///
/// Images from the photo library might have orientation metadata rather than correctly oriented pixels.
/// This function ensures the image is properly oriented for processing by the YOLO model.
///
/// - Parameter uiImage: The input image that may have incorrect orientation metadata.
/// - Returns: A new UIImage with the correct orientation for processing.
func getCorrectOrientationUIImage(uiImage: UIImage) -> UIImage {
var newImage = UIImage()
let ciContext = CIContext()
switch uiImage.imageOrientation.rawValue {
case 1:
guard let orientedCIImage = CIImage(image: uiImage)?.oriented(CGImagePropertyOrientation.down),
let cgImage = ciContext.createCGImage(orientedCIImage, from: orientedCIImage.extent)
else { return uiImage }
newImage = UIImage(cgImage: cgImage)
case 3:
guard let orientedCIImage = CIImage(image: uiImage)?.oriented(CGImagePropertyOrientation.right),
let cgImage = ciContext.createCGImage(orientedCIImage, from: orientedCIImage.extent)
else { return uiImage }
newImage = UIImage(cgImage: cgImage)
default:
newImage = uiImage
}
return newImage
}

Now I'm wondering when it DO or DON'T have to call that before passing an image to YOLO predict!?

Is it "safe" to always call it or can that screw up orientation?

Is there some way I can tell if my image has correct orientation?

Would it make sense that you move that function into the package for easier re-use?
(currently I copy & paste it into my project)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requestedsolutionsUltralytics Solutions issues, PR's, Discussions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions