private void Run()
        {
            gameObject.transform.localScale = new Vector3(texture2D.width, texture2D.height, 1);
            Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);

            float width  = gameObject.transform.localScale.x;
            float height = gameObject.transform.localScale.y;

            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;
            }

            FaceLandmarkDetector faceLandmarkDetector = new FaceLandmarkDetector(frontal_cat_face_svm_filepath, sp_cat_face_68_dat_filepath);

            faceLandmarkDetector.SetImage(texture2D);

            //detect face rects
            List <Rect> detectResult = faceLandmarkDetector.Detect();

            foreach (var rect in detectResult)
            {
                Debug.Log("face : " + rect);

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

                Debug.Log("face points count : " + points.Count);
                foreach (var point in points)
                {
                    Debug.Log("face point : x " + point.x + " y " + point.y);
                }

                //draw landmark points
                faceLandmarkDetector.DrawDetectLandmarkResult(texture2D, 0, 255, 0, 255);
            }

            //draw face rects
            faceLandmarkDetector.DrawDetectResult(texture2D, 255, 0, 0, 255, 3);

            faceLandmarkDetector.Dispose();

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

            if (fpsMonitor != null)
            {
                fpsMonitor.Add("dlib object detector", "frontal_cat_face.svm");
                fpsMonitor.Add("dlib shape predictor", "sp_cat_face_68.dat");
                fpsMonitor.Add("width", width.ToString());
                fpsMonitor.Add("height", height.ToString());
                fpsMonitor.Add("orientation", Screen.orientation.ToString());
            }
        }
        /// <summary>
        /// Sets a image.
        /// </summary>
        /// <param name="faceLandmarkDetector">Face landmark detector.</param>
        /// <param name="imgMat">Image mat.</param>
        public static void SetImage(FaceLandmarkDetector faceLandmarkDetector, Mat imgMat)
        {
            if (faceLandmarkDetector == null)
            {
                throw new ArgumentNullException("faceLandmarkDetector");
            }
            if (faceLandmarkDetector != null)
            {
                faceLandmarkDetector.ThrowIfDisposed();
            }

            if (imgMat == null)
            {
                throw new ArgumentNullException("imgMat");
            }
            if (imgMat != null)
            {
                imgMat.ThrowIfDisposed();
            }
            if (!imgMat.isContinuous())
            {
                throw new ArgumentException("imgMat.isContinuous() must be true.");
            }

            faceLandmarkDetector.SetImage((IntPtr)imgMat.dataAddr(), imgMat.width(), imgMat.height(), (int)imgMat.elemSize());
        }
Пример #3
0
    private void Update()
    {
        if (!hasInit || !streamManager.WebCam.didUpdateThisFrame)
        {
            return;
        }

        //colors = streamManager.WebCam.GetPixels32();

        //faceLandmarkDetector.SetImage(colors, textureWidth, textureHeight, 4, true);

        faceLandmarkDetector.SetImage(streamManager.WebCam);
        detectResult = faceLandmarkDetector.Detect();

        reArrange = true;

        //foreach (var rect in detectResult)
        //{
        //    faceLandmarkDetector.DetectLandmark(rect);

        //    faceLandmarkDetector.DrawDetectLandmarkResult(colors, textureWidth, textureHeight, 4, true, 0, 255, 0, 255);
        //}

        //faceLandmarkDetector.DrawDetectResult(colors, textureWidth, textureHeight, 4, true, 255, 0, 0, 255, 2);

        //texture.SetPixels32(colors);
        //texture.Apply(false);
    }
        // Update is called once per frame
        void Update()
        {
            if (!initDone)
            {
                return;
            }

            if (screenOrientation != Screen.orientation)
            {
                screenOrientation = Screen.orientation;
                updateLayout();
            }

                        #if UNITY_IOS && !UNITY_EDITOR && (UNITY_4_6_3 || UNITY_4_6_4 || UNITY_5_0_0 || UNITY_5_0_1)
            if (webCamTexture.width > 16 && webCamTexture.height > 16)
            {
                        #else
            if (webCamTexture.didUpdateThisFrame)
            {
                                #endif


                webCamTexture.GetPixels32(colors);
                faceLandmarkDetector.SetImage <Color32> (colors, webCamTexture.width, webCamTexture.height, 4, flip);

                //detect face rects
                List <Rect> detectResult = faceLandmarkDetector.Detect();

                foreach (var rect in detectResult)
                {
//			Debug.Log ("face : " + rect);

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


//			Debug.Log ("face point : " + points.Count);
                    if (points.Count > 0)
                    {
//				Debug.Log ("face points : x " + point.x + " y " + point.y);

                        //draw landmark points
                        faceLandmarkDetector.DrawDetectLandmarkResult <Color32> (colors, webCamTexture.width, webCamTexture.height, 4, flip, 0, 255, 0, 255);
                    }

                    //draw face rect
                    faceLandmarkDetector.DrawDetectResult <Color32> (colors, webCamTexture.width, webCamTexture.height, 4, flip, 255, 0, 0, 255, 2);
                }

                texture2D.SetPixels32(colors);
                texture2D.Apply();
            }
        }

        void OnDisable()
        {
            webCamTexture.Stop();
            faceLandmarkDetector.Dispose();
        }
Пример #5
0
 /// <summary>
 /// Sets a image.
 /// </summary>
 /// <param name="faceLandmarkDetector">Face landmark detector.</param>
 /// <param name="imgMat">Image mat.</param>
 public static void SetImage(FaceLandmarkDetector faceLandmarkDetector, Mat imgMat)
 {
     if (!imgMat.isContinuous())
     {
         throw new ArgumentException("imgMat.isContinuous() must be true.");
     }
     faceLandmarkDetector.SetImage((IntPtr)imgMat.dataAddr(), imgMat.width(), imgMat.height(), (int)imgMat.elemSize());
 }
Пример #6
0
        private void Run()
        {
            //if true, The error log of the Native side Dlib will be displayed on the Unity Editor Console.
            Utils.setDebugMode(true);


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

            float width  = gameObject.transform.localScale.x;
            float height = gameObject.transform.localScale.y;

            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;
            }

            FaceLandmarkDetector faceLandmarkDetector = new FaceLandmarkDetector(sp_human_face_68_dat_filepath);

            faceLandmarkDetector.SetImage(texture2D);

            //detect face rects
            List <Rect> detectResult = faceLandmarkDetector.Detect();

            foreach (var rect in detectResult)
            {
                Debug.Log("face : " + rect);

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

                Debug.Log("face points count : " + points.Count);
                foreach (var point in points)
                {
                    Debug.Log("face point : x " + point.x + " y " + point.y);
                }

                //draw landmark points
                faceLandmarkDetector.DrawDetectLandmarkResult(texture2D, 0, 255, 0, 255);
            }

            //draw face rect
            faceLandmarkDetector.DrawDetectResult(texture2D, 255, 0, 0, 255, 2);

            faceLandmarkDetector.Dispose();

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


            Utils.setDebugMode(false);
        }
        // Use this for initialization
        void Start()
        {
            gameObject.transform.localScale = new Vector3(texture2D.width, texture2D.height, 1);
            Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);

            float width  = gameObject.transform.localScale.x;
            float height = gameObject.transform.localScale.y;

            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;
            }


            FaceLandmarkDetector faceLandmarkDetector = new FaceLandmarkDetector(Utils.getFilePath("frontal_cat_face.svm"), Utils.getFilePath("shape_predictor_68_cat_face_landmarks.dat"));

            faceLandmarkDetector.SetImage(texture2D);

            //detect face rects
            List <Rect> detectResult = faceLandmarkDetector.Detect();

            foreach (var rect in detectResult)
            {
                Debug.Log("face : " + rect);

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

                Debug.Log("face points count : " + points.Count);
                if (points.Count > 0)
                {
                    foreach (var point in points)
                    {
                        Debug.Log("face point : x " + point.x + " y " + point.y);
                    }

                    //draw landmark points
                    faceLandmarkDetector.DrawDetectLandmarkResult(texture2D, 0, 255, 0, 255);
                }

                //draw face rects
                faceLandmarkDetector.DrawDetectResult(texture2D, 255, 0, 0, 255, 3);
            }

            faceLandmarkDetector.Dispose();

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture2D;
        }
