-
-
Notifications
You must be signed in to change notification settings - Fork 77
Fix: image orientation #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,25 +52,35 @@ struct YOLOSingleImageSwiftUITests { | |
| let correctedImage1 = getCorrectOrientationUIImage(uiImage: originalImage) | ||
| #expect(correctedImage1.size.width == originalImage.size.width) | ||
| #expect(correctedImage1.size.height == originalImage.size.height) | ||
| #expect(correctedImage1.imageOrientation == .up, "Normalized image should have .up orientation") | ||
|
|
||
| // Create image with orientation = .down (1) | ||
| let imageDown = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .down) | ||
| let correctedImageDown = getCorrectOrientationUIImage(uiImage: imageDown) | ||
|
|
||
| // Check if the function returns a valid image | ||
| #expect(correctedImageDown != nil, "Corrected image should not be nil") | ||
| #expect(correctedImageDown.imageOrientation == .up, "Normalized image should have .up orientation") | ||
|
||
|
|
||
| // Create image with orientation = .left (3) | ||
| let imageLeft = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .left) | ||
| let correctedImageLeft = getCorrectOrientationUIImage(uiImage: imageLeft) | ||
|
|
||
| // Check if the function returns a valid image | ||
| #expect(correctedImageLeft != nil, "Corrected image should not be nil") | ||
|
|
||
| // For left orientation specifically, we'll verify the image data is handled correctly | ||
| // by checking the image isn't nil since we can't reliably check orientation property changes | ||
| #expect(correctedImageLeft.imageOrientation == .up, "Normalized image should have .up orientation") | ||
| #expect(correctedImageLeft.size.width > 0, "Image width should be positive") | ||
| #expect(correctedImageLeft.size.height > 0, "Image height should be positive") | ||
|
|
||
|
|
||
| let orientations: [UIImage.Orientation] = [.up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored] | ||
| for orientation in orientations { | ||
| let testImage = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: orientation) | ||
| let normalized = getCorrectOrientationUIImage(uiImage: testImage) | ||
| #expect(normalized.imageOrientation == .up, "All orientations should normalize to .up") | ||
| #expect(normalized.size.width > 0, "Normalized image should have valid width") | ||
| #expect(normalized.size.height > 0, "Normalized image should have valid height") | ||
| } | ||
| } | ||
|
|
||
| /// Tests the YOLO model initialization. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,26 +89,14 @@ class ViewController: UIViewController, PHPickerViewControllerDelegate { | |
| /// - Parameter uiImage: The input image that may have incorrect orientation metadata. | ||
| /// - Returns: A 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 } | ||
| guard uiImage.imageOrientation != .up 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 | ||
| UIGraphicsBeginImageContextWithOptions(uiImage.size, false, uiImage.scale) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 MEDIUM: Same feedback here—using |
||
| uiImage.draw(in: CGRect(origin: .zero, size: uiImage.size)) | ||
| let normalizedImage = UIGraphicsGetImageFromCurrentImageContext() | ||
| UIGraphicsEndImageContext() | ||
|
|
||
| return normalizedImage ?? uiImage | ||
| } | ||
|
|
||
| /// Sets up the UI components including the image view and pick button. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,25 +50,34 @@ struct YOLOSingleImageUIKitTests { | |
| let correctedImage1 = viewController.getCorrectOrientationUIImage(uiImage: originalImage) | ||
| #expect(correctedImage1.size.width == originalImage.size.width) | ||
| #expect(correctedImage1.size.height == originalImage.size.height) | ||
| #expect(correctedImage1.imageOrientation == .up, "Normalized image should have .up orientation") | ||
|
|
||
| // Create image with orientation = .down (1) | ||
| let imageDown = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .down) | ||
| let correctedImageDown = viewController.getCorrectOrientationUIImage(uiImage: imageDown) | ||
|
|
||
| // Check if the function returns a valid image | ||
| #expect(correctedImageDown != nil, "Corrected image should not be nil") | ||
| #expect(correctedImageDown.imageOrientation == .up, "Normalized image should have .up orientation") | ||
|
||
|
|
||
| // Create image with orientation = .left (3) | ||
| let imageLeft = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: .left) | ||
| let correctedImageLeft = viewController.getCorrectOrientationUIImage(uiImage: imageLeft) | ||
|
|
||
| // Check if the function returns a valid image | ||
| #expect(correctedImageLeft != nil, "Corrected image should not be nil") | ||
|
|
||
| // For left orientation specifically, we'll verify the image data is handled correctly | ||
| // by checking the image isn't nil since we can't reliably check orientation property changes | ||
| #expect(correctedImageLeft.imageOrientation == .up, "Normalized image should have .up orientation") | ||
| #expect(correctedImageLeft.size.width > 0, "Image width should be positive") | ||
| #expect(correctedImageLeft.size.height > 0, "Image height should be positive") | ||
|
|
||
| let orientations: [UIImage.Orientation] = [.up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored] | ||
| for orientation in orientations { | ||
| let testImage = UIImage(cgImage: originalImage.cgImage!, scale: 1.0, orientation: orientation) | ||
| let normalized = viewController.getCorrectOrientationUIImage(uiImage: testImage) | ||
| #expect(normalized.imageOrientation == .up, "All orientations should normalize to .up") | ||
| #expect(normalized.size.width > 0, "Normalized image should have valid width") | ||
| #expect(normalized.size.height > 0, "Normalized image should have valid height") | ||
| } | ||
| } | ||
|
|
||
| /// Tests the basic view controller properties without accessing private methods. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 MEDIUM: Nice simplification! One small request: could we wrap the drawing block with
defer { UIGraphicsEndImageContext() }? That guarantees the context is torn down even if we add another exit path or the draw routine starts returning early, and it keeps us safe from leaks if this helper evolves.