2

In the WWDC 2017 video from session 506. There's a piece of the code in the 1st demo that looks like this:

let exifOrientation = self.exifOrientationFromDeviceOrientation()

The use of self. indicates that it is supposed to be a property from the ViewController. Given that this exifOrientationFromDeviceOrientation() method doesn't exist in a UIViewController class, I suppose ViewController is conforming to a protocol that grants that functionality. Can someone indicate which protocol that is?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Andre Guerra
  • 1,117
  • 1
  • 9
  • 18

4 Answers4

0

I strongly recommend you take a look at https://github.com/yulingtianxia/Core-ML-Sample/blob/master/CoreMLSample/ViewController.swift which is part of the Core-ML-Sample project on GitHub.

var exifOrientationFromDeviceOrientation: Int32 {
    let exifOrientation: DeviceOrientation
    enum DeviceOrientation: Int32 {
        case top0ColLeft = 1
        case top0ColRight = 2
        case bottom0ColRight = 3
        case bottom0ColLeft = 4
        case left0ColTop = 5
        case right0ColTop = 6
        case right0ColBottom = 7
        case left0ColBottom = 8
    }
    switch UIDevice.current.orientation {
    case .portraitUpsideDown:
        exifOrientation = .left0ColBottom
    case .landscapeLeft:
        exifOrientation = .top0ColLeft
    case .landscapeRight:
        exifOrientation = .bottom0ColRight
    default:
        exifOrientation = .right0ColTop
    }
    return exifOrientation.rawValue
}

Cheers!

ekscrypto
  • 3,718
  • 1
  • 24
  • 38
  • I am working on object localization and I think this Exif related method for grabbing the orientation may be wrong. One should follow WWDC sample code from https://developer.apple.com/documentation/vision/training_a_create_ml_model_to_classify_flowers. I used the method in this project and things worked as expected. – kawingkelvin Nov 05 '18 at 00:31
  • Further, I am on iOS 12.0 and apple's own demo source code should be trusted more than GitHub from someone else, holding all else equal – kawingkelvin Nov 05 '18 at 00:32
  • @kawingkelvin exifOrientation is the implementer's own variable definition; the actual values are defined here: https://developer.apple.com/documentation/uikit/uideviceorientation – ekscrypto Nov 06 '18 at 11:49
0

It's worse than that. It refers to a function that is in their app's subclass of UIViewController(). I could not find a copy of this app anywhere in the WWDC samples -- I don't think that they included it.

But, this project seems to have an equivalent:

https://github.com/yulingtianxia/Core-ML-Sample/blob/master/CoreMLSample/ViewController.swift#L107

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

You are referring to this code posted here:

They create the variable in code:

var exifOrientationFromDeviceOrientation: Int32 {
    let exifOrientation: DeviceOrientation
    enum DeviceOrientation: Int32 {
        case top0ColLeft = 1
        case top0ColRight = 2
        case bottom0ColRight = 3
        case bottom0ColLeft = 4
        case left0ColTop = 5
        case right0ColTop = 6
        case right0ColBottom = 7
        case left0ColBottom = 8
    }
    switch UIDevice.current.orientation {
    case .portraitUpsideDown:
        exifOrientation = .left0ColBottom
    case .landscapeLeft:
        exifOrientation = .top0ColLeft
    case .landscapeRight:
        exifOrientation = .bottom0ColRight
    default:
        exifOrientation = .right0ColTop
    }
    return exifOrientation.rawValue
}
CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • Thanks for your response, CodeBender. Appreciate if you could point me to a location where I can find the full code. Best regards. – Andre Guerra Sep 25 '17 at 20:38
  • @AndreGuerra I posted the link. You can find it on "posted here:" in my answer. – CodeBender Sep 25 '17 at 20:39
  • o.O - My bad. Hadn't noticed it before. Thks. – Andre Guerra Sep 26 '17 at 00:53
  • Can you explain why is the default value returning the equivalent of a 6? I've looked it up on Apple's documentation and 6 corresponds to a CGImagePropertyOrientation.**right**. It doesn't make sense to enumerate 8 different DeviceOrientation and use only 4 of them right? Somehow I've applied this missing piece to my Face recognition demo and it worked, just can't explain why. – Andre Guerra Oct 04 '17 at 19:21
  • @AndreGuerra Venturing a guess that it is to handle mirrored images, but then is not accounted for in the switch below? – CodeBender Oct 04 '17 at 19:25
  • It still doesn't explain why a regular portrait orientation on the device returns an exif orientation of right0ColTop (equivalent to 6). It does cause the FaceRecognition to work, but the reason is unknown to me. – Andre Guerra Oct 04 '17 at 19:30
0

Please look at "Training a Create ML Model to Classify Flowers" from Apple. The related code should work for iOS 12.x.

I have used the code posted in this thread without realizing they are not (or no longer) correct. I detected this only when I started testing an image localization model which draws a bounding box as well as classification. So if you have the same trouble, you can try change the code.

I am going back to my classification model to see if the accuracy improves. My intended object has rotational invariance so I don't expect this to change much. But for all models that have a clear orientation, your accuracy may drop, if you don't do the orientation correctly.

kawingkelvin
  • 3,649
  • 2
  • 30
  • 50