Last active
July 11, 2016 18:24
-
-
Save isoiphone/c694c3c3b38aa03971f2d4053c0bb817 to your computer and use it in GitHub Desktop.
Using CoreImage to detect faces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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