iOS AI相机App开发教程,02 实现瘦脸功能

瘦脸功能实现原理

瘦脸

人脸检测

首先需要对照片进行人脸检测,检测出人脸轮廓位置。人脸检测的方法非常多。在“魔法相机”中使用了 iOS Vision 库中的 VNDetectFaceLandmarksRequest 方法来进行人脸检测。使用这个方法可以充分利用iPhone的AI能力,并且检测效果也非常好。

MLS图像变形算法

MLS变形算法又叫做移动最小二乘变形算法,该算法引用量较高,效果相对较好,在变形过程中几乎毫无违和感,这也是MLS的优势所在。

MLS变形算法在美颜中的人脸变形特效或者图像变形特效中有着较多的应用,比如“天天P图”中的疯狂换脸特效,就是以MLS变形为基础开发设计的。”魔法相机“的瘦脸功能和变老功能也使用了MLS算法。

实现原理

使用人脸检测获得人脸眼睛到下巴的轮廓点,并在图片边缘进行锚点,然后通过MLS变形,将人像下半部分的脸和下巴的锚点做适当的收缩。

代码讲解

// 瘦脸特效方法
func DoFaceThin(uiImage: UIImage, completionHandler: @escaping CompletionHandle) {
        let image = CIImage(image: uiImage)
        let weakSelf = self
        let width = uiImage.size.width
        let height = uiImage.size.height
        
        // 人脸检测完成回调方法
        let faceRequest = VNDetectFaceLandmarksRequest() { request, error in
            guard let results = request.results as? [VNFaceObservation] else {
             ...
            }
            let face = results.first
             ...
            guard let boundingRect = face?.boundingBox else {
               ...
            }

           // 获得人脸位置盒子
            var boundingBox = CGRect(x:CGFloat(boundingRect.origin.x * width),
                                     y:CGFloat(height - ((boundingRect.origin.y + boundingRect.size.height) * height)),
                                     width: CGFloat(boundingRect.size.width * width),
                                     height: CGFloat(boundingRect.size.height * height))
            // 锚点坐标
            var points: [CGPoint] = []
            // 变换后坐标
            var toPoints: [CGPoint] = []

            // 将人脸轮廓点添加到锚点数组中
            for point in face!.landmarks!.faceContour!.normalizedPoints {
               let p = CGPoint(x: CGFloat(point.x * boundingBox.width + boundingBox.origin.x),
                               y: CGFloat((1.0 - point.y) * boundingBox.height + boundingBox.origin.y))
                points.append(p)
                toPoints.append(p)
            }
            
            // 将图片边缘添加到锚点数组里
            for i in stride(from: 0, to: Int(height) ,by: 80) {
                let p1 = CGPoint(x: 0, y:CGFloat(i))
                let p2 = CGPoint(x: width, y:CGFloat(i))
                points.append(p1)
                toPoints.append(p1)
                points.append(p2)
                toPoints.append(p2)
            }
            // 将图片四角添加到锚点列表中
            let bottomLeft = CGPoint(x: 0, y: height)
            let bottomRight = CGPoint(x: width, y: height)
            points.append(bottomLeft)
            toPoints.append(bottomLeft)
            points.append(bottomRight)
            toPoints.append(bottomRight)
            

            // 对人脸轮廓进行收缩,实现瘦脸效果
            for index in 1...6 {
                let idx1 = 8 - index
                let idx2 = 8 + index
                
                let w = points[idx1].x - points[idx2].x
                
                let padding = w/CGFloat(8+index*2)/2
                
                toPoints[idx1].x -= padding
                toPoints[idx2].x += padding
            }
            
            // MLS 变换实例,传入锚点和目标点坐标,对图片进行变换
            let thinFaceOperation = ThinFaceOperation()
            thinFaceOperation.setupData(image: uiImage, landmarks: points, toPoints: toPoints)
            
            let imageInput = PictureInput(image: uiImage)
            
            let imageOutput = PictureOutput()
            imageOutput.imageAvailableCallback = { image in
                completionHandler(image)
                weakSelf.lockFx.unlock()
            }
            
            imageInput --> thinFaceOperation --> imageOutput
            imageInput.processImage(synchronously:true)
        }
        
        // 执行人脸检测
        let handler = VNImageRequestHandler(ciImage: image!,options: [:])
        DispatchQueue.global(qos: .userInitiated).async {
            try? handler.perform([faceRequest])
        }
    }

MSL变换需要两组数据,分别是锚点(原始点)的坐标位置数组和目标点(变换后的点)的坐标位置数组。
实现 VNDetectFaceLandmarksRequest 回调方法,在回调中处理人脸数据。
将图片的边缘和4角坐标也都传入锚点和目标点数组,这样可以防止图片边缘因为MSL变换产生畸变。
将人类数据传入锚点和目标点数组,并处理目标点坐标,使脸部收缩变瘦。
最后调用MSL方法实例,完成变换操作。

瘦脸特效完整代码 MagicCamera/Vision/ThinFace.swift

MSL算法的具体实现可以看源码文件: MagicCamera/Vision/MlsOperation/MovingLeastSquareHelper.mm

魔法相机项目

项目地址: william0wang/MagicCamera (github.com)
系列教程: 魔法相机 - 开发教程

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容