Skip to content

Instantly share code, notes, and snippets.

@isoiphone
Last active July 11, 2016 18:24
Show Gist options
  • Save isoiphone/c694c3c3b38aa03971f2d4053c0bb817 to your computer and use it in GitHub Desktop.
Save isoiphone/c694c3c3b38aa03971f2d4053c0bb817 to your computer and use it in GitHub Desktop.
Using CoreImage to detect faces
import CoreImage
extension UIImage {
func rectsForFaces() -> [CGRect] {
guard let cgImage = CGImage else { return [] }
let ciImage = CoreImage.CIImage(CGImage: cgImage)
let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)
let hallOfFaces = detector.featuresInImage(ciImage)
return hallOfFaces.flatMap { feature in
guard feature.type == CIFeatureTypeFace else { return nil }
return feature.bounds
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment