Exemplo n.º 1
0
        /// <summary>
        /// Raises the webcam texture to mat helper initialized event.
        /// </summary>
        public void OnWebCamTextureToMatHelperInitialized()
        {
            Debug.Log("OnWebCamTextureToMatHelperInitialized");

            Mat webCamTextureMat = webCamTextureToMatHelper.GetMat();

            texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;

            gameObject.transform.localScale = new Vector3(webCamTextureMat.cols(), webCamTextureMat.rows(), 1);
            Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);


            float width  = webCamTextureMat.width();
            float height = webCamTextureMat.height();

            float widthScale  = (float)Screen.width / width;
            float heightScale = (float)Screen.height / height;

            if (widthScale < heightScale)
            {
                Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
            }
            else
            {
                Camera.main.orthographicSize = height / 2;
            }


            Mat downScaleMat = imageOptimizationHelper.GetDownScaleMat((webCamTextureMat));

            gray1Mat = new Mat(downScaleMat.rows(), downScaleMat.cols(), CvType.CV_8UC1);
            gray2Mat = new Mat(downScaleMat.rows(), downScaleMat.cols(), CvType.CV_8UC1);

            int ksize = 7;

            float[] kernelData = new float[ksize * ksize];
            for (int i = 0; i < kernelData.Length; i++)
            {
                if (i == kernelData.Length / 2)
                {
                    kernelData [i] = (-(kernelData.Length - 1));
                }
                else
                {
                    kernelData [i] = 1;
                }
            }
            kernel = new Mat(ksize, ksize, CvType.CV_32F);
            kernel.put(0, 0, kernelData);

            byteArray = new byte[downScaleMat.width() * downScaleMat.height()];

            subdiv = new Subdiv2D();
        }
Exemplo n.º 2
0
        void FaceLM(Mat rgbaMat)
        {
            Mat   downScaleRgbaMat = null;
            float DOWNSCALE_RATIO  = 1.0f;

            if (enableDownScale)
            {
                downScaleRgbaMat = imageOptimizationHelper.GetDownScaleMat(rgbaMat);
                DOWNSCALE_RATIO  = imageOptimizationHelper.downscaleRatio;
            }
            else
            {
                downScaleRgbaMat = rgbaMat;
                DOWNSCALE_RATIO  = 1.0f;
            }

            OpenCVForUnityUtils.SetImage(faceLandmarkDetector, downScaleRgbaMat);
            //   Imgproc.cvtColor(downScaleRgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
            List <UnityEngine.Rect> detectionResult = faceLandmarkDetector.Detect();

            if (detectionResult.Count > 0)
            {
                if (enableDownScale)
                {
                    for (int i = 0; i < detectionResult.Count; ++i)
                    {
                        var rect = detectionResult[i];
                        detectionResult[i] = new UnityEngine.Rect(
                            rect.x * DOWNSCALE_RATIO,
                            rect.y * DOWNSCALE_RATIO,
                            rect.width * DOWNSCALE_RATIO,
                            rect.height * DOWNSCALE_RATIO);
                    }
                }

                OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbaMat);

                //detect landmark points
                facePoints = faceLandmarkDetector.DetectLandmark(detectionResult[0]);

                OpenCVForUnityUtils.DrawFaceLandmark(rgbaMat, facePoints, new Scalar(0, 255, 0, 255), 1, false, true);
            }
        }