Пример #8
0
        // Update is called once per frame
        void Update()
        {
            if (!hasInitDone)
            {
                return;
            }

            if (screenOrientation != Screen.orientation)
            {
                StartCoroutine(initWebCamTextureContract());
            }


            if (webCamTexture.didUpdateThisFrame)
            {
                webCamTexture.GetPixels32(colors);

                //Adjust an array of color pixels according to screen orientation and WebCamDevice parameter.
                colors = RotateAndFlip(colors, webCamTexture.width, webCamTexture.height);


                faceLandmarkDetector.SetImage <Color32>(colors, texture2D.width, texture2D.height, 4, true);

                //detect face rects
                List <Rect> detectResult = faceLandmarkDetector.Detect();

                bool eventTrigger = true;
                foreach (var rect in detectResult)
                {
                    //Debug.Log ("face : " + rect);

                    //detect landmark points
                    var landmarkList = faceLandmarkDetector.DetectLandmark(rect);
                    if (eventTrigger)
                    {
                        if (OnFacelandmarkUpdated != null)
                        {
                            OnFacelandmarkUpdated(this, new FacelandmarkResultEventArgs(rect, landmarkList));
                        }
                        eventTrigger = false;
                    }
                    //draw landmark points
                    faceLandmarkDetector.DrawDetectLandmarkResult <Color32>(colors, texture2D.width, texture2D.height, 4, true, 0, 255, 0, 255);
                }

                //draw face rect
                faceLandmarkDetector.DrawDetectResult <Color32>(colors, texture2D.width, texture2D.height, 4, true, 255, 0, 0, 255, 2);



                texture2D.SetPixels32(colors);
                texture2D.Apply();
            }
        }
    public List <Rect> DetectFaces(Texture2D imgTexture)
    {
        if (string.IsNullOrEmpty(dlibShapePredictorFilePath))
        {
            Debug.LogError(
                "shape predictor file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
        }

        faceLandmarkDetector.SetImage(imgTexture);

        //detect face rects
        List <Rect> faceRects = faceLandmarkDetector.Detect();

        return(faceRects);
    }
        // Update is called once per frame
        void Update()
        {
            if (adjustPixelsDirection)
            {
                // Catch the orientation change of the screen.
                if (screenOrientation != Screen.orientation && (screenWidth != Screen.width || screenHeight != Screen.height))
                {
                    Initialize();
                }
                else
                {
                    screenWidth  = Screen.width;
                    screenHeight = Screen.height;
                }
            }


            if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
            {
                Color32[] colors = GetColors();

                if (colors != null)
                {
                    faceLandmarkDetector.SetImage <Color32> (colors, texture.width, texture.height, 4, true);

                    //detect face rects
                    List <Rect> detectResult = faceLandmarkDetector.Detect();

                    foreach (var rect in detectResult)
                    {
                        //Debug.Log ("face : " + rect);

                        //detect landmark points
                        faceLandmarkDetector.DetectLandmark(rect);

                        //draw landmark points
                        faceLandmarkDetector.DrawDetectLandmarkResult <Color32> (colors, texture.width, texture.height, 4, true, 0, 255, 0, 255);
                    }

                    //draw face rect
                    faceLandmarkDetector.DrawDetectResult <Color32> (colors, texture.width, texture.height, 4, true, 255, 0, 0, 255, 2);

                    texture.SetPixels32(colors);
                    texture.Apply(false);
                }
            }
        }
        private void ShowImage(Texture2D texture2D)
        {
            if (dstTexture2D != null)
            {
                Texture2D.Destroy(dstTexture2D);
            }
            dstTexture2D = new Texture2D(texture2D.width, texture2D.height, texture2D.format, false);
            dstTexture2D.SetPixels32(texture2D.GetPixels32());
            dstTexture2D.Apply();

            gameObject.transform.localScale = new Vector3(texture2D.width, texture2D.height, 1);

            float width  = gameObject.transform.localScale.x;
            float height = gameObject.transform.localScale.y;

            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;
            }

            faceLandmarkDetector.SetImage(texture2D);

            //detect face rects
            List <Rect> detectResult = faceLandmarkDetector.Detect();

            foreach (var rect in detectResult)
            {
                //detect landmark points
                faceLandmarkDetector.DetectLandmark(rect);
                // draw landmark points
                faceLandmarkDetector.DrawDetectLandmarkResult(dstTexture2D, 0, 255, 0, 255);
            }

            // draw face rect
            faceLandmarkDetector.DrawDetectResult(dstTexture2D, 255, 0, 0, 255, 2);

            gameObject.GetComponent <Renderer>().material.mainTexture = dstTexture2D;
        }
