1
0

FaceDetecter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. // using OpenCVSharp;
  6. using OpenCVForUnity;
  7. using OpenCVForUnity.CoreModule;
  8. using OpenCVForUnity.ImgprocModule;
  9. using OpenCVForUnity.UnityUtils;
  10. using OpenCVForUnity.ObjdetectModule;
  11. namespace DiageoWhiskyBlending
  12. {
  13. public class FaceDetecter : MonoBehaviour
  14. {
  15. private string cascadePath; // 人脸识别训练数据文件xml的路径
  16. private Mat gray; // 灰度图,方便识别
  17. private Mat rotatedNewMat;
  18. private MatOfRect faceRect; // 识别到的人脸的区域
  19. private CascadeClassifier classifier; // 人脸识别分类器
  20. public float index = 0;
  21. // ==================================================
  22. private void Start()
  23. {
  24. // LogAllWebCam();
  25. Init();
  26. }
  27. // ==================================================
  28. private void Init()
  29. {
  30. cascadePath = Application.streamingAssetsPath + "/haarcascade_frontalface_alt2.xml"; //读取人脸识别训练数据xml
  31. gray = new Mat(); // 初始化Mat
  32. faceRect = new MatOfRect(); // 初始化识别到的人脸的区域
  33. classifier = new CascadeClassifier(cascadePath); // 初始化人脸识别分类器
  34. return;
  35. }
  36. public void DetectFace(Mat rgbaMat)
  37. {
  38. rotatedNewMat = rgbaMat.clone();
  39. rotatedNewMat = MatRotate(rotatedNewMat); // 旋转原数据
  40. Imgproc.cvtColor(rotatedNewMat, gray, Imgproc.COLOR_RGBA2GRAY); // 将获取到的摄像头画面转化为灰度图并赋值给gray
  41. // mat/面部矩形向量组/识别精度越高越快越不准/面部识别次数2次以上算识别/?性能优化/最小检测尺寸/最大检测尺寸
  42. classifier.detectMultiScale(gray, faceRect, 1.1d, 2, 2, new Size(20, 20), new Size()); // 检测gray中的人脸
  43. OpenCVForUnity.CoreModule.Rect[] rects = faceRect.toArray();
  44. if (rects.Length > 0)
  45. {
  46. Debug.Log("检测到面部...[OK]");
  47. for (int i = 0; i < rects.Length; i++)
  48. {
  49. 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即为框的宽和高
  50. }
  51. index += Time.deltaTime;
  52. if (index > 1.5f)
  53. {
  54. Debug.Log("asdasdadasdasdasd");
  55. }
  56. }
  57. else
  58. {
  59. index = 0;
  60. }
  61. // 深复制识别、画框数据并显示
  62. if (PreviewImage)
  63. {
  64. FinalMatPreview(rotatedNewMat);
  65. }
  66. return;
  67. }
  68. // ==================================================
  69. // 预览画面
  70. public Image PreviewImage;
  71. private void FinalMatPreview(Mat previewMat)
  72. {
  73. Mat tempMat = previewMat.clone();
  74. Texture2D tempTexture2D = new Texture2D(tempMat.width(), tempMat.height(), TextureFormat.RGBA32, false);
  75. Utils.matToTexture2D(previewMat, tempTexture2D);
  76. PreviewImage.sprite = Sprite.Create(tempTexture2D, new UnityEngine.Rect(0, 0, 440, 440), Vector2.zero);
  77. return;
  78. }
  79. // ==================================================
  80. /// <summary>
  81. /// Mat旋转方法
  82. /// </summary>
  83. /// <param name="orginalMat"></param>
  84. /// <returns></returns>
  85. private Mat MatRotate(Mat orginalMat)
  86. {
  87. Mat rotatedMat = new Mat(orginalMat.height(), orginalMat.width(), CvType.CV_8UC4, new Scalar(0, 0, 0, 255)); // DEBUG:宽高可能相反
  88. // mat旋转
  89. Point center = new Point(orginalMat.cols() / 2f, orginalMat.rows() / 2f);
  90. Mat mat = Imgproc.getRotationMatrix2D(center, 90, 1);
  91. Imgproc.warpAffine(orginalMat, rotatedMat, mat, orginalMat.size());
  92. return rotatedMat;
  93. }
  94. /// <summary>
  95. /// 预览有多少相机
  96. /// </summary>
  97. private void LogAllWebCam()
  98. {
  99. Debug.Log($"Found {WebCamTexture.devices.Length} device...<color=green>[OK]</color>");
  100. foreach (WebCamDevice webcamDevice in WebCamTexture.devices)
  101. {
  102. Debug.Log($"{webcamDevice.name}...<color=green>[OK]</color>");
  103. }
  104. return;
  105. }
  106. }
  107. }