WebCamTextureProcesser.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using OpenCVForUnity;
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.ImgprocModule;
  4. using OpenCVForUnity.UnityUtils;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. /// <summary>
  10. /// WebCam色彩图处理
  11. /// </summary>
  12. public class WebCamTextureProcesser : MonoBehaviour
  13. {
  14. public static WebCamTextureProcesser Instance;
  15. private WebCamDevice webCamDevice;
  16. private WebCamTexture webCamTexture;
  17. private Mat rgbaMat;
  18. private Color32[] colors;
  19. private bool isInitWaiting = false;
  20. private bool hasInitDone = false; // 如果Init完毕
  21. // ==================================================
  22. private void Awake() => Instance = this;
  23. private void Start() => Init();
  24. private void Update() => ProcessWebCamTexture();
  25. private void OnDestroy() => Dispose();
  26. // ==================================================
  27. private void Init()
  28. {
  29. if (isInitWaiting)
  30. {
  31. return;
  32. }
  33. StartCoroutine(nameof(InitAction));
  34. return;
  35. }
  36. private IEnumerator InitAction()
  37. {
  38. if (hasInitDone) // 防止重复安装
  39. {
  40. Dispose();
  41. }
  42. isInitWaiting = true;
  43. CreateCamera(); // 创建相机
  44. while (true)
  45. {
  46. if (webCamTexture.didUpdateThisFrame)
  47. {
  48. isInitWaiting = false;
  49. hasInitDone = true;
  50. if (colors == null || colors.Length != webCamTexture.width * webCamTexture.height) // 确定color尺寸
  51. {
  52. colors = new Color32[webCamTexture.width * webCamTexture.height];
  53. }
  54. if (orginalPreviewTexture2D == null || orginalPreviewTexture2D.width != webCamTexture.width || orginalPreviewTexture2D.height != webCamTexture.height) // 确定texture2d尺寸
  55. {
  56. orginalPreviewTexture2D = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
  57. }
  58. rgbaMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4, new Scalar(0, 0, 0, 255)); // 高、宽
  59. // UpdateOriginalPreview(); // ?这里为什么会有预览
  60. break;
  61. }
  62. else
  63. {
  64. yield return null;
  65. }
  66. }
  67. yield break;
  68. }
  69. // ==================================================
  70. #region 色彩画面处理
  71. private void ProcessWebCamTexture()
  72. {
  73. if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
  74. {
  75. Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
  76. transform.GetComponent<FacialRecognitionHandler>().DetectFace(rgbaMat); // 传入mat 检测人脸 // 会导致原数据反转?
  77. UpdateOriginalPreview();
  78. }
  79. return;
  80. }
  81. #endregion
  82. // ==================================================
  83. #region 释放资源
  84. private void Dispose()
  85. {
  86. isInitWaiting = false;
  87. hasInitDone = false;
  88. if (webCamTexture != null)
  89. {
  90. webCamTexture.Stop();
  91. Destroy(webCamTexture);
  92. webCamTexture = null;
  93. }
  94. if (rgbaMat != null)
  95. {
  96. rgbaMat.Dispose();
  97. rgbaMat = null;
  98. }
  99. if (orginalPreviewTexture2D != null)
  100. {
  101. Destroy(orginalPreviewTexture2D);
  102. orginalPreviewTexture2D = null;
  103. }
  104. return;
  105. }
  106. #endregion
  107. // ==================================================
  108. #region 创建、配置WebCamera
  109. [SerializeField] private string requestedDeviceName = "MX Brio"; // "GC21 Video" // "Logitech BRIO"
  110. private int requestedWidth = 480; // 440
  111. private int requestedHeight = 270; // 440
  112. private int requestedFPS = 30;
  113. private void CreateCamera()
  114. {
  115. foreach (WebCamDevice device in WebCamTexture.devices)
  116. {
  117. if (device.name == requestedDeviceName)
  118. {
  119. webCamDevice = device;
  120. webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
  121. webCamTexture.Play();
  122. Debug.Log($"[WTP] Name:{webCamTexture.deviceName} / Width:{webCamTexture.width} / Height:{webCamTexture.height} / FPS:{webCamTexture.requestedFPS}");
  123. Debug.Log($"[WTP] VideoRotationAngle:{webCamTexture.videoRotationAngle} / VideoVerticallyMirrored:{webCamTexture.videoVerticallyMirrored} / IsFrongFacing:{webCamDevice.isFrontFacing}");
  124. return;
  125. }
  126. }
  127. return;
  128. }
  129. #endregion
  130. // ==================================================
  131. #region 预览画面
  132. private bool allowPreview = false;
  133. public void SwitchPreview(bool value)
  134. {
  135. allowPreview = value;
  136. return;
  137. }
  138. [SerializeField] private Image originalPreviewImage;
  139. private Texture2D orginalPreviewTexture2D;
  140. private void UpdateOriginalPreview()
  141. {
  142. if (!allowPreview)
  143. {
  144. return;
  145. }
  146. if (originalPreviewImage == null)
  147. {
  148. return;
  149. }
  150. Utils.matToTexture2D(rgbaMat, orginalPreviewTexture2D, colors); // Mat更新为texture2d
  151. // texture2D = RotateTexutre(texture2D, false);
  152. originalPreviewImage.sprite = Sprite.Create(orginalPreviewTexture2D, new UnityEngine.Rect(0, 0, orginalPreviewTexture2D.width, orginalPreviewTexture2D.height), Vector2.zero);
  153. return;
  154. }
  155. #endregion
  156. // ==================================================
  157. // 按钮
  158. public void OnPlayButtonClick()
  159. {
  160. if (hasInitDone)
  161. {
  162. webCamTexture.Play();
  163. }
  164. return;
  165. }
  166. public void OnPauseButtonClick()
  167. {
  168. if (hasInitDone)
  169. {
  170. webCamTexture.Pause();
  171. }
  172. return;
  173. }
  174. public void OnStopButtonClick()
  175. {
  176. if (hasInitDone)
  177. {
  178. webCamTexture.Stop();
  179. }
  180. return;
  181. }
  182. // ==================================================
  183. // Texture画面旋转 // 不必要的 // 旋转处理在FaceDetecter中进行
  184. // private Texture2D RotateTexutre(Texture2D originalTexture, bool clockwise)
  185. // {
  186. // Color32[] original = originalTexture.GetPixels32();
  187. // Color32[] rotated = new Color32[original.Length];
  188. // int w = originalTexture.width;
  189. // int h = originalTexture.height;
  190. // int iRotated, iOriginal;
  191. // for (int j = 0; j < h; ++j)
  192. // {
  193. // for (int i = 0; i < w; ++i)
  194. // {
  195. // iRotated = (i + 1) * h - j - 1;
  196. // iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
  197. // rotated[iRotated] = original[iOriginal];
  198. // }
  199. // }
  200. // Texture2D newTexture = new Texture2D(originalTexture.height, originalTexture.width, TextureFormat.RGBA32, false);
  201. // newTexture.SetPixels32(rotated);
  202. // newTexture.Apply();
  203. // return newTexture;
  204. // }
  205. }