Пример #12
0
    void Update()
    {
        // 画面の回転を取得
        // 傾きが変わった場合
        if (_screenOrientation != Screen.orientation &&
            (_screenWidth != Screen.width || _screenHeight != Screen.height))
        {
            Initialize();
        }
        else
        {
            _screenWidth  = Screen.width;
            _screenHeight = Screen.height;
        }

        // ランドマーク推定
        if (_hasInitDone && _webCamTexture.isPlaying && _webCamTexture.didUpdateThisFrame)
        {
            Color32[] colors = GetColors();
            if (colors != null)
            {
                faceLandmarkDetector.SetImage <Color32>(colors, _texture.width, _texture.height, 4, true);

                // 顔が含まれる領域を取得
                List <Rect> detectResult = faceLandmarkDetector.Detect();

                foreach (var rect in detectResult)
                {
                    // 顔認識(ランドマーク推定)
                    Landmarks = faceLandmarkDetector.DetectLandmark(rect);
                    // ランドマークを描画
                    faceLandmarkDetector.DrawDetectLandmarkResult <Color32>(colors, _texture.width, _texture.height, 4,
                                                                            true, 0, 255, 0, 255);
                }

                // 顔領域を矩形で描画
                faceLandmarkDetector.DrawDetectResult <Color32>(colors, _texture.width, _texture.height, 4, true, 255, 0,
                                                                0, 255, 2);

                _texture.SetPixels32(colors);
                _texture.Apply(false);
            }
        }
    }
        // Update is called once per frame
        void Update()
        {
            Debug.Log("called update func");
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat       rgbaMat = webCamTextureToMatHelper.GetMat();
                Color32[] rgbabuf = webCamTextureToMatHelper.GetBufferColors();

                if (rgbabuf != null && faceLandmarkDetector != null && texture != null)
                {
                    Debug.Log("on Update above SetImage");
                    faceLandmarkDetector.SetImage <Color32> (rgbabuf, texture.width, texture.height, 4, true);

                    //detect face rects
                    List <UnityEngine.Rect> detectResult = faceLandmarkDetector.Detect();

                    foreach (var rect in detectResult)
                    {
                        Debug.Log("face : " + rect);

                        //detect landmark points
                        faceLandmarkDetector.DetectLandmark(rect);

                        //draw landmark points
                        faceLandmarkDetector.DrawDetectLandmarkResult <Color32> (rgbabuf, texture.width, texture.height, 4, true, 255, 255, 255, 255);
                        //faceLandmarkDetector.DrawDetectLandmarkResult<Color32>(drawbuf, texture.width, texture.height, 4, true, 255, 255, 255, 255);

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

                        if (points.Count > 0)
                        {
                            live2DModelUpdate(points);
                        }
                    }


                    if (isHideCameraImage == false)
                    {
                        texture.SetPixels32(rgbabuf);
                        texture.Apply(false);
                    }
                }
            }
        }
