Skip to content

Instantly share code, notes, and snippets.

@LucasAbijmil
Last active May 11, 2024 12:58
Show Gist options
  • Select an option

  • Save LucasAbijmil/1d28332a28ecd496065720ea77903297 to your computer and use it in GitHub Desktop.

Select an option

Save LucasAbijmil/1d28332a28ecd496065720ea77903297 to your computer and use it in GitHub Desktop.
Face detection & face landmarks with Vision framework
/// - If you want to **only** detect faces in a photo you must use the `VNDetectFaceRectanglesRequest` object.
/// - The `results` property, of type `[VNFaceObservation]?` contains the number of faces detected.
/// - You can also retrieve the confidence for each `VNFaceObservation` by accessing the `confidence` property of type `VNConfidence` which provide a value between 0 and 1.
/// - ⚠️ Even if you retrieved a `VNFaceObservation` object, the `landmarks` property (which provides the facial features) **will always return** `nil` as you use a `VNDetectFaceRectanglesRequest` for the request.
private func detectFaces(in image: CGImage) throws -> [VNFaceObservation]? {
let request = VNDetectFaceRectanglesRequest()
request.revision = VNDetectFaceRectanglesRequestRevision3
let handler = VNImageRequestHandler(cgImage: image)
try handler.perform([request])
return request.results
}
/// - If you want to retrieve the facial features, thanks to the `landmarks` property, **you must use** the `VNDetectFaceLandmarksRequest` object.
/// - With this, you’ll be able to retrieve all the landmarks points on a face. See https://developer.apple.com/documentation/vision/vnfacelandmarks2d).
/// - As the `VNFaceLandmarks2D` inherits from the `VNFaceLandmarks` object, you’ll be able to retrieve a confidence through the `VNConfidence` object (see above).
private func detectLandmarksFaces(in image: CGImage) throws -> [VNFaceObservation]? {
let request = VNDetectFaceLandmarksRequest()
request.revision = VNDetectFaceLandmarksRequestRevision3
request.constellation = .constellation76Points
let handler = VNImageRequestHandler(cgImage: image)
try handler.perform([request])
return request.results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment