美文网首页
Facerig 低配简化版demo

Facerig 低配简化版demo

作者: HellyCla | 来源:发表于2018-03-07 22:32 被阅读347次

    主要实现人脸的姿势捕捉和模拟。根据人脸识别及人脸特征点的检测,由特征点的二维信息还原出人脸姿势的旋转角,再控制模型,让模型做出相同的姿势。

    平台:Android
    相关库dlib-android, live2D-android SDK

    • 实时预览

    使用API camera 在运行时动态获取摄像头权限。CameraPreview类继承SurfaceView,用来预览相机数据。首先获取设备支持的预览图片格式、大小等信息,初始化前置摄像头。利用setPreviewCallbackWithBuffer函数来获取到预览的数据,在Callback函数中将YUVImage转换为Bitmap。使用带有Buffer的回调函数可以加快程序的运行速度,确保实时的预览。

    • 人脸识别与特征点检测

    这里采用了dlib库中的68点检测人脸功能。而dlib是一个C++库,因此需要将它移植到Android上,使用了tzutalin编译好的dlib-android。作者将C++接口用Java包装好了,我们可以直接调用Java接口。

    • 人脸姿势估计

    估计人脸法线方向,参考论文Estimating Gaze from a Single View of a Face
    Determing the gaze of faces in images

    人脸面部模型如图所示:


    Facial model

    建立坐标系如图所示:

    Estimating the facial normal
    人脸法线方向即为鼻基到鼻尖连线的向量,两个已知角度如图所示。
    则法线向量n为:

    式中未知的theta角可以通过模型比例Rn和图像中的两个量化数据解方程得到:




    计算人脸凝视角度 凝视方向和人脸法线方向

    人脸凝视方向与人脸法线方向相差近似固定的夹角,约为15度,在求出人脸法线方向后就可以相应的计算出人脸凝视方向。

     private void solveFacePose(ArrayList<Point> landmarks) {
            //Log.d(TAG, "analyze emotion");
    
            Point leftEye, rightEye, noseTip, mouthLeft, mouthRight;
            leftEye = landmarks.get(36);
            rightEye = landmarks.get(45);
            noseTip = landmarks.get(30);
            mouthLeft = landmarks.get(48);
            mouthRight = landmarks.get(54);
    
            Point noseBase = new Point((leftEye.x+rightEye.x)/2, (leftEye.y+rightEye.y)/2);
            Point mouth = new Point((mouthLeft.x+mouthRight.x)/2, (mouthLeft.y+mouthRight.y)/2);
    
            Point n = new Point((int)(mouth.x + (noseBase.x - mouth.x)*r_m),
                    (int)(mouth.y + (noseBase.y - mouth.y)*r_m));
    
            double theta = Math.acos( (double)((noseBase.x-n.x)*(noseTip.x-n.x) + (noseBase.y-n.y)*(noseTip.y-n.y)) /
            Math.hypot(noseTip.x-n.x, noseTip.y-n.y) / Math.hypot(noseBase.x-n.x, noseBase.y-n.y));
            double tau = Math.atan2( (double)(n.y-noseTip.y), (double)(n.x-noseTip.x) );
    
            double m1 = (double)((noseTip.x-n.x)*(noseTip.x-n.x) + (noseTip.y-n.y)*(noseTip.y-n.y)) /
                            ((noseBase.x-mouth.x)*(noseBase.x-mouth.x) + (noseBase.y-mouth.y)*(noseBase.y-mouth.y)),
                    m2 = Math.cos(theta)*Math.cos(theta);
            double a = r_n*r_n*(1-m2);
            double b = m1 - r_n*r_n + 2*m2*r_n*r_n;
            double c = - m2*r_n*r_n;
    
            double delta = Math.acos(Math.sqrt( (Math.sqrt(b*b-4*a*c) - b)/(2*a) ));
    
            //fn: facial normal, sfn: standard(no rotation) facial normal
            double[] fn = new double[3], sfn = new double[3];
            fn[0] = Math.sin(delta)*Math.cos(tau);
            fn[1] = Math.sin(delta)*Math.sin(tau);
            fn[2] = - Math.cos(delta);
    
            double alpha = Math.PI / 12;
            sfn[0] = 0;
            sfn[1] = Math.sin(alpha);
            sfn[2] = - Math.cos(alpha);
    
            //PITCH:X YAW:Y ROLL:X
            //Log.d(TAG, "facial normal: " + fn[0] + " " + fn[1] + " " + fn[2]);
            //Log.d(TAG, "standard facial normal: " + sfn[0] + " " + sfn[1] + " " + sfn[2]);
    
            /*
            live2d rotation order: ZXY
            live2d coordinate           my coordinate           my coordinate
            angle Z                     z-axis                  Yaw
            angle X                     y-axis                  Pitch
            angle Y                     x-axis                  Roll
            my coordinate is same as the paper:
            Estimating Gaze from a Single View of a Face
            link: ieeexplore.ieee.org/document/576433/
             */
    
            // (w, x, y, z) is Euler quaternion
            //
            double w, x, y, z;
            double angle = Math.acos((sfn[0]*fn[0] + sfn[1]*fn[1] + sfn[2]*fn[2]) /
                    Math.sqrt(sfn[0]*sfn[0] + sfn[1]*sfn[1] + sfn[2]*sfn[2]) /
                    Math.sqrt(fn[0]*fn[0] + fn[1]*fn[1] + fn[2]*fn[2]));
            w = Math.cos(0.5*angle);
            x = sfn[1]*fn[2] - sfn[2]*fn[1];
            y = sfn[2]*fn[0] - sfn[0]*fn[2];
            z = sfn[0]*fn[1] - sfn[1]*fn[0];
    
            double l = Math.sqrt(x*x + y*y + z*z);
            x = Math.sin(0.5*angle)*x/l;
            y = Math.sin(0.5*angle)*y/l;
            z = Math.sin(0.5*angle)*z/l;
    
            //Log.d(TAG, "Angle: " + w);
            
            double yaw, pitch, roll;
            roll = Math.atan2(2*(w*x+y*z), 1-2*(x*x+y*y));
            pitch = Math.asin(2*(w*y-z*x));
            yaw = Math.atan2(2*(w*z+x*y), 1-2*(y*y+z*z));
    
    //        if(yaw < Math.PI / 18) {
            if(sfn[0] < 0.1 && sfn[1] < 0.1) {
                roll = 1.5*Math.atan2(rightEye.y-leftEye.y, rightEye.x-leftEye.x);
            }
            yaw = Math.max(-30, Math.min(30, yaw*180/Math.PI));
            pitch = Math.max(-30, Math.min(30, pitch*180/Math.PI));
            roll = Math.max(-30, Math.min(30, roll*180/Math.PI));
    
            Log.d(TAG, "Yaw: " + yaw + " Pitch: " + pitch + " Roll: " + roll);
    
            this.mActivity.emotion[0] = yaw;
            this.mActivity.emotion[1] = pitch;
            this.mActivity.emotion[2] = roll;
            //this.mActivity.mGLSurfaceView.requestRender();
        }
    

    Live2D模型

    Live2D是一种应用于电子游戏的绘图渲染技术,技术由日本Cybernoids公司开发。通过一系列的连续图像和人物建模来生成一种类似三维模型的二维图像,对于以动画风格为主的冒险游戏来说非常有用,现在也经常用来制作动态壁纸。缺点是Live 2D人物无法大幅度转身,3D效果其实很差。新的Live2D官方说可以显示360度图像,如转身之类大幅度的动作,但是目前还未有试用版。它本质上是二维图片,所以对性能要求较低,非常适合Android设备使用。


    live2D
    Epsilon_free.gif

    live2D 2.0 给人物增加了眨眼动作,使人物看起来更加生动,同时做了帧与帧之间的平滑,进行了一定程度的防抖。
    下载了来自Live2D的任务素材
    以下是某程序猿对live2D的吐槽:


    在这个demo中我们需要的接口如下:

            live2DModel.setParamFloat(L2DStandardID.PARAM_ANGLE_Z, (float) mActivity.emotion[0], 0.75f);
            live2DModel.setParamFloat(L2DStandardID.PARAM_ANGLE_X , (float) mActivity.emotion[1], 0.75f);
            live2DModel.setParamFloat(L2DStandardID.PARAM_ANGLE_Y , (float) mActivity.emotion[2], 0.75f);
            live2DModel.setParamFloat(L2DStandardID.PARAM_MOUTH_OPEN_Y, (float) mActivity.emotion[3], 0.75f);
            live2DModel.setParamFloat(L2DStandardID.PARAM_MOUTH_FORM, (float) mActivity.emotion[3], 0.75f);
    
    ezgif.com-video-to-gif.gif

    Live2Dmodel变换原理
    Live2D技术

    相关文章

      网友评论

          本文标题:Facerig 低配简化版demo

          本文链接:https://www.haomeiwen.com/subject/miisfftx.html