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());
            }
        }
        // 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();
        }
Пример #3
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;
        }
Пример #5
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();
            }
        }
        // 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;
        }
Пример #8
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);
            }
        }
    }
Пример #9
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());
            }
        }
Пример #10
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());
            }
        }
        // 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();
                }
            }
        }
        // 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
        }
Пример #13
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);
                }
            }
        }
Пример #14
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);
        }