EnoxSoftware / DlibFaceLandmarkDetector

FaceLandmark Detector using Dlib (Unity Asset Plugin)
https://assetstore.unity.com/packages/tools/integration/dlib-facelandmark-detector-64314
95 stars 31 forks source link

Head tilt calculated #9

Closed shacharoz closed 4 years ago

shacharoz commented 4 years ago

hey @EnoxSoftware i managed to accurately calculate the tilt of the head. by adding this function to the code. it would be nice if you can add it to your documentation. i think it is quite useful. it takes the two eye locations and calculates it from there.

///

/// calculate the head tilt by using the straight triangle rules: cos alpha = A / C /// /// /// /// private float CalculateHeadTiltAngle(List _landmarks) { //take left eye and right eye from the face landmarks Vector2 lefteye = _landmarks[36]; Vector2 righteye = _landmarks[45];

        //identify the distance between the two points (one triangle line)
        float C = Vector2.Distance(lefteye, righteye);
        //identify the length of the second triangle line
        float A =  righteye.x - lefteye.x;
        //convert the cos alpha to the angle. dont forget to convert to degrees from radians
        float tilt = Mathf.Acos(A / C) * 57.2958f; //radians to degrees
        //add direction of the tilt (minus or plus)
        tilt = (lefteye.y < righteye.y) ? tilt * -1 : tilt;

        Debug.Log(tilt);
        return tilt;
    }
EnoxSoftware commented 4 years ago

This is very helpful. Thank you very much.