1
0
MirzkisD1Ex0 1 жил өмнө
parent
commit
e4b8ce907a

+ 59 - 18
Materials/OpenCVModify/FaceDetecter.cs

@@ -14,11 +14,11 @@ namespace DiageoWhiskyBlending
 {
   public class FaceDetecter : MonoBehaviour
   {
-    private string cascadePath; // 人脸识别训练数据文件xml的路径
     private Mat gray; // 灰度图,方便识别
     private Mat rotatedNewMat;
     private MatOfRect faceRect; // 识别到的人脸的区域
     private CascadeClassifier classifier; // 人脸识别分类器
+    private string cascadePath;
 
     public float index = 0;
 
@@ -30,23 +30,55 @@ namespace DiageoWhiskyBlending
       Init();
     }
 
+    private void OnDestroy()
+    {
+      Dispose();
+    }
+
+    private void OnApplicationQuit()
+    {
+      Dispose();
+    }
+
     // ==================================================
 
     private void Init()
     {
-      cascadePath = Application.streamingAssetsPath + "/haarcascade_frontalface_alt2.xml";   //读取人脸识别训练数据xml
+      cascadePath = Application.streamingAssetsPath + "/haarcascade_frontalface_alt2.xml";
       gray = new Mat(); // 初始化Mat
       faceRect = new MatOfRect(); // 初始化识别到的人脸的区域
       classifier = new CascadeClassifier(cascadePath); // 初始化人脸识别分类器
+
+      previewMat = new Mat();
+      previewTexture2D = new Texture2D(440, 440, TextureFormat.RGBA32, false);
+      return;
+    }
+
+    private void Dispose()
+    {
+      if (rotatedNewMat != null)
+      {
+        rotatedNewMat.Dispose();
+        rotatedNewMat = null;
+      }
+
+      if (previewMat != null)
+      {
+        previewMat.Dispose();
+        previewMat = null;
+      }
+      if (previewTexture2D != null)
+      {
+        Destroy(previewTexture2D);
+        previewTexture2D = null;
+      }
       return;
     }
 
 
     public void DetectFace(Mat rgbaMat)
     {
-      rotatedNewMat = rgbaMat.clone();
-      rotatedNewMat = MatRotate(rotatedNewMat); // 旋转原数据
-
+      rotatedNewMat = MatRotate(rgbaMat.clone()); // 旋转原数据
 
       Imgproc.cvtColor(rotatedNewMat, gray, Imgproc.COLOR_RGBA2GRAY); // 将获取到的摄像头画面转化为灰度图并赋值给gray
 
@@ -56,40 +88,49 @@ namespace DiageoWhiskyBlending
       OpenCVForUnity.CoreModule.Rect[] rects = faceRect.toArray();
       if (rects.Length > 0)
       {
-        Debug.Log("检测到面部...[OK]");
         for (int i = 0; i < rects.Length; i++)
         {
           Imgproc.rectangle(rotatedNewMat, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 255, 0, 255), 2);  //在原本的画面中画框,框出人脸额位置,其中rects[i].x和rects[i].y为框的左上角的顶点,rects[i].width、rects[i].height即为框的宽和高
         }
 
         index += Time.deltaTime;
-        if (index > 1.5f)
+        if (index > .3f)
         {
-          Debug.Log("asdasdadasdasdasd");
+          Debug.Log("连续监测超过1s...[OK]");
+          GameManager.Instance.EnterLogicScene00();
         }
       }
       else
       {
+        // if (index > 0)
+        // {
+        //   index -= Time.deltaTime / 2;
+        //   if (index < 0)
+        //   {
         index = 0;
+        // }
+        // }
       }
 
       // 深复制识别、画框数据并显示
-      if (PreviewImage)
-      {
-        FinalMatPreview(rotatedNewMat);
-      }
+      UpdatePreview(rotatedNewMat);
       return;
     }
 
     // ==================================================
     // 预览画面
-    public Image PreviewImage;
-    private void FinalMatPreview(Mat previewMat)
+    public Image ImagePreviewImage;
+    private Mat previewMat;
+    private Texture2D previewTexture2D;
+    private void UpdatePreview(Mat value)
     {
-      Mat tempMat = previewMat.clone();
-      Texture2D tempTexture2D = new Texture2D(tempMat.width(), tempMat.height(), TextureFormat.RGBA32, false);
-      Utils.matToTexture2D(previewMat, tempTexture2D);
-      PreviewImage.sprite = Sprite.Create(tempTexture2D, new UnityEngine.Rect(0, 0, 440, 440), Vector2.zero);
+      if (!ImagePreviewImage)
+      {
+        return;
+      }
+      previewMat = value.clone();
+      Utils.matToTexture2D(previewMat, previewTexture2D);
+      ImagePreviewImage.sprite = Sprite.Create(previewTexture2D, new UnityEngine.Rect(0, 0, 440, 440), Vector2.zero);
       return;
     }
 

+ 50 - 38
Materials/OpenCVModify/WebCamTextureToMat.cs

