Пример #1
0
        // Update is called once per frame
        public override void UpdateValue()
        {
            if (faceLandmarkDetector == null)
            {
                return;
            }

            if (matSource == null)
            {
                return;
            }

            didUpdateFaceLanmarkPoints = false;

            Mat rgbaMat = matSource.GetMatSource();

            if (rgbaMat != null)
            {
                if (debugRawImage != null)
                {
                    if (texture == null || (texture.width != rgbaMat.width() || texture.height != rgbaMat.height()))
                    {
                        texture = new Texture2D(rgbaMat.cols(), rgbaMat.rows(), TextureFormat.RGBA32, false);
                    }
                    if (colors == null || colors.Length != rgbaMat.width() * rgbaMat.height())
                    {
                        colors = new Color32[rgbaMat.width() * rgbaMat.height()];
                    }
                    debugRawImage.texture = texture;
                }


                OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbaMat);

                if (openCVFaceRectGetter != null)
                {
                    UnityEngine.Rect faceRect = openCVFaceRectGetter.GetFaceRect();

#if UNITY_5_5_OR_NEWER
                    if (faceRect != UnityEngine.Rect.zero)
#else
                    if (faceRect != new UnityEngine.Rect())
#endif
                    {
                        faceRect = new UnityEngine.Rect((float)faceRect.x, (float)faceRect.y + (float)(faceRect.height * 0.1f), (float)faceRect.width, (float)faceRect.height);

                        //detect landmark points
                        List <Vector2> points = faceLandmarkDetector.DetectLandmark(faceRect);


                        faceLandmarkPoints = points;

                        didUpdateFaceLanmarkPoints = true;

                        OpenCVForUnityUtils.DrawFaceLandmark(rgbaMat, points, new Scalar(0, 255, 0, 255), 2);
                    }
                }
                else
                {
                    //detect face rects
                    List <UnityEngine.Rect> detectResult = faceLandmarkDetector.Detect();

                    if (detectResult.Count > 0)
                    {
                        //detect landmark points
                        List <Vector2> points = faceLandmarkDetector.DetectLandmark(detectResult [0]);


                        faceLandmarkPoints = points;

                        didUpdateFaceLanmarkPoints = true;

                        if (debugRawImage != null)
                        {
                            OpenCVForUnityUtils.DrawFaceLandmark(rgbaMat, points, new Scalar(0, 255, 0, 255), 2);
                        }
                    }
                }

                //Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar (255, 255, 255, 255), 1, Imgproc.LINE_AA, false);

                if (debugRawImage != null)
                {
                    OpenCVForUnity.Utils.matToTexture2D(rgbaMat, texture, colors);
                }
            }
        }
        public override void UpdateValue()
        {
            if (faceLandmarkDetector == null)
            {
                return;
            }

            if (matSourceGetterInterface == null)
            {
                return;
            }

            didUpdateFaceLanmarkPoints = false;

            Mat rgbaMat          = matSourceGetterInterface.GetMatSource();
            Mat downScaleRgbaMat = matSourceGetterInterface.GetDownScaleMatSource();

            if (rgbaMat != null)
            {
                if (isDebugMode && screen != null)
                {
                    if (debugMat != null && (debugMat.width() != rgbaMat.width() || debugMat.height() != rgbaMat.height()))
                    {
                        debugMat.Dispose();
                        debugMat = null;
                    }
                    debugMat = debugMat ?? new Mat(rgbaMat.rows(), rgbaMat.cols(), rgbaMat.type());

                    if (hideImage)
                    {
                        debugMat.setTo(new Scalar(0, 0, 0, 255));
                    }
                    else
                    {
                        rgbaMat.copyTo(debugMat);
                    }

                    if (debugTexture != null && (debugTexture.width != debugMat.width() || debugTexture.height != debugMat.height()))
                    {
                        Texture2D.Destroy(debugTexture);
                        debugTexture = null;
                    }
                    if (debugTexture == null)
                    {
                        debugTexture = new Texture2D(debugMat.width(), debugMat.height(), TextureFormat.RGBA32, false, false);

                        Vector2 size = screen.rectTransform.sizeDelta;
                        screen.rectTransform.sizeDelta = new Vector2(size.x, size.x * (float)debugMat.height() / (float)debugMat.width());
                    }

                    if (debugColors != null && debugColors.Length != debugMat.width() * debugMat.height())
                    {
                        debugColors = new Color32[debugMat.width() * debugMat.height()];
                    }
                    screen.texture = debugTexture;
                    screen.enabled = true;
                }
                else
                {
                    if (screen != null)
                    {
                        screen.enabled = false;
                    }
                }


                if (faceRectGetterInterface != null)
                {
                    UnityEngine.Rect faceRect = faceRectGetterInterface.GetFaceRect();

                    if (faceRect != UnityEngine.Rect.zero)
                    {
                        // correct the deviation of the detection result of the face rectangle of OpenCV and Dlib.
                        faceRect = new UnityEngine.Rect(
                            faceRect.x + (faceRect.width * 0.05f),
                            faceRect.y + (faceRect.height * 0.1f),
                            faceRect.width * 0.9f,
                            faceRect.height * 0.9f);

                        OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbaMat);
                        List <Vector2> points = faceLandmarkDetector.DetectLandmark(faceRect);

                        faceLandmarkPoints = points;

                        didUpdateFaceLanmarkPoints = true;

                        if (isDebugMode && screen != null)
                        {
                            OpenCVForUnityUtils.DrawFaceLandmark(debugMat, points, new Scalar(0, 255, 0, 255), 2);
                        }
                    }
                }
                else
                {
                    //detect face rects
                    OpenCVForUnityUtils.SetImage(faceLandmarkDetector, downScaleRgbaMat);
                    List <UnityEngine.Rect> detectResult = faceLandmarkDetector.Detect();

                    OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbaMat);
                    if (detectResult.Count > 0)
                    {
                        // restore to original size rect
                        UnityEngine.Rect r = detectResult[0];
                        float            downscaleRatio = matSourceGetterInterface.GetDownScaleRatio();
                        UnityEngine.Rect rect           = new UnityEngine.Rect(
                            r.x * downscaleRatio,
                            r.y * downscaleRatio,
                            r.width * downscaleRatio,
                            r.height * downscaleRatio
                            );

                        // detect landmark points
                        List <Vector2> points = faceLandmarkDetector.DetectLandmark(rect);

                        faceLandmarkPoints = points;

                        didUpdateFaceLanmarkPoints = true;

                        if (isDebugMode && screen != null)
                        {
                            OpenCVForUnityUtils.DrawFaceLandmark(debugMat, points, new Scalar(0, 255, 0, 255), 2);
                        }
                    }
                }

                //Imgproc.putText (debugMat, "W:" + debugMat.width () + " H:" + debugMat.height () + " SO:" + Screen.orientation, new Point (5, debugMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar (255, 255, 255, 255), 1, Imgproc.LINE_AA, false);

                if (isDebugMode && screen != null)
                {
                    OpenCVForUnity.UnityUtils.Utils.matToTexture2D(debugMat, debugTexture, debugColors);
                }
            }
        }