-
-
Save duncangh/ef2920425a0e466eacc517a53d64f003 to your computer and use it in GitHub Desktop.
iOS 11 Vision. Face detection
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 Vision | |
//1 | |
let sourceImage = UIImage(named: "jony.jpg") | |
var resultImage = sourceImage | |
//2 | |
let detectFaceRequest = VNDetectFaceLandmarksRequest { (request, error) in | |
//4 | |
if let results = request.results as? [VNFaceObservation] { | |
//5 | |
for faceObservation in results { | |
//6 | |
guard let landmarks = faceObservation.landmarks else { | |
continue | |
} | |
let boundingRect = faceObservation.boundingBox | |
var landmarkRegions: [VNFaceLandmarkRegion2D] = [] | |
//7 | |
if let faceContour = landmarks.faceContour { | |
landmarkRegions.append(faceContour) | |
} | |
//8 | |
resultImage = self.drawOnImage(source: resultImage, boundingRect: boundingRect, faceLandmarkRegions: landmarkRegions) | |
} | |
} | |
} | |
//3 | |
let vnImage = VNImageRequestHandler(cgImage: sourceImage.cgImage!, options: [:]) | |
try? vnImage.perform([detectFaceRequest]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment