|
| 1 | +using MagicLeapWithOpenCVForUnity.UnityUtils.Helper; |
| 2 | +using OpenCVForUnity.CoreModule; |
| 3 | +using OpenCVForUnity.ImgprocModule; |
| 4 | +using OpenCVForUnity.ObjdetectModule; |
| 5 | +using OpenCVForUnity.UnityUtils; |
| 6 | +using OpenCVForUnity.UnityUtils.Helper; |
| 7 | +using OpenCVForUnityExample; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using UnityEngine; |
| 11 | +using UnityEngine.SceneManagement; |
| 12 | + |
| 13 | +namespace MagicLeapWithOpenCVForUnityExample |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// DetectFace MLCameraPreview Example |
| 17 | + /// </summary> |
| 18 | + [RequireComponent(typeof(MLCameraPreviewToMatHelper), typeof(ImageOptimizationHelper))] |
| 19 | + public class DetectFaceMLCameraPreviewExample : MonoBehaviour |
| 20 | + { |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The texture. |
| 24 | + /// </summary> |
| 25 | + Texture2D texture; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The webcam texture to mat helper. |
| 29 | + /// </summary> |
| 30 | + MLCameraPreviewToMatHelper webCamTextureToMatHelper; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The image optimization helper. |
| 34 | + /// </summary> |
| 35 | + ImageOptimizationHelper imageOptimizationHelper; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// The FPS monitor. |
| 39 | + /// </summary> |
| 40 | + FpsMonitor fpsMonitor; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Determines if enable downscale. |
| 44 | + /// </summary> |
| 45 | + public bool enableDownScale; |
| 46 | + |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Determines if enable skipframe. |
| 50 | + /// </summary> |
| 51 | + public bool enableSkipFrame; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// rgbaMat |
| 55 | + /// </summary> |
| 56 | + Mat rgbaMat; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// The gray mat. |
| 60 | + /// </summary> |
| 61 | + Mat grayMat; |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// The cascade. |
| 65 | + /// </summary> |
| 66 | + CascadeClassifier cascade; |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// detectResult |
| 70 | + /// </summary> |
| 71 | + OpenCVForUnity.CoreModule.Rect[] detectResult; |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// tokenSource |
| 75 | + /// </summary> |
| 76 | + CancellationTokenSource tokenSource = new CancellationTokenSource(); |
| 77 | + |
| 78 | + |
| 79 | + // Use this for initialization |
| 80 | + void Start() |
| 81 | + { |
| 82 | + fpsMonitor = GetComponent<FpsMonitor>(); |
| 83 | + |
| 84 | + imageOptimizationHelper = gameObject.GetComponent<ImageOptimizationHelper>(); |
| 85 | + webCamTextureToMatHelper = gameObject.GetComponent<MLCameraPreviewToMatHelper>(); |
| 86 | + webCamTextureToMatHelper.Initialize(); |
| 87 | + |
| 88 | + |
| 89 | + cascade = new CascadeClassifier(); |
| 90 | + //cascade.load (Utils.getFilePath ("lbpcascade_frontalface.xml")); |
| 91 | + cascade.load(Utils.getFilePath("haarcascade_frontalface_alt.xml")); |
| 92 | + if (cascade.empty()) |
| 93 | + { |
| 94 | + Debug.LogError("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. "); |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Raises the webcam texture to mat helper initialized event. |
| 101 | + /// </summary> |
| 102 | + public void OnWebCamTextureToMatHelperInitialized() |
| 103 | + { |
| 104 | + Debug.Log("OnWebCamTextureToMatHelperInitialized"); |
| 105 | + |
| 106 | + Mat webCamTextureMat = webCamTextureToMatHelper.GetMat(); |
| 107 | + Mat downscaleMat = imageOptimizationHelper.GetDownScaleMat(webCamTextureMat); |
| 108 | + |
| 109 | + texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); |
| 110 | + |
| 111 | + gameObject.GetComponent<Renderer>().material.mainTexture = texture; |
| 112 | + |
| 113 | + // gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1); |
| 114 | + // Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); |
| 115 | + |
| 116 | + if (fpsMonitor != null) |
| 117 | + { |
| 118 | + fpsMonitor.Add("deviceName", webCamTextureToMatHelper.GetDeviceName().ToString()); |
| 119 | + fpsMonitor.Add("width", webCamTextureToMatHelper.GetWidth().ToString()); |
| 120 | + fpsMonitor.Add("height", webCamTextureToMatHelper.GetHeight().ToString()); |
| 121 | + fpsMonitor.Add("downscaleRaito", imageOptimizationHelper.downscaleRatio.ToString()); |
| 122 | + fpsMonitor.Add("frameSkippingRatio", imageOptimizationHelper.frameSkippingRatio.ToString()); |
| 123 | + fpsMonitor.Add("downscale_width", downscaleMat.width().ToString()); |
| 124 | + fpsMonitor.Add("downscale_height", downscaleMat.height().ToString()); |
| 125 | + fpsMonitor.Add("orientation", Screen.orientation.ToString()); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + // float width = webCamTextureMat.width (); |
| 130 | + // float height = webCamTextureMat.height (); |
| 131 | + // |
| 132 | + // float widthScale = (float)Screen.width / width; |
| 133 | + // float heightScale = (float)Screen.height / height; |
| 134 | + // if (widthScale < heightScale) { |
| 135 | + // Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2; |
| 136 | + // } else { |
| 137 | + // Camera.main.orthographicSize = height / 2; |
| 138 | + // } |
| 139 | + |
| 140 | + |
| 141 | + rgbaMat = new Mat(webCamTextureMat.rows(), webCamTextureMat.cols(), CvType.CV_8UC4); |
| 142 | + grayMat = new Mat(downscaleMat.rows(), downscaleMat.cols(), CvType.CV_8UC1); |
| 143 | + |
| 144 | + //Main Process |
| 145 | + Process(); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Raises the webcam texture to mat helper disposed event. |
| 150 | + /// </summary> |
| 151 | + public void OnWebCamTextureToMatHelperDisposed() |
| 152 | + { |
| 153 | + Debug.Log("OnWebCamTextureToMatHelperDisposed"); |
| 154 | + |
| 155 | + |
| 156 | + // Cancel Task |
| 157 | + tokenSource.Cancel(); |
| 158 | + |
| 159 | + |
| 160 | + if (rgbaMat != null) |
| 161 | + rgbaMat.Dispose(); |
| 162 | + |
| 163 | + if (grayMat != null) |
| 164 | + grayMat.Dispose(); |
| 165 | + |
| 166 | + if (texture != null) |
| 167 | + { |
| 168 | + Texture2D.Destroy(texture); |
| 169 | + texture = null; |
| 170 | + } |
| 171 | + |
| 172 | + if (texture != null) |
| 173 | + { |
| 174 | + Texture2D.Destroy(texture); |
| 175 | + texture = null; |
| 176 | + } |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// Raises the webcam texture to mat helper error occurred event. |
| 182 | + /// </summary> |
| 183 | + /// <param name="errorCode">Error code.</param> |
| 184 | + public void OnWebCamTextureToMatHelperErrorOccurred(MLCameraPreviewToMatHelper.ErrorCode errorCode) |
| 185 | + { |
| 186 | + Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode); |
| 187 | + } |
| 188 | + |
| 189 | + // Update is called once per frame |
| 190 | + void Update() |
| 191 | + { |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + /// <summary> |
| 196 | + /// Raises the destroy event. |
| 197 | + /// </summary> |
| 198 | + void OnDestroy() |
| 199 | + { |
| 200 | + if (webCamTextureToMatHelper != null) |
| 201 | + webCamTextureToMatHelper.Dispose(); |
| 202 | + |
| 203 | + if (imageOptimizationHelper != null) |
| 204 | + imageOptimizationHelper.Dispose(); |
| 205 | + |
| 206 | + if (cascade != null) |
| 207 | + cascade.Dispose(); |
| 208 | + } |
| 209 | + |
| 210 | + /// <summary> |
| 211 | + /// Raises the back button click event. |
| 212 | + /// </summary> |
| 213 | + public void OnBackButtonClick() |
| 214 | + { |
| 215 | + SceneManager.LoadScene("OpenCVForUnityExample"); |
| 216 | + } |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// Raises the play button click event. |
| 220 | + /// </summary> |
| 221 | + public void OnPlayButtonClick() |
| 222 | + { |
| 223 | + webCamTextureToMatHelper.Play(); |
| 224 | + } |
| 225 | + |
| 226 | + /// <summary> |
| 227 | + /// Raises the pause button click event. |
| 228 | + /// </summary> |
| 229 | + public void OnPauseButtonClick() |
| 230 | + { |
| 231 | + webCamTextureToMatHelper.Pause(); |
| 232 | + } |
| 233 | + |
| 234 | + /// <summary> |
| 235 | + /// Raises the stop button click event. |
| 236 | + /// </summary> |
| 237 | + public void OnStopButtonClick() |
| 238 | + { |
| 239 | + webCamTextureToMatHelper.Stop(); |
| 240 | + } |
| 241 | + |
| 242 | + /// <summary> |
| 243 | + /// Raises the change camera button click event. |
| 244 | + /// </summary> |
| 245 | + public void OnChangeCameraButtonClick() |
| 246 | + { |
| 247 | + webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing(); |
| 248 | + } |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + /// <summary> |
| 253 | + /// Precess |
| 254 | + /// </summary> |
| 255 | + /// <returns></returns> |
| 256 | + private async void Process() |
| 257 | + { |
| 258 | + |
| 259 | + float DOWNSCALE_RATIO = 1.0f; |
| 260 | + |
| 261 | + while (true) |
| 262 | + { |
| 263 | + |
| 264 | + // Check TaskCancel |
| 265 | + if (tokenSource.Token.IsCancellationRequested) |
| 266 | + { |
| 267 | + break; |
| 268 | + } |
| 269 | + |
| 270 | + |
| 271 | + rgbaMat = webCamTextureToMatHelper.GetMat(); |
| 272 | + // Debug.Log ("rgbaMat.ToString() " + rgbaMat.ToString ()); |
| 273 | + |
| 274 | + Mat downScaleRgbaMat = null; |
| 275 | + DOWNSCALE_RATIO = 1.0f; |
| 276 | + if (enableDownScale) |
| 277 | + { |
| 278 | + downScaleRgbaMat = imageOptimizationHelper.GetDownScaleMat(rgbaMat); |
| 279 | + DOWNSCALE_RATIO = imageOptimizationHelper.downscaleRatio; |
| 280 | + } |
| 281 | + else |
| 282 | + { |
| 283 | + downScaleRgbaMat = rgbaMat; |
| 284 | + DOWNSCALE_RATIO = 1.0f; |
| 285 | + } |
| 286 | + Imgproc.cvtColor(downScaleRgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY); |
| 287 | + |
| 288 | + |
| 289 | + await Task.Run(() => |
| 290 | + { |
| 291 | + // detect faces on the downscale image |
| 292 | + if (!enableSkipFrame || !imageOptimizationHelper.IsCurrentFrameSkipped()) |
| 293 | + { |
| 294 | + |
| 295 | + using (Mat equalizeHistMat = new Mat()) |
| 296 | + using (MatOfRect faces = new MatOfRect()) |
| 297 | + { |
| 298 | + Imgproc.equalizeHist(grayMat, equalizeHistMat); |
| 299 | + |
| 300 | + cascade.detectMultiScale(equalizeHistMat, faces, 1.1f, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE, new Size(equalizeHistMat.cols() * 0.15, equalizeHistMat.cols() * 0.15), new Size()); |
| 301 | + |
| 302 | + |
| 303 | + //Debug.Log("faces.dump() " + faces.dump()); |
| 304 | + |
| 305 | + detectResult = faces.toArray(); |
| 306 | + } |
| 307 | + |
| 308 | + if (enableDownScale) |
| 309 | + { |
| 310 | + for (int i = 0; i < detectResult.Length; ++i) |
| 311 | + { |
| 312 | + var rect = detectResult[i]; |
| 313 | + detectResult[i] = new OpenCVForUnity.CoreModule.Rect( |
| 314 | + (int)(rect.x * DOWNSCALE_RATIO), |
| 315 | + (int)(rect.y * DOWNSCALE_RATIO), |
| 316 | + (int)(rect.width * DOWNSCALE_RATIO), |
| 317 | + (int)(rect.height * DOWNSCALE_RATIO)); |
| 318 | + } |
| 319 | + |
| 320 | + } |
| 321 | + |
| 322 | + |
| 323 | + //Imgproc.rectangle(rgbaMat, new Point(0, 0), new Point(rgbaMat.width(), rgbaMat.height()), new Scalar(0, 0, 0, 0), -1); |
| 324 | + |
| 325 | + for (int i = 0; i < detectResult.Length; i++) |
| 326 | + { |
| 327 | + |
| 328 | + Imgproc.rectangle(rgbaMat, new Point(detectResult[i].x, detectResult[i].y), new Point(detectResult[i].x + detectResult[i].width, detectResult[i].y + detectResult[i].height), new Scalar(255, 0, 0, 255), 4); |
| 329 | + } |
| 330 | + } |
| 331 | + }); |
| 332 | + |
| 333 | + |
| 334 | + |
| 335 | + Utils.fastMatToTexture2D(rgbaMat, texture); |
| 336 | + |
| 337 | + |
| 338 | + Thread.Sleep(10); |
| 339 | + |
| 340 | + |
| 341 | + } |
| 342 | + |
| 343 | + } |
| 344 | + |
| 345 | + } |
| 346 | +} |
0 commit comments