void Start()
        {
            _uiPreview.texture = _image;

            using var detector = new BlazeFace.FaceDetector(_blazeFace);
            detector.ProcessImage(_image, 0.5f);

            var detection = detector.Detections.First();
            var cropScale = new Vector2(detection.extent.x,
                                        detection.extent.y) * 1.5f;
            var cropOffset = detection.center - cropScale / 2;

            _cropRT = new RenderTexture(192, 192, 0);

            Graphics.Blit(_image, _cropRT, cropScale, cropOffset);


            _builder = new FaceMesh.MeshBuilder(_faceMesh);
            _builder.ProcessImage(_cropRT);

            _material = new Material(_shader);
            _material.SetVector("_Scale", cropScale);
            _material.SetVector("_Offset", cropOffset);
            _material.SetBuffer("_Vertices", _builder.VertexBuffer);
        }
Beispiel #2
0
        void Update()
        {
            _previewUI.texture = _webcam.Texture;

            _detector.ProcessImage(_webcam.Texture, 0.5f);

            var detection = _detector.Detections.FirstOrDefault();

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

            var cropScale = new Vector2(detection.extent.x,
                                        detection.extent.y) * 1.5f;
            var cropOffset = detection.center - cropScale / 2;

            Graphics.Blit(_webcam.Texture, _cropRT, cropScale, cropOffset);

            _builder.ProcessImage(_cropRT);

            _material.SetVector("_Scale", cropScale);
            _material.SetVector("_Offset", cropOffset);
            _material.SetBuffer("_Vertices", _builder.VertexBuffer);

            Graphics.DrawMesh(_template, transform.localToWorldMatrix, _material, 0);
        }
        void Start()
        {
            _uiPreview.texture = _image;

            _builder = new FaceMesh.MeshBuilder(_faceMesh);
            _builder.ProcessImage(_image);

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

            // Use the first detection. Break if no detection.
            var detection = _detector.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
            _builder.ProcessImage(_cropRT);

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

            _faceMaterial.mainTexture = _faceTexture;
            _faceMaterial.SetBuffer("_Vertices", _builder.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", _builder.VertexBuffer);
            Graphics.DrawMesh(_wireTemplate, mw, _wireMaterial, 0);

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