Пример #14
0
 void SetupFaceDetection()
 {
     spexMap = new List <Image>();
     hairMap = new List <Image>();
     faceLandmarkDetector = new FaceLandmarkDetector(System.IO.Path.Combine(Application.streamingAssetsPath, LibraryMap[library]));
     faceLandmarkDetector.SetImage(streamManager.WebCam);
     textureWidth  = streamManager.WebCam.width;
     textureHeight = streamManager.WebCam.height;
     Debug.Log($"Width: {textureWidth} Height: {textureHeight}");
     texture = new Texture2D(textureWidth, textureHeight, TextureFormat.RGBA32, false);
     overlayImage.texture      = texture;
     overlayFitter.aspectMode  = streamManager.fitter.aspectMode;
     overlayFitter.aspectRatio = streamManager.fitter.aspectRatio;
     widthRatio  = Screen.width / (float)textureWidth;
     heightRatio = Screen.height / (float)textureHeight;
     Debug.Log($"Screen Ratio: {screenRatio = Screen.width / (float)Screen.height}");
     Debug.Log($"Width Ratio: {widthRatio} Height Ratio: {heightRatio}");
     fillVertically = streamManager.WebCamRatio > screenRatio;
     hasInit        = true;
 }
        private string Benchmark(Texture2D targetImg, FaceLandmarkDetector detector, int times = 100)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            string result = "sp_name: " + dlibShapePredictorFileName + "\n";

            result += "times: " + times + " (size: " + targetImg.width + "*" + targetImg.height + ")" + "\n";

            detector.SetImage(targetImg);

            // FaceLandmarkDetector.Detect() benchmark.
            sw.Start();
            for (int i = 0; i < times; ++i)
            {
                detector.Detect();
            }
            sw.Stop();
            result += " Detect(): " + sw.ElapsedMilliseconds + "ms" + " Avg:" + sw.ElapsedMilliseconds / times + "ms" + "\n";


            // FaceLandmarkDetector.DetectLandmark() benchmark.
            List <Rect> detectResult = detector.Detect();

            sw.Reset();
            sw.Start();
            for (int i = 0; i < times; ++i)
            {
                foreach (var rect in detectResult)
                {
                    detector.DetectLandmark(rect);
                }
            }
            sw.Stop();
            result += " DetectLandmark(): " + sw.ElapsedMilliseconds + "ms" + " Avg:" + sw.ElapsedMilliseconds / times + "ms";

            return(result);
        }
Пример #16
0
    //struct FaceData
    //{
    //    internal Vector2 leftEye;
    //    internal Vector2 rightEye;
    //    internal Image spex;

    //    internal void ReOrient(Vector2 leftEye, Vector2 rightEye, Image spex)
    //    {
    //        leftEye
    //    }
    //}

    void SetupFaceDetection()
    {
        spexMap = new List <Image>();
        faceLandmarkDetector = new FaceLandmarkDetector(System.IO.Path.Combine(Application.streamingAssetsPath, LibraryMap[library]));
        faceLandmarkDetector.SetImage(sourceImage);
    }
Пример #17
0
        void Update()
        {
            if (true)
            {
                // Catch the orientation change of the screen.
                if (screenOrientation != Screen.orientation && (screenWidth != Screen.width || screenHeight != Screen.height))
                {
                    Initialize();
                }
                else
                {
                    screenWidth  = Screen.width;
                    screenHeight = Screen.height;
                }
            }

            if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
            {
                float     red    = 0;
                float     green  = 0;
                float     blue   = 0;
                Color32[] colors = GetColors();

                dist1.color = Color.red;
                dist2.color = Color.green;
                dist3.color = Color.blue;

                color = -1;

                if (colors != null)
                {
                    faceLandmarkDetector.SetImage <Color32> (colors, texture.width, texture.height, 4, true);

                    faceLandmarkDetector.DrawDetectResult <Color32> (colors, texture.width, texture.height, 4, true, 255, 0, 0, 255, 2);
                    texture.SetPixels32(colors);
                    texture.Apply();

                    if (updatedpoints && copyablepoints.Count > 0)
                    {
                        lock (copyablepointslocker)
                        {
                            updatedpoints = false;
                            points        = copyablepoints;
                            rectHeight    = copyableHeight;
                            rectWidth     = copyableWidth;
                            rect          = copyableRect;
                            if (firstFrame)
                            {
                                normal_points         = points;
                                firstFrame            = false;
                                number_of_prev_points = 1;
                                rgbColors.Add(red);
                                rgbColors.Add(green);
                                rgbColors.Add(blue);
                                rgbColors.Add(0);
                                rgbColors.Add(0);
                                rgbColors.Add(0);
                                recabText.text = "calibrating...";
                            }
                            else if (number_of_prev_points < 10)
                            {
                                setNormal();
                                recabText.text = "calibrating...";
                            }
                            else
                            {
                                recabText.text = "calibrated!";

                                faceDirHor();
                                faceDirVer();

                                if (headHorDirs.Count > 15)
                                {
                                    headHorDirs.RemoveAt(0);
                                    headHorDirs.Add(curHorDir);
                                }
                                else
                                {
                                    headHorDirs.Add(curHorDir);
                                }
                                headShake2(headHorDirs);

                                if (headVerDirs.Count > 15)
                                {
                                    headVerDirs.RemoveAt(0);
                                    headVerDirs.Add(curVerDir);
                                }
                                else
                                {
                                    headVerDirs.Add(curVerDir);
                                }
                                headNod2(headVerDirs);



                                mouthDetect();

                                float prev_eyesVal_left = quadArea(normal_points[37], normal_points[38], normal_points[40], normal_points[41]);
                                float cur_eyesVal_left  = quadArea(points[37], points[38], points[40], points[41]);

                                float prev_eyesVal_right = quadArea(normal_points[43], normal_points[44], normal_points[46], normal_points[47]);
                                float cur_eyesVal_right  = quadArea(points[43], points[44], points[46], points[47]);

                                float percDiff_left  = (((prev_eyesVal_left - cur_eyesVal_left) / (prev_eyesVal_left)) * 100);
                                float percDiff_right = (((prev_eyesVal_right - cur_eyesVal_right) / (prev_eyesVal_right)) * 100);


                                if (percDiff_left > 40 && percDiff_right > 40)
                                {
                                    eyesText.color = Color.green;
                                    eyesText.text  = "Eyes Closed";
                                }
                                else if (percDiff_left > 40)
                                {
                                    eyesText.color = Color.green;
                                    eyesText.text  = "Left Wink";
                                }
                                else if (percDiff_right > 40)
                                {
                                    eyesText.color = Color.green;
                                    eyesText.text  = "Right Wink";
                                }
                                else
                                {
                                    eyesText.color = Color.red;
                                    eyesText.text  = "Eyes Open";
                                }

                                #if UNITY_EDITOR
                                double[] f = getLandmarks_desktop(points, rect);
                                #else
                                double[] f = getLandmarks(points, rect);
                                #endif

                                int emotion = predict(f);

                                if (emotion == 0)
                                {
                                    emotion1.color = Color.green;
                                    emotion2.color = Color.red;
                                    emotion3.color = Color.red;
                                    emotion4.color = Color.red;
                                }
                                else if (emotion == 1)
                                {
                                    emotion1.color = Color.red;;
                                    emotion2.color = Color.green;
                                    emotion3.color = Color.red;
                                    emotion4.color = Color.red;
                                }
                                else if (emotion == 2)
                                {
                                    emotion1.color = Color.red;
                                    emotion2.color = Color.red;
                                    emotion3.color = Color.green;
                                    emotion4.color = Color.red;
                                }
                                else if (emotion == 3)
                                {
                                    emotion1.color = Color.red;
                                    emotion2.color = Color.red;
                                    emotion3.color = Color.red;
                                    emotion4.color = Color.green;
                                }
                            }
                        }
                    }
                }
            }
            // faceLandmarkDetector.DrawDetectLandmarkResult<Color32> (colors, texture.width, texture.height, 4, true, 0, 255, 0, 255);
        }
