Пример #1
0
    void DrawResults(FaceDetect.Result detection, FaceMesh.Result face)
    {
        cameraView.rectTransform.GetWorldCorners(rtCorners);
        Vector3 min = rtCorners[0];
        Vector3 max = rtCorners[2];

        // Draw Face Detection
        {
            draw.color = Color.blue;
            Rect rect = MathTF.Lerp(min, max, detection.rect, true);
            draw.Rect(rect, 0.05f);
            foreach (Vector2 p in detection.keypoints)
            {
                draw.Point(MathTF.Lerp(min, max, new Vector3(p.x, 1f - p.y, 0)), 0.1f);
            }
        }
        draw.Apply();

        // Draw face
        draw.color = Color.green;
        float zScale = (max.x - min.x) / 2;

        for (int i = 0; i < face.keypoints.Length; i++)
        {
            Vector3 p = MathTF.Lerp(min, max, face.keypoints[i]);
            p.z = face.keypoints[i].z * zScale;
            faceKeypoints[i] = p;
            draw.Point(p, 0.05f);
        }
        draw.Apply();

        // Update Mesh
        FaceMeshBuilder.UpdateMesh(faceMeshFilter.sharedMesh, faceKeypoints);
    }
Пример #2
0
    private void OnTextureUpdate(Texture texture)
    {
        if (detectionResult == null || !useLandmarkToDetection)
        {
            faceDetect.Invoke(texture);
            cameraView.material = faceDetect.transformMat;
            detectionResult     = faceDetect.GetResults().FirstOrDefault();

            if (detectionResult == null)
            {
                return;
            }
        }

        faceMesh.Invoke(texture, detectionResult);
        croppedView.texture = faceMesh.inputTex;
        meshResult          = faceMesh.GetResult();

        if (meshResult.score < 0.5f)
        {
            detectionResult = null;
            return;
        }

        if (useLandmarkToDetection)
        {
            detectionResult = faceMesh.LandmarkToDetection(meshResult);
        }
    }
Пример #3
0
    void Update()
    {
        if (detection == null || !useLandmarkToDetection)
        {
            faceDetect.Invoke(webcamTexture);
            cameraView.material = faceDetect.transformMat;
            detection           = faceDetect.GetResults().FirstOrDefault();

            if (detection == null)
            {
                return;
            }
        }

        faceMesh.Invoke(webcamTexture, detection);
        croppedView.texture = faceMesh.inputTex;
        var meshResult = faceMesh.GetResult();

        if (meshResult.score < 0.5f)
        {
            detection = null;
            return;
        }

        DrawResults(detection, meshResult);

        if (useLandmarkToDetection)
        {
            detection = faceMesh.LandmarkToDetection(meshResult);
        }
    }
    private void DrawResults(FaceDetect.Result faceDetectionResult,
                             FaceMesh.Result faceMesh,
                             IrisDetect.Result irisLeftResult,
                             IrisDetect.Result irisRightResult)
    {
        viewWebCam.rectTransform.GetWorldCorners(m_rtCorners);
        Vector3 min = m_rtCorners[0];
        Vector3 max = m_rtCorners[2];

        float zScale = (max.x - min.x) / 2;

        Color32 faceMeshDetectColor = new Color32(30, 150, 255, 255);
        Color32 eyeDetectColor      = new Color32(255, 255, 0, 255);
        Color32 irisDetectColor     = new Color32(255, 0, 0, 255);

        // TODO check why 6 landmarks are not showing up properly
        //DrawFaceDetection(faceDetectionResult, min, max, new Color32(80,200,255,255), 0.05f);

        DrawFaceMeshDetection(faceMesh, min, max, faceMeshDetectColor, zScale, 0.035f);

        DrawEyeDetection(min, max, irisLeftResult, irisRightResult, eyeDetectColor, zScale, 0.035f);

        DrawIrisDetection(min, max, irisLeftResult, irisRightResult, irisDetectColor, zScale, 0.035f);

        //TODO uncomment this to show up mesh
        //RenderFaceMesh();
    }
    private void DrawFaceDetection(FaceDetect.Result detection, Vector3 min, Vector3 max, Color color, float pntSize)
    {
        // Draw Face Detection
        m_drawPrimitive.color = color;
        UnityEngine.Rect rect = MathTF.Lerp(min, max, detection.rect, true);
        m_drawPrimitive.Rect(rect, 0.03f);

        float zScale = (max.x - min.x) / 2;

        for (int i = 0; i < detection.keypoints.Length; i++)
        {
            Vector3 p = MathTF.Lerp(min, max, new Vector3(detection.keypoints[i].x, 1f - detection.keypoints[i].y, 0));
            //Debug.Log("Detection Pnts: " +  detection.keypoints.Length + "\n");
            m_drawPrimitive.Point(p, pntSize);
        }

        m_drawPrimitive.Apply();
    }
    void Update()
    {
        //////////////////////////////////////////////////////////////////////////////////////////
        // Face Detection

        // TODO: detected points should be 6 and it works for the first run but then they are only 2
        if (m_faceDetectionResult == null || !useLandmarkToDetection)
        {
            m_faceDetect.Invoke(m_webcamTexture);

            viewWebCam.material = m_faceDetect.transformMat;

            m_faceDetectionResult = m_faceDetect.GetResults().FirstOrDefault();


            if (m_faceDetectionResult == null)
            {
                return;
            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////
        // Face Mesh Landmarks Detection

        m_faceMesh.Invoke(m_webcamTexture, m_faceDetectionResult);

        viewFaceDetect.texture = m_faceMesh.inputTex;


        var faceMeshResult = m_faceMesh.GetResult();

        if (faceMeshResult.score < 0.5f)
        {
            m_faceDetectionResult = null;
            return;
        }

        //////////////////////////////////////////////////////////////////////////////////////////
        // Eye and Iris Landmarks Detection

        int[] indxLeftEye  = { 33, 133 };
        int[] indxRightEye = { 362, 263 };


        m_irisLeftDetect.Invoke(m_webcamTexture, indxLeftEye, m_faceDetectionResult, faceMeshResult, 1);
        m_irisRightDetect.Invoke(m_webcamTexture, indxRightEye, m_faceDetectionResult, faceMeshResult, -1);

        viewEyeLeft.texture  = m_irisLeftDetect.inputTex;
        viewEyeRight.texture = m_irisRightDetect.inputTex;

        m_irisLeftResult  = m_irisLeftDetect.GetResult(1);
        m_irisRightResult = m_irisRightDetect.GetResult(-1);

        //////////////////////////////////////////////////////////////////////////////////////////
        // Draw Results

        DrawResults(m_faceDetectionResult, faceMeshResult, m_irisLeftResult, m_irisRightResult);

        if (useLandmarkToDetection)
        {
            m_faceDetectionResult = m_faceMesh.LandmarkToDetection(faceMeshResult);
        }
    }