Once you add to Instagram, you make your face smaller, proper?
That is the best way to implement a small face filter.
Distorts primarily the facial space.
This lets you step by step cut back the scale of the face whereas retaining the environment as pure as potential.
let request = VNDetectFaceRectanglesRequest()
let handler = VNImageRequestHandler(cgImage: cgImage, choices: [:])
attempt? handler.carry out([request])
guard let outcomes = request.outcomes, let firstFace = outcomes.first else { return nil }
let field = firstFace.boundingBox
let boxInImage = VNImageRectForNormalizedRect(CGRect(x: field.minX, y: field.minY, width: field.width, top: field.top), cgImage.width, cgImage.top)
That is the realm of the face within the picture. Apply a filter known as CIBumpDistortion
to this space .
let boxCenter = CGPoint(x: boxInImage!.minX+boxInImage!.width/2, y: boxInImage!.minY+boxInImage!.top/2)let radius = boxInImage!.top * 1.7
let filter = CIFilter(title: "CIBumpDistortion", parameters: [kCIInputImageKey:ciImage,
kCIInputCenterKey:CIVector(cgPoint: boxCenter),
kCIInputRadiusKey:NSNumber(floatLiteral: radius),
kCIInputScaleKey:NSNumber(floatLiteral: -Double(scale))
])?.outputImage!
let cropped = filter?.cropped(to: ciImage.extent)
This provides you with a picture the place solely the face is distorted and shrunken.
If the radius exceeds the sting of the picture, your entire picture space will turn into smaller, so it’s cropped on the finish.