Пример #18
0
        void Update()
        {
            if (adjustPixelsDirection)
            {
                // Catch the orientation change of the screen.
                if (screenOrientation != Screen.orientation && (screenWidth != Screen.width || screenHeight != Screen.height))
                {
                    Initialize();
                }
                else
                {
                    screenWidth  = Screen.width;
                    screenHeight = Screen.height;
                }
            }


            if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
            {
                Color32[] colors = GetColors();

                if (colors != null)
                {
                    faceLandmarkDetector.SetImage <Color32> (colors, texture.width, texture.height, 4, true);

                    //detect face rects
                    List <Rect> detectResult = faceLandmarkDetector.Detect();

                    foreach (var rect in detectResult)
                    {
                        //Debug.Log ("face : " + rect);

                        //detect landmark points
                        points = faceLandmarkDetector.DetectLandmark(rect);

                        float eyeBoundUpper = 9;
                        float eyeBoundLower = 8;
                        float ebUpper       = 1;
                        float ebLower       = 8;

                        //left eye
                        leftEyeV1[0].x = (texture.width / 2) - points[36].x + eyeBoundUpper;
                        leftEyeV1[0].y = -(texture.height / 2) + points[36].y;

                        leftEyeV1[1].x = (texture.width / 2) - points[36].x + eyeBoundUpper;
                        leftEyeV1[1].y = -(texture.height / 2) + points[36].y - (eyeBoundUpper * 2);

                        leftEyeV1[2].x = (texture.width / 2) - points[37].x;
                        leftEyeV1[2].y = -(texture.height / 2) + points[37].y - (eyeBoundUpper * 2);

                        leftEyeV1[3].x = (texture.width / 2) - points[38].x;
                        leftEyeV1[3].y = -(texture.height / 2) + points[38].y - eyeBoundUpper;

                        leftEyeV1[4].x = (texture.width / 2) - points[39].x;
                        leftEyeV1[4].y = -(texture.height / 2) + points[39].y - eyeBoundUpper;

                        leftEyeV1[5].x = (texture.width / 2) - points[39].x;
                        leftEyeV1[5].y = -(texture.height / 2) + points[39].y;

                        leftEyeV1[6].x = (texture.width / 2) - points[38].x;
                        leftEyeV1[6].y = -(texture.height / 2) + points[38].y;

                        leftEyeV1[7].x = (texture.width / 2) - points[37].x;
                        leftEyeV1[7].y = -(texture.height / 2) + points[37].y;

                        leftEyeV1[8].x = (texture.width / 2) - points[36].x;
                        leftEyeV1[8].y = -(texture.height / 2) + points[36].y;


                        leftEyeV2[0].x = (texture.width / 2) - points[36].x + eyeBoundUpper + 2;
                        leftEyeV2[0].y = -(texture.height / 2) + points[36].y + 2;

                        leftEyeV2[1].x = (texture.width / 2) - points[36].x;
                        leftEyeV2[1].y = -(texture.height / 2) + points[36].y + 2;

                        leftEyeV2[2].x = (texture.width / 2) - points[41].x;
                        leftEyeV2[2].y = -(texture.height / 2) + points[41].y + 2;

                        leftEyeV2[3].x = (texture.width / 2) - points[40].x;
                        leftEyeV2[3].y = -(texture.height / 2) + points[40].y + 2;

                        leftEyeV2[4].x = (texture.width / 2) - points[39].x;
                        leftEyeV2[4].y = -(texture.height / 2) + points[39].y + 2;

                        leftEyeV2[5].x = (texture.width / 2) - points[39].x;
                        leftEyeV2[5].y = -(texture.height / 2) + points[39].y + eyeBoundLower;

                        leftEyeV2[6].x = (texture.width / 2) - points[40].x;
                        leftEyeV2[6].y = -(texture.height / 2) + points[40].y + eyeBoundLower;

                        leftEyeV2[7].x = (texture.width / 2) - points[41].x;
                        leftEyeV2[7].y = -(texture.height / 2) + points[41].y + eyeBoundLower;


                        //right eye
                        rightEyeV1[0].x = (texture.width / 2) - points[42].x;
                        rightEyeV1[0].y = -(texture.height / 2) + points[42].y - eyeBoundUpper;

                        rightEyeV1[1].x = (texture.width / 2) - points[43].x;
                        rightEyeV1[1].y = -(texture.height / 2) + points[43].y - eyeBoundUpper;

                        rightEyeV1[2].x = (texture.width / 2) - points[44].x;
                        rightEyeV1[2].y = -(texture.height / 2) + points[44].y - (eyeBoundUpper * 2);

                        rightEyeV1[3].x = (texture.width / 2) - points[44].x - eyeBoundUpper;
                        rightEyeV1[3].y = -(texture.height / 2) + points[44].y - (eyeBoundUpper * 2);

                        rightEyeV1[4].x = (texture.width / 2) - points[45].x - eyeBoundUpper;
                        rightEyeV1[4].y = -(texture.height / 2) + points[45].y;

                        rightEyeV1[5].x = (texture.width / 2) - points[45].x;
                        rightEyeV1[5].y = -(texture.height / 2) + points[45].y;

                        rightEyeV1[6].x = (texture.width / 2) - points[44].x;
                        rightEyeV1[6].y = -(texture.height / 2) + points[44].y;

                        rightEyeV1[7].x = (texture.width / 2) - points[43].x;
                        rightEyeV1[7].y = -(texture.height / 2) + points[43].y;

                        rightEyeV1[8].x = (texture.width / 2) - points[42].x;
                        rightEyeV1[8].y = -(texture.height / 2) + points[42].y;



                        rightEyeV2[0].x = (texture.width / 2) - points[42].x;
                        rightEyeV2[0].y = -(texture.height / 2) + points[42].y + 2;

                        rightEyeV2[1].x = (texture.width / 2) - points[47].x;
                        rightEyeV2[1].y = -(texture.height / 2) + points[47].y + 2;

                        rightEyeV2[2].x = (texture.width / 2) - points[46].x;
                        rightEyeV2[2].y = -(texture.height / 2) + points[46].y + 2;

                        rightEyeV2[3].x = (texture.width / 2) - points[45].x;
                        rightEyeV2[3].y = -(texture.height / 2) + points[45].y + 2;

                        rightEyeV2[4].x = (texture.width / 2) - points[45].x - eyeBoundLower;
                        rightEyeV2[4].y = -(texture.height / 2) + points[45].y + 2;

                        rightEyeV2[5].x = (texture.width / 2) - points[46].x;
                        rightEyeV2[5].y = -(texture.height / 2) + points[46].y + (eyeBoundLower);

                        rightEyeV2[6].x = (texture.width / 2) - points[47].x;
                        rightEyeV2[6].y = -(texture.height / 2) + points[47].y + (eyeBoundLower);

                        rightEyeV2[7].x = (texture.width / 2) - points[42].x;
                        rightEyeV2[7].y = -(texture.height / 2) + points[42].y + (eyeBoundLower);

                        //lip
                        for (int i = 48; i < 68; i++)
                        {
                            lipV[i - 48].x = (texture.width / 2) - points[i].x;
                            lipV[i - 48].y = -(texture.height / 2) + points[i].y;
                        }

                        int rear = 9;

                        for (int i = 17; i < 22; i++)
                        {
                            leftEyebrowV[i - 17].x = (texture.width / 2) - points[i].x;
                            leftEyebrowV[i - 17].y = -(texture.height / 2) + points[i].y - ebUpper;

                            leftEyebrowV[rear - i + 17].x = (texture.width / 2) - points[i].x;
                            leftEyebrowV[rear - i + 17].y = -(texture.height / 2) + points[i].y + ebLower;
                        }

                        for (int i = 22; i < 27; i++)
                        {
                            rightEyebrowV[i - 22].x = (texture.width / 2) - points[i].x;
                            rightEyebrowV[i - 22].y = -(texture.height / 2) + points[i].y - ebUpper;

                            rightEyebrowV[rear - i + 22].x = (texture.width / 2) - points[i].x;
                            rightEyebrowV[rear - i + 22].y = -(texture.height / 2) + points[i].y + ebLower;
                        }

                        leftEyeMF1.mesh.vertices     = leftEyeV1;
                        leftEyeMF2.mesh.vertices     = leftEyeV2;
                        rightEyeMF1.mesh.vertices    = rightEyeV1;
                        rightEyeMF2.mesh.vertices    = rightEyeV2;
                        lipMF.mesh.vertices          = lipV;
                        leftEyebrowMF.mesh.vertices  = leftEyebrowV;
                        rightEyebrowMF.mesh.vertices = rightEyebrowV;
                    }

                    texture.SetPixels32(colors);
                    texture.Apply(false);
                }
            }
        }