@@ -17,7 +17,6 @@ namespace DiageoWhiskyBlending
     private Mat rgbaMat;
 
     private Color32[] colors;
-    private Texture2D texture2D;
     private bool isInitWaiting = false;
     private bool hasInitDone = false; // 如果Init完毕
 
@@ -33,11 +32,9 @@ namespace DiageoWhiskyBlending
       if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
       {
         Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
-        //Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
-
         transform.GetComponent<FaceDetecter>().DetectFace(rgbaMat); // 传入mat 检测人脸 // 会导致原数据反转?
 
-        UpdateGraphic();
+        // UpdatePreview();
       }
     }
 
@@ -46,6 +43,11 @@ namespace DiageoWhiskyBlending
       Dispose();
     }
 
+    private void OnApplicationQuit()
+    {
+      Dispose();
+    }
+
     // ==================================================
 
     private void Init()
@@ -81,13 +83,14 @@ namespace DiageoWhiskyBlending
           {
             colors = new Color32[webCamTexture.width * webCamTexture.height];
           }
-          if (texture2D == null || texture2D.width != webCamTexture.width || texture2D.height != webCamTexture.height) // 确定texture2d尺寸
+          if (orginalPreviewTexture2D == null || orginalPreviewTexture2D.width != webCamTexture.width || orginalPreviewTexture2D.height != webCamTexture.height) // 确定texture2d尺寸
           {
-            texture2D = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
+            orginalPreviewTexture2D = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
           }
 
           rgbaMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4, new Scalar(0, 0, 0, 255)); // 高、宽
-          UpdateGraphic();
+
+          // UpdatePreview();
           break;
         }
         else
@@ -117,10 +120,10 @@ namespace DiageoWhiskyBlending
         rgbaMat.Dispose();
         rgbaMat = null;
       }
-      if (texture2D != null)
+      if (orginalPreviewTexture2D != null)
       {
-        Destroy(texture2D);
-        texture2D = null;
+        Destroy(orginalPreviewTexture2D);
+        orginalPreviewTexture2D = null;
       }
       return;
     }
@@ -178,40 +181,49 @@ namespace DiageoWhiskyBlending
     }
 
     // ==================================================
-    // 画面更新
-    private void UpdateGraphic()
+    // 预览画面
+
+    public Image ImageOrginalPreview;
+    private Texture2D orginalPreviewTexture2D;
+    private void UpdatePreview()
     {
-      Utils.matToTexture2D(rgbaMat, texture2D, colors); // Mat更新为texture2d
+      if (!ImageOrginalPreview)
+      {
+        return;
+      }
+
+      Utils.matToTexture2D(rgbaMat, orginalPreviewTexture2D, colors); // Mat更新为texture2d
+
       // texture2D = RotateTexutre(texture2D, false);
-      gameObject.GetComponent<Image>().sprite = Sprite.Create(texture2D, new UnityEngine.Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
+      ImageOrginalPreview.sprite = Sprite.Create(orginalPreviewTexture2D, new UnityEngine.Rect(0, 0, orginalPreviewTexture2D.width, orginalPreviewTexture2D.height), Vector2.zero);
       return;
     }
 
     // ==================================================
     //  Texture画面旋转
-    private Texture2D RotateTexutre(Texture2D originalTexture, bool clockwise)
-    {
-      Color32[] original = originalTexture.GetPixels32();
-      Color32[] rotated = new Color32[original.Length];
-      int w = originalTexture.width;
-      int h = originalTexture.height;
-
-      int iRotated, iOriginal;
-
-      for (int j = 0; j < h; ++j)
-      {
-        for (int i = 0; i < w; ++i)
-        {
-          iRotated = (i + 1) * h - j - 1;
-          iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
-          rotated[iRotated] = original[iOriginal];
-        }
-      }
-
-      Texture2D newTexture = new Texture2D(originalTexture.height, originalTexture.width, TextureFormat.RGBA32, false);
-      newTexture.SetPixels32(rotated);
-      newTexture.Apply();
-      return newTexture;
-    }
+    // private Texture2D RotateTexutre(Texture2D originalTexture, bool clockwise)
+    // {
+    //   Color32[] original = originalTexture.GetPixels32();
+    //   Color32[] rotated = new Color32[original.Length];
+    //   int w = originalTexture.width;
+    //   int h = originalTexture.height;
+
+    //   int iRotated, iOriginal;
+
+    //   for (int j = 0; j < h; ++j)
+    //   {
+    //     for (int i = 0; i < w; ++i)
+    //     {
+    //       iRotated = (i + 1) * h - j - 1;
+    //       iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
+    //       rotated[iRotated] = original[iOriginal];
+    //     }
+    //   }
+
+    //   Texture2D newTexture = new Texture2D(originalTexture.height, originalTexture.width, TextureFormat.RGBA32, false);
+    //   newTexture.SetPixels32(rotated);
+    //   newTexture.Apply();
+    //   return newTexture;
+    // }
   }
 }