public virtual void UpdateObject(int id, Vector3[] vertices, int[] triangles = null, Vector2[] uv = null, Vector2[] uv2 = null) { if (showingObjects.ContainsKey(id)) { TrackedMesh tm = showingObjects[id]; if (vertices.Length != tm.meshFilter.mesh.vertices.Length) { Debug.LogError("The number of vertices does not match."); } tm.meshFilter.mesh.vertices = vertices; if (triangles != null) { tm.meshFilter.mesh.triangles = triangles; } if (uv != null) { tm.meshFilter.mesh.uv = uv; } if (uv2 != null) { tm.meshFilter.mesh.uv2 = uv2; } tm.meshFilter.mesh.RecalculateBounds(); tm.meshFilter.mesh.RecalculateNormals(); } }
private void CorrectFaceMaskColor(int id, Mat src, Mat dst, List <Vector2> src_landmarkPoints, List <Vector2> dst_landmarkPoints) { Texture2D LUTTex = faceMaskColorCorrector.UpdateLUTTex(id, src, dst, src_landmarkPoints, dst_landmarkPoints); TrackedMesh tm = meshOverlay.GetObjectById(id); tm.sharedMaterial.SetTexture(shader_LUTTexID, LUTTex); }
public virtual TrackedMesh CreateObject(int id, Texture2D tex = null) { if (_baseObject == null) { Debug.LogError("The baseObject does not exist."); } if (!showingObjects.ContainsKey(id)) { GameObject obj = GetPoolObject(overlayTransform); if (obj == null) { return(null); } TrackedMesh tm = obj.GetComponent <TrackedMesh>(); if (tm != null) { tm.id = id; tm.transform.localPosition = Vector3.zero; tm.transform.localRotation = new Quaternion(); tm.transform.localScale = Vector3.one; if (tex != null) { Renderer tmRenderer = tm.transform.GetComponent <Renderer>(); tmRenderer.sharedMaterial.SetTexture("_MainTex", tex); } showingObjects.Add(id, tm); } return(tm); } else { return(null); } }
private void MaskFace(TrackedMeshOverlay meshOverlay, TrackedRect tr, List <Vector2> landmarkPoints, List <Vector2> landmarkPointsInMaskImage, float maskImageWidth = 0, float maskImageHeight = 0) { float imageWidth = meshOverlay.width; float imageHeight = meshOverlay.height; if (maskImageWidth == 0) { maskImageWidth = imageWidth; } if (maskImageHeight == 0) { maskImageHeight = imageHeight; } TrackedMesh tm = meshOverlay.GetObjectById(tr.id); Vector3[] vertices = tm.meshFilter.mesh.vertices; if (vertices.Length == landmarkPoints.Count) { for (int j = 0; j < vertices.Length; j++) { vertices[j].x = landmarkPoints[j].x / imageWidth - 0.5f; vertices[j].y = 0.5f - landmarkPoints[j].y / imageHeight; } } Vector2[] uv = tm.meshFilter.mesh.uv; if (uv.Length == landmarkPointsInMaskImage.Count) { for (int jj = 0; jj < uv.Length; jj++) { uv[jj].x = landmarkPointsInMaskImage[jj].x / maskImageWidth; uv[jj].y = (maskImageHeight - landmarkPointsInMaskImage[jj].y) / maskImageHeight; } } meshOverlay.UpdateObject(tr.id, vertices, null, uv); if (tr.numFramesNotDetected > 3) { tm.sharedMaterial.SetFloat(shader_FadeID, 1f); } else if (tr.numFramesNotDetected > 0 && tr.numFramesNotDetected <= 3) { tm.sharedMaterial.SetFloat(shader_FadeID, 0.3f + (0.7f / 4f) * tr.numFramesNotDetected); } else { tm.sharedMaterial.SetFloat(shader_FadeID, 0.3f); } // filter non frontal faces. if (filterNonFrontalFaces && frontalFaceChecker.GetFrontalFaceRate(landmarkPoints) < frontalFaceRateLowerLimit) { tm.sharedMaterial.SetFloat(shader_FadeID, 1f); } }
private void Run() { meshOverlay = this.GetComponent <TrackedMeshOverlay> (); // Customize face mask. GameObject newBaseObject = Instantiate(meshOverlay.baseObject); newBaseObject.name = "CustomFaceMaskTrackedMesh"; TrackedMesh tm = newBaseObject.GetComponent <TrackedMesh> (); if (extendForehead) { ExtendForehead(tm.meshFilter.mesh); } Texture alphaMask = tm.material.GetTexture("_MaskTex"); Vector2[] uv = tm.meshFilter.sharedMesh.uv2; Texture2D newAlphaMask = CreateFaceMaskAlphaMaskTexture(alphaMask.width, alphaMask.height, uv, makeBothEyesTransparent, makeMouthTransparent); tm.material.SetTexture("_MaskTex", newAlphaMask); meshOverlay.baseObject = newBaseObject; shader_FadeID = Shader.PropertyToID("_Fade"); shader_ColorCorrectionID = Shader.PropertyToID("_ColorCorrection"); shader_LUTTexID = Shader.PropertyToID("_LUTTex"); rectangleTracker = new RectangleTracker(); faceLandmarkDetector = new FaceLandmarkDetector(sp_human_face_68_dat_filepath); lowPassFilterDict = new Dictionary <int, LowPassPointsFilter> (); opticalFlowFilterDict = new Dictionary <int, OFPointsFilter> (); faceMaskColorCorrector = new FaceMaskColorCorrector(); #if UNITY_ANDROID && !UNITY_EDITOR // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2). webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true; #endif webCamTextureToMatHelper.Initialize(); displayFaceRectsToggle.isOn = displayFaceRects; useDlibFaceDetecterToggle.isOn = useDlibFaceDetecter; enableNoiseFilterToggle.isOn = enableNoiseFilter; enableColorCorrectionToggle.isOn = enableColorCorrection; filterNonFrontalFacesToggle.isOn = filterNonFrontalFaces; displayDebugFacePointsToggle.isOn = displayDebugFacePoints; }
private void Run() { meshOverlay = this.GetComponent <TrackedMeshOverlay>(); // Customize face mask. GameObject newBaseObject = Instantiate(meshOverlay.baseObject); newBaseObject.name = "CustomFaceMaskTrackedMesh"; TrackedMesh tm = newBaseObject.GetComponent <TrackedMesh>(); if (extendForehead) { ExtendForehead(tm.meshFilter.mesh); } Texture alphaMask = tm.material.GetTexture("_MaskTex"); Vector2[] uv = tm.meshFilter.sharedMesh.uv2; Texture2D newAlphaMask = CreateFaceMaskAlphaMaskTexture(alphaMask.width, alphaMask.height, uv, makeBothEyesTransparent, makeMouthTransparent); tm.material.SetTexture("_MaskTex", newAlphaMask); meshOverlay.baseObject = newBaseObject; shader_FadeID = Shader.PropertyToID("_Fade"); shader_LUTTexID = Shader.PropertyToID("_LUTTex"); rectangleTracker = new RectangleTracker(); faceLandmarkDetector = new FaceLandmarkDetector(sp_human_face_68_dat_filepath); lowPassFilterDict = new Dictionary <int, LowPassPointsFilter>(); opticalFlowFilterDict = new Dictionary <int, OFPointsFilter>(); webCamTextureToMatHelper.Initialize(); displayFaceRectsToggle.isOn = displayFaceRects; useDlibFaceDetecterToggle.isOn = useDlibFaceDetecter; enableNoiseFilterToggle.isOn = enableNoiseFilter; filterNonFrontalFacesToggle.isOn = filterNonFrontalFaces; displayDebugFacePointsToggle.isOn = displayDebugFacePoints; }
private void Run() { meshOverlay = this.GetComponent <TrackedMeshOverlay> (); // Customize face mask. GameObject newBaseObject = Instantiate(meshOverlay.baseObject); newBaseObject.name = "CustomFaceMaskTrackedMesh"; TrackedMesh tm = newBaseObject.GetComponent <TrackedMesh> (); if (extendForehead) { ExtendForehead(tm.meshFilter.mesh); } Texture alphaMask = tm.material.GetTexture("_MaskTex"); Vector2[] uv = tm.meshFilter.sharedMesh.uv2; Texture2D newAlphaMask = CreateFaceMaskAlphaMaskTexture(alphaMask.width, alphaMask.height, uv, makeBothEyesTransparent, makeMouthTransparent); tm.material.SetTexture("_MaskTex", newAlphaMask); meshOverlay.baseObject = newBaseObject; shader_FadeID = Shader.PropertyToID("_Fade"); shader_ColorCorrectionID = Shader.PropertyToID("_ColorCorrection"); shader_LUTTexID = Shader.PropertyToID("_LUTTex"); rectangleTracker = new RectangleTracker(); faceLandmarkDetector = new FaceLandmarkDetector(sp_human_face_68_dat_filepath); lowPassFilterDict = new Dictionary <int, LowPassPointsFilter> (); opticalFlowFilterDict = new Dictionary <int, OFPointsFilter> (); faceMaskColorCorrector = new FaceMaskColorCorrector(); #if UNITY_ANDROID && !UNITY_EDITOR // Set the requestedFPS parameter to avoid the problem of the WebCamTexture image becoming low light on some Android devices. (Pixel, pixel 2) // https://forum.unity.com/threads/android-webcamtexture-in-low-light-only-some-models.520656/ // https://forum.unity.com/threads/released-opencv-for-unity.277080/page-33#post-3445178 rearCameraRequestedFPS = webCamTextureToMatHelper.requestedFPS; if (webCamTextureToMatHelper.requestedIsFrontFacing) { webCamTextureToMatHelper.requestedFPS = 15; webCamTextureToMatHelper.Initialize(); } else { webCamTextureToMatHelper.Initialize(); } #else webCamTextureToMatHelper.Initialize(); #endif displayFaceRectsToggle.isOn = displayFaceRects; useDlibFaceDetecterToggle.isOn = useDlibFaceDetecter; enableNoiseFilterToggle.isOn = enableNoiseFilter; enableColorCorrectionToggle.isOn = enableColorCorrection; filterNonFrontalFacesToggle.isOn = filterNonFrontalFaces; displayDebugFacePointsToggle.isOn = displayDebugFacePoints; }