Пример #19
0
        // Update is called once per frame
        void Update()
        {
            if (adjustPixelsDirection)
            {
                // Catch the orientation change of the screen.
                if (screenOrientation != Screen.orientation && (screenWidth != Screen.width || screenHeight != Screen.height))
                {
                    Initialize();
                }
                else
                {
                    screenWidth  = Screen.width;
                    screenHeight = Screen.height;
                }
            }


            if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
            {
                int       middle_x = 0, middle_y = 0;
                Color32[] colors = GetColors();

                if (colors != null)
                {
                    faceLandmarkDetector.SetImage <Color32> (colors, texture.width, texture.height, 4, true);

                    //detect face rects
                    List <Rect> detectResult = faceLandmarkDetector.Detect();

                    foreach (var rect in detectResult)
                    {
                        //Debug.Log ("face : " + rect);

                        //detect landmark points
                        points   = faceLandmarkDetector.DetectLandmark(rect);
                        middle_x = (int)((points[29].x + points[1].x) / 2);
                        middle_y = (int)((points[29].y + points[1].y) / 2);
                    }
                    if (btnPressed)
                    {
                        //최종 r, g, b값
                        double result_r = 0, result_g = 0, result_b = 0;

                        //잘라낼 사진 높이, 넓이
                        int width  = 10;
                        int height = 10;

                        //추출 이미지 중심 랜드마크 포인트 좌표 로그 출력
                        Debug.Log("middle coordinate x:" + middle_x + " y:" + middle_y);

                        //카메라 이미지 잘라서 "saved.png" 라는 의미로 Resource 경로에 저장
                        SaveImage(middle_x, middle_y);

                        UnityEngine.Rect rect = new UnityEngine.Rect(0, 0, width, height);

                        double sum_r = 0, sum_g = 0, sum_b = 0;

                        //잘라낸 이미지 텍스쳐로 변환
                        Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
                        tex = Resources.Load <Texture2D>("saved1");


                        //텍스처로부터 rgb추출
                        tex.ReadPixels(rect, 0, 0);
                        for (int pixel_y = 0; pixel_y < height; ++pixel_y)
                        {
                            for (int pixel_x = 0; pixel_x < width; ++pixel_x)
                            {
                                Color c = tex.GetPixel(pixel_x, pixel_y);

                                Debug.LogFormat("pixel[{0},{1}] = {2}, R:{3} G:{4} B:{5}", pixel_x, pixel_y, c, c.r * 255, c.g * 255, c.b * 255);
                                sum_r += c.r * 255;
                                sum_g += c.g * 255;
                                sum_b += c.b * 255;
                            }
                        }
                        //[결과] rgb = 각 픽셀 rgb 평균값
                        result_r = sum_r / (width * height);
                        result_g = sum_g / (width * height);
                        result_b = sum_b / (width * height);

                        Debug.LogFormat(" [평균] R:{0} G:{1} B:{2}", result_r, result_g, result_b);

                        webCamTexture.Stop();

                        //피부톤 분류
                        if (result_r > result_b)
                        {
                            skinTone = "웜";
                        }
                        else
                        {
                            skinTone = "쿨";
                        }
                        Debug.Log("피부톤: " + skinTone);
                        //script = GameObject.FindObjectOfType<Result>();

                        //	Debug.Log(GameObject.FindObjectOfType<Script>());
                        //	script.GetSkinTone(skinTone);
                    }
                    faceLandmarkDetector.DrawDetectResult <Color32> (colors, texture.width, texture.height, 4, true, 107, 255, 64, 255, 2);
                    texture.SetPixels32(colors);
                    texture.Apply(false);
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (!hasInitDone)
            {
                return;
            }

            if (screenOrientation != Screen.orientation)
            {
                screenOrientation = Screen.orientation;
                UpdateLayout();
            }

            #if UNITY_IOS && !UNITY_EDITOR && (UNITY_4_6_3 || UNITY_4_6_4 || UNITY_5_0_0 || UNITY_5_0_1)
            if (webCamTexture.width > 16 && webCamTexture.height > 16)
            {
            #else
            if (webCamTexture.didUpdateThisFrame)
            {
                #endif

                webCamTexture.GetPixels32(colors);
                faceLandmarkDetector.SetImage <Color32> (colors, webCamTexture.width, webCamTexture.height, 4, flip);

                //detect face rects
                List <Rect> detectResult = faceLandmarkDetector.Detect();

                foreach (var rect in detectResult)
                {
                    //Debug.Log ("face : " + rect);

                    //detect landmark points
                    faceLandmarkDetector.DetectLandmark(rect);

                    //draw landmark points
                    faceLandmarkDetector.DrawDetectLandmarkResult <Color32> (colors, webCamTexture.width, webCamTexture.height, 4, flip, 0, 255, 0, 255);
                }

                //draw face rect
                faceLandmarkDetector.DrawDetectResult <Color32> (colors, webCamTexture.width, webCamTexture.height, 4, flip, 255, 0, 0, 255, 2);

                texture2D.SetPixels32(colors);
                texture2D.Apply();
            }
        }

        /// <summary>
        /// Raises the destroy event.
        /// </summary>
        void OnDestroy()
        {
            if (webCamTexture != null)
            {
                webCamTexture.Stop();
            }
            if (faceLandmarkDetector != null)
            {
                faceLandmarkDetector.Dispose();
            }

            #if UNITY_WEBGL && !UNITY_EDITOR
            foreach (var coroutine in coroutines)
            {
                StopCoroutine(coroutine);
                ((IDisposable)coroutine).Dispose();
            }
            #endif
        }
Пример #21
0
        private void Run()
        {
            if (string.IsNullOrEmpty(dlibShapePredictorFilePath))
            {
                Debug.LogError("shape predictor file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
            }

            //if true, The error log of the Native side Dlib will be displayed on the Unity Editor Console.
            Utils.setDebugMode(true);

            Texture2D dstTexture2D = new Texture2D(texture2D.width, texture2D.height, texture2D.format, false);

            Graphics.CopyTexture(texture2D, dstTexture2D);

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

            float width  = gameObject.transform.localScale.x;
            float height = gameObject.transform.localScale.y;

            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;
            }

            FaceLandmarkDetector faceLandmarkDetector = new FaceLandmarkDetector(dlibShapePredictorFilePath);

            faceLandmarkDetector.SetImage(texture2D);

            //detect face rects
            List <Rect> detectResult = faceLandmarkDetector.Detect();


            foreach (var rect in detectResult)
            {
                Debug.Log("face : " + rect);

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

                Debug.Log("face points count : " + points.Count);
                foreach (var point in points)
                {
                    Debug.Log("face point : x " + point.x + " y " + point.y);
                }

                //draw landmark points
                faceLandmarkDetector.DrawDetectLandmarkResult(dstTexture2D, 0, 255, 0, 255);
            }

            //draw face rect
            faceLandmarkDetector.DrawDetectResult(dstTexture2D, 255, 0, 0, 255, 2);

            faceLandmarkDetector.Dispose();

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


            Utils.setDebugMode(false);

            if (fpsMonitor != null)
            {
                fpsMonitor.Add("dlib shape predictor", dlibShapePredictorFileName);
                fpsMonitor.Add("width", width.ToString());
                fpsMonitor.Add("height", height.ToString());
                fpsMonitor.Add("orientation", Screen.orientation.ToString());
            }
        }
Пример #22
0
        private void Run()
        {
            if (string.IsNullOrEmpty(object_detector_filepath))
            {
                Debug.LogError("object detecter file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
            }
            if (string.IsNullOrEmpty(shape_predictor_filepath))
            {
                Debug.LogError("shape predictor file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
            }

            Texture2D dstTexture2D = new Texture2D(texture2D.width, texture2D.height, texture2D.format, false);

            Graphics.CopyTexture(texture2D, dstTexture2D);

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

            float width  = gameObject.transform.localScale.x;
            float height = gameObject.transform.localScale.y;

            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;
            }

            FaceLandmarkDetector faceLandmarkDetector = new FaceLandmarkDetector(object_detector_filepath, shape_predictor_filepath);

            faceLandmarkDetector.SetImage(texture2D);

            //detect face rects
            List <Rect> detectResult = faceLandmarkDetector.Detect();

            foreach (var rect in detectResult)
            {
                Debug.Log("face : " + rect);

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

                Debug.Log("face points count : " + points.Count);
                foreach (var point in points)
                {
                    Debug.Log("face point : x " + point.x + " y " + point.y);
                }

                //draw landmark points
                faceLandmarkDetector.DrawDetectLandmarkResult(dstTexture2D, 0, 255, 0, 255);
            }

            //draw face rects
            faceLandmarkDetector.DrawDetectResult(dstTexture2D, 255, 0, 0, 255, 3);

            faceLandmarkDetector.Dispose();

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

            if (fpsMonitor != null)
            {
                fpsMonitor.Add("dlib object detector", "frontal_cat_face.svm");
                fpsMonitor.Add("dlib shape predictor", "sp_cat_face_68.dat");
                fpsMonitor.Add("width", width.ToString());
                fpsMonitor.Add("height", height.ToString());
                fpsMonitor.Add("orientation", Screen.orientation.ToString());
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (adjustPixelsDirection)
            {
                // Catch the orientation change of the screen.
                if (screenOrientation != Screen.orientation && (screenWidth != Screen.width || screenHeight != Screen.height))
                {
                    Initialize();
                }
                else
                {
                    screenWidth  = Screen.width;
                    screenHeight = Screen.height;
                }
            }


            if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
            {
                Color32[] colors = GetColors();

                if (colors != null)
                {
                    faceLandmarkDetector.SetImage <Color32>(colors, texture.width, texture.height, 4, true);

                    //detect face rects
                    List <Rect> detectResult = faceLandmarkDetector.Detect();

                    //restore landmark informations
                    List <Vector2> landmarks = null;

                    foreach (var rect in detectResult)
                    {
                        //Debug.Log ("face : " + rect);

                        //detect landmark points
                        landmarks = faceLandmarkDetector.DetectLandmark(rect);

                        //draw landmark points
                        faceLandmarkDetector.DrawDetectLandmarkResult <Color32>(colors, texture.width, texture.height, 4, true, 0, 255, 0, 255);
                    }

                    //if (landmarks != null)
                    //{
                    //	Console.Write("landmark.size()=" + landmarks.Count.ToString() + "\n");
                    //	foreach (var i in landmarks)
                    //		Console.Write(i.x.ToString() + "," + i.y.ToString() + "  ");
                    //	Console.Write("\n");
                    //}
                    //else
                    //	Console.WriteLine("landmarks=null");
                    if (landmarks != null)
                    {
                        faceToFace.UpdateFacial(new FaceLandmarks(landmarks));
                    }

                    //draw face rect
                    faceLandmarkDetector.DrawDetectResult <Color32>(colors, texture.width, texture.height, 4, true, 255, 0, 0, 255, 2);

                    texture.SetPixels32(colors);
                    texture.Apply();
                }
            }
        }