I have photo with face on it.
I have carnival mask:
With this function I detect the face:
let ciImage = CIImage(cgImage: photo)
let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)!
let faces = faceDetector.features(in: ciImage)
if let face = faces.first as? CIFaceFeature {
}
How to detect the holes in the mask ?
How I can place the mask on the face after detecting the holes of the mask?
I'd probably try this approach:
Get leftEyePosition, rightEyePosition, and the faceAngle value. (All part of CIFaceFeature)
Calculate the distance in between the left and right eye.
Here is a link on how to calculate the distance: https://www.hackingwithswift.com/example-code/core-graphics/how-to-calculate-the-distance-between-two-cgpoints
Create constants with the original dimensions of the mask as well as the x and y distance to the center of one of the eyes.
With the distance of the eyes you calculate the new width of your mask proportionally.
That should get you a mask with the right size. Also calculate the new x and y distances to the center of one of eyes of the mask the same way.
Adjust all values proportionally again to fit the final intended size on the screen.
Place the mask on the photo using the coordinates of the eyes, offsetting by the mask eye to corner distance.
Use the faceAngle value to rotate the mask.
Before importing the mask into the project, convert it to a png with transparent background, remove the white background. You could do that in code, but that would be a lot of work and depending on the masks source file it might not turn out as well.
UPDATE, I've tried my solution. It's a simple iOS one screen app, just copy the code into the ViewController.swift file, add your mask as png and a photo of a face as photo.jpg into the project and it should work.
Here is a link to your photo as png if you want to try:
QPTF1.png
Result:
Here is my uncommented approach to scan for the eyes. It has still some quirks but should be a starting point.