void Start()
        {
            _uiPreview.texture = _image;

            _detector = new FaceMesh.FaceLandmarkDetector(_faceMesh);
            _detector.ProcessImage(_image);

            _material = new Material(_shader);
            _material.SetBuffer("_Vertices", _detector.VertexBuffer);
        }
Beispiel #2
0
        void LateUpdate()
        {
            // Face detection
            _boxDetector.ProcessImage(_webcam.Texture, 0.5f);

            // Use the first detection. Break if no detection.
            var detection = _boxDetector.Detections.FirstOrDefault();

            if (detection.score == 0)
            {
                return;
            }

            // Face region analysis
            var scale  = detection.extent * 1.6f;
            var offset = detection.center - scale * 0.5f;
            var angle  = Vector2.Angle(Vector2.up, detection.nose - detection.mouth);

            if (detection.nose.x > detection.mouth.x)
            {
                angle *= -1;
            }

            // Face region cropping
            _cropMaterial.SetMatrix("_Xform", MakeBlitMatrix(offset, angle, scale));
            Graphics.Blit(_webcam.Texture, _cropRT, _cropMaterial, 0);

            // Face landmark detection
            _landmarkDetector.ProcessImage(_cropRT);

            // Visualization (face)
            var mf = MakeBlitMatrix(offset - new Vector2(0.75f, 0.5f), angle, scale);

            _faceMaterial.mainTexture = _faceTexture;
            _faceMaterial.SetBuffer("_Vertices", _landmarkDetector.VertexBuffer);
            Graphics.DrawMesh(_faceTemplate, mf, _faceMaterial, 0);

            // Visualization (wire)
            var mw = MakeBlitMatrix(new Vector2(0.25f, -0.5f), 0, Vector2.one * 0.5f);

            _wireMaterial.SetBuffer("_Vertices", _landmarkDetector.VertexBuffer);
            Graphics.DrawMesh(_wireTemplate, mw, _wireMaterial, 0);

            // UI update
            _mainUI.texture    = _webcam.Texture;
            _cropUI.texture    = _cropRT;
            _previewUI.texture = _webcam.Texture;
        }