/// <summary> /// Raises the web cam texture to mat helper initialized event. /// </summary> public void OnWebCamTextureToMatHelperInitialized() { Debug.Log("OnWebCamTextureToMatHelperInitialized"); Mat webCamTextureMat = webCamTextureToMatHelper.GetMat(); texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); OpenCVForUnity.UnityUtils.Utils.fastMatToTexture2D(webCamTextureMat, texture); gameObject.GetComponent <Renderer>().material.mainTexture = texture; gameObject.transform.localScale = new Vector3(webCamTextureMat.cols(), webCamTextureMat.rows(), 1); Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); if (fpsMonitor != null) { fpsMonitor.Add("dlib shape predictor", dlibShapePredictorFileName); fpsMonitor.Add("width", webCamTextureToMatHelper.GetWidth().ToString()); fpsMonitor.Add("height", webCamTextureToMatHelper.GetHeight().ToString()); fpsMonitor.Add("orientation", Screen.orientation.ToString()); } float width = webCamTextureMat.width(); float height = webCamTextureMat.height(); 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; } if (lowPassFilter != null) { lowPassFilter.Reset(); } if (kalmanFilter != null) { kalmanFilter.Reset(); } if (opticalFlowFilter != null) { opticalFlowFilter.Reset(); } skippedFrames = 0; }
// Update is called once per frame void Update() { if (capture == null) { return; } //Loop play if (capture.get(Videoio.CAP_PROP_POS_FRAMES) >= capture.get(Videoio.CAP_PROP_FRAME_COUNT)) { capture.set(Videoio.CAP_PROP_POS_FRAMES, 0); } //error PlayerLoop called recursively! on iOS.reccomend WebCamTexture. if (capture.grab()) { capture.retrieve(rgbMat, 0); Imgproc.cvtColor(rgbMat, rgbMat, Imgproc.COLOR_BGR2RGB); //Debug.Log ("Mat toString " + rgbMat.ToString ()); OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbMat); //detect face rects List <UnityEngine.Rect> detectResult = faceLandmarkDetector.Detect(); UnityEngine.Rect rect = new UnityEngine.Rect(); List <Vector2> points = null; if (detectResult.Count > 0) { rect = detectResult [0]; //detect landmark points points = faceLandmarkDetector.DetectLandmark(rect); skippedFrames = 0; } else { skippedFrames++; if (skippedFrames == maximumAllowedSkippedFrames) { if (drawLowPassFilter) { lowPassFilter.Reset(); } if (drawKalmanFilter) { kalmanFilter.Reset(); } if (drawOpticalFlowFilter) { opticalFlowFilter.Reset(); } if (drawOFAndLPFilter) { opticalFlowFilter.Reset(); } lowPassFilter.Reset(); } } if (drawLowPassFilter) { lowPassFilter.Process(rgbMat, points, lowPassFilteredPoints, isDebugMode); } if (drawKalmanFilter) { kalmanFilter.Process(rgbMat, points, kalmanFilteredPoints, isDebugMode); } if (drawOpticalFlowFilter) { opticalFlowFilter.Process(rgbMat, points, opticalFlowFilteredPoints, isDebugMode); } if (drawOFAndLPFilter) { opticalFlowFilter.Process(rgbMat, points, points, false); lowPassFilter.Process(rgbMat, points, ofAndLPFilteredPoints, isDebugMode); } if (points != null && !isDebugMode) { // draw raw landmark points. OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, points, new Scalar(0, 255, 0), 2); } // draw face rect. //OpenCVForUnityUtils.DrawFaceRect (rgbMat, rect, new Scalar (255, 0, 0), 2); // draw filtered lam points. if (points != null && !isDebugMode) { if (drawLowPassFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, lowPassFilteredPoints, new Scalar(0, 255, 255), 2); } if (drawKalmanFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, kalmanFilteredPoints, new Scalar(0, 0, 255), 2); } if (drawOpticalFlowFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, opticalFlowFilteredPoints, new Scalar(255, 0, 0), 2); } if (drawOFAndLPFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, ofAndLPFilteredPoints, new Scalar(255, 0, 255), 2); } } //Imgproc.putText (rgbMat, "W:" + rgbMat.width () + " H:" + rgbMat.height () + " SO:" + Screen.orientation, new Point (5, rgbMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar (255, 255, 255), 1, Imgproc.LINE_AA, false); OpenCVForUnity.UnityUtils.Utils.fastMatToTexture2D(rgbMat, texture); } }
// Update is called once per frame void Update() { if (sourceToMatHelper.IsPlaying() && sourceToMatHelper.DidUpdateThisFrame()) { Mat rgbMat = sourceToMatHelper.GetMat(); OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbMat); //detect face rects List <UnityEngine.Rect> detectResult = faceLandmarkDetector.Detect(); UnityEngine.Rect rect = new UnityEngine.Rect(); List <Vector2> points = null; if (detectResult.Count > 0) { rect = detectResult[0]; //detect landmark points points = faceLandmarkDetector.DetectLandmark(rect); skippedFrames = 0; } else { skippedFrames++; if (skippedFrames == maximumAllowedSkippedFrames) { if (drawLowPassFilter) { lowPassFilter.Reset(); } if (drawKalmanFilter) { kalmanFilter.Reset(); } if (drawOpticalFlowFilter) { opticalFlowFilter.Reset(); } if (drawOFAndLPFilter) { opticalFlowFilter.Reset(); } lowPassFilter.Reset(); } } if (drawLowPassFilter) { lowPassFilter.Process(rgbMat, points, lowPassFilteredPoints, isDebugMode); } if (drawKalmanFilter) { kalmanFilter.Process(rgbMat, points, kalmanFilteredPoints, isDebugMode); } if (drawOpticalFlowFilter) { opticalFlowFilter.Process(rgbMat, points, opticalFlowFilteredPoints, isDebugMode); } if (drawOFAndLPFilter) { opticalFlowFilter.Process(rgbMat, points, points, false); lowPassFilter.Process(rgbMat, points, ofAndLPFilteredPoints, isDebugMode); } if (points != null && !isDebugMode) { // draw raw landmark points. OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, points, new Scalar(0, 255, 0), 2); } // draw face rect. //OpenCVForUnityUtils.DrawFaceRect (rgbMat, rect, new Scalar (255, 0, 0), 2); // draw filtered lam points. if (points != null && !isDebugMode) { if (drawLowPassFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, lowPassFilteredPoints, new Scalar(0, 255, 255), 2); } if (drawKalmanFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, kalmanFilteredPoints, new Scalar(0, 0, 255), 2); } if (drawOpticalFlowFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, opticalFlowFilteredPoints, new Scalar(255, 0, 0), 2); } if (drawOFAndLPFilter) { OpenCVForUnityUtils.DrawFaceLandmark(rgbMat, ofAndLPFilteredPoints, new Scalar(255, 0, 255), 2); } } //Imgproc.putText (rgbMat, "W:" + rgbMat.width () + " H:" + rgbMat.height () + " SO:" + Screen.orientation, new Point (5, rgbMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar (255, 255, 255), 1, Imgproc.LINE_AA, false); Utils.fastMatToTexture2D(rgbMat, texture); } }