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(); }
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; }
// 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; }
// 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); } } } }
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); } } }
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()); } }
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 }