0

I want to use this code to detect faces in an image via the CIFaceDetector.

    let Orientation: Int!

    switch ((info[UIImagePickerControllerOriginalImage] as? UIImage)?.imageOrientation)! {
    case UIImageOrientation.up:
        Orientation = 1
    case UIImageOrientation.down:
        Orientation = 3
    case UIImageOrientation.left:
        Orientation = 8
    case UIImageOrientation.right:
        Orientation = 6
    case UIImageOrientation.upMirrored:
        Orientation = 2
    case UIImageOrientation.downMirrored:
        Orientation = 4
    case UIImageOrientation.leftMirrored:
        Orientation = 5
    case UIImageOrientation.rightMirrored:
        Orientation = 7
    }

    let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorImageOrientation: Orientation] as [String : Any]
    let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)
    let faces = faceDetector?.features(in: personciImage)

However this only works in landscape right. I have no idea why it does not work in all the other orientations. I mean the orientation should be set right. I am also casting an UIImage to an CIImage.

 guard let personciImage = CIImage(image: (info[UIImagePickerControllerOriginalImage] as? UIImage)!) else {
        return
    }

Is the problem there?

Lenny1357
  • 748
  • 1
  • 13
  • 21

1 Answers1

0

From the CIDetector configuration docs here it seems like the orientation key is not actually an option for this class. It is likely that the face detection class actually prefers for YOU to rotate the image before passing it in.

Rotating an image is fairly simple, and can be searched on SO as well, I've found a quick example, but I'm sure there are more! quick rotation search example

BHendricks
  • 4,423
  • 6
  • 32
  • 59