MirzkisD1Ex0 1 年之前
父节点
当前提交
6c2bed4e76

二进制
Materials/Alpha Video Mask/01.png


二进制
Materials/Alpha Video Mask/02.png


二进制
Materials/Alpha Video Mask/03_整体场景结构.png


+ 65 - 0
Materials/Alpha Video Mask/AlphaVideoHandler.cs

@@ -0,0 +1,65 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Video;
+
+public class AlphaVideoHandler : MonoBehaviour
+{
+  public static AlphaVideoHandler Instance;
+
+  private VideoPlayer videoPlayer;
+
+  // ==================================================
+
+  private void Awake() => Instance = this;
+  private void Start() => Init();
+  private void Update() => ShortcutKey();
+
+  // ==================================================
+
+  private void Init()
+  {
+    videoPlayer = GetComponent<VideoPlayer>();
+    StartCoroutine(nameof(InitAction));
+    return;
+  }
+
+  private IEnumerator InitAction()
+  {
+    yield return new WaitForSeconds(1f); // 为了视频加载完毕而延迟
+    videoPlayer.Play();
+    yield return new WaitForSeconds(0.2f); // 显示擦除的短暂空白
+    videoPlayer.Pause();
+    yield break;
+  }
+
+  // ==================================================
+
+  public void SwitchAlphaVideo(bool value)
+  {
+    if (value)
+    {
+      videoPlayer.Play();
+    }
+    else
+    {
+      videoPlayer.Pause();
+    }
+    return;
+  }
+
+  // ==================================================
+
+  private void ShortcutKey()
+  {
+    if (Input.GetKeyDown(KeyCode.Q))
+    {
+      SwitchAlphaVideo(true);
+    }
+    if (Input.GetKeyDown(KeyCode.W))
+    {
+      SwitchAlphaVideo(false);
+    }
+    return;
+  }
+}

+ 56 - 0
Materials/Alpha Video Mask/MaskedShader.shader

@@ -0,0 +1,56 @@
+Shader "Custom/MaskedShader" 
+{
+    Properties 
+    {
+        _MainTex ("Texture", 2D) = "white" {}
+        _AlphaVideo ("Alpha Video", 2D) = "white" {}
+    }
+    SubShader 
+    {
+        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
+        LOD 100
+ 
+        Pass
+        {
+            Blend SrcAlpha OneMinusSrcAlpha
+ 
+            CGPROGRAM
+            #pragma vertex vert
+            #pragma fragment frag
+ 
+            #include "UnityCG.cginc"
+ 
+            struct appdata
+            {
+                float4 vertex : POSITION;
+                float2 uv : TEXCOORD0;
+            };
+ 
+            struct v2f
+            {
+                float2 uv : TEXCOORD0;
+                float4 vertex : SV_POSITION;
+            };
+ 
+            sampler2D _MainTex;
+            sampler2D _AlphaVideo;
+            float4 _MainTex_ST;
+ 
+            v2f vert (appdata v)
+            {
+                v2f o;
+                o.vertex = UnityObjectToClipPos(v.vertex);
+                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
+                return o;
+            }
+ 
+            fixed4 frag (v2f i) : SV_Target
+            {
+                fixed4 col = tex2D(_MainTex, i.uv);
+                fixed alpha = tex2D(_AlphaVideo, i.uv).r; // Assuming the alpha is in the red channel
+                return fixed4(col.rgb, alpha);
+            }
+            ENDCG
+        }
+    }
+}

+ 26 - 5
Materials/OpenCV/面部识别模块/FacialRecognitionHandler.cs

@@ -15,6 +15,8 @@ using OpenCVForUnity.ObjdetectModule;
 /// </summary>
 public class FacialRecognitionHandler : MonoBehaviour
 {
+  public static FacialRecognitionHandler Instance;
+
   private Mat gray; // 灰度图,方便识别
   private Mat rotatedNewMat;
   private MatOfRect faceRect; // 识别到的人脸的区域
@@ -23,8 +25,14 @@ public class FacialRecognitionHandler : MonoBehaviour
 
   [SerializeField] private float timer = 0;
 
+  #region 配置
+  private int resultPreviewTexture2DWidth = 480;
+  private int resultPreviewTexture2DHeight = 270;
+  #endregion
+
   // ==================================================
 
+  private void Awake() => Instance = this;
   private void Start() => Init();
   private void OnDestroy() => Dispose();
 
@@ -38,7 +46,7 @@ public class FacialRecognitionHandler : MonoBehaviour
     classifier = new CascadeClassifier(cascadePath); // 初始化人脸识别分类器
 
     previewMat = new Mat();
-    resultPreviewTexture2D = new Texture2D(440, 440, TextureFormat.RGBA32, false);
+    resultPreviewTexture2D = new Texture2D(resultPreviewTexture2DWidth, resultPreviewTexture2DHeight, TextureFormat.RGBA32, false);
     return;
   }
 
@@ -78,7 +86,7 @@ public class FacialRecognitionHandler : MonoBehaviour
 
     // mat/面部矩形向量组/识别精度越高越快越不准/面部识别次数2次以上算识别/?性能优化/最小检测尺寸/最大检测尺寸
     // classifier.detectMultiScale(gray, faceRect, 1.1d, 2, 2, new Size(20, 20), new Size()); // 检测gray中的人脸
-    classifier.detectMultiScale(gray, faceRect, 1.1d, 3, 2, new Size(20, 20), new Size());
+    classifier.detectMultiScale(gray, faceRect, 1.1d, 6, 2, new Size(20, 20), new Size());
 
     OpenCVForUnity.CoreModule.Rect[] rects = faceRect.toArray();
     if (rects.Length > 0)
@@ -89,9 +97,10 @@ public class FacialRecognitionHandler : MonoBehaviour
       }
 
       timer += Time.deltaTime;
-      if (timer > .2f)
+      if (timer > .3f)
       {
         Debug.Log("检测到面部");
+        AVProVideoManager.Instance.StartPlayVideo();
       }
     }
     else
@@ -115,11 +124,23 @@ public class FacialRecognitionHandler : MonoBehaviour
 
   // ==================================================
   #region 预览画面
+  private bool allowPreview = false;
+  public void SwitchPreview(bool value)
+  {
+    allowPreview = value;
+    return;
+  }
+
   [SerializeField] private Image resultPreviewImage;
   private Mat previewMat;
   private Texture2D resultPreviewTexture2D;
   private void UpdateResultPreview(Mat value)
   {
+    if (!allowPreview)
+    {
+      return;
+    }
+
     if (resultPreviewImage == null)
     {
       return;
@@ -127,7 +148,7 @@ public class FacialRecognitionHandler : MonoBehaviour
 
     previewMat = value.clone();
     Utils.matToTexture2D(previewMat, resultPreviewTexture2D);
-    resultPreviewImage.sprite = Sprite.Create(resultPreviewTexture2D, new UnityEngine.Rect(0, 0, 440, 440), Vector2.zero);
+    resultPreviewImage.sprite = Sprite.Create(resultPreviewTexture2D, new UnityEngine.Rect(0, 0, resultPreviewTexture2D.width, resultPreviewTexture2D.height), Vector2.zero);
     return;
   }
   #endregion
@@ -156,7 +177,7 @@ public class FacialRecognitionHandler : MonoBehaviour
   /// </summary>
   private void LogAllWebCam()
   {
-    Debug.Log($"Found {WebCamTexture.devices.Length} device...<color=green>[OK]</color>");
+    Debug.Log($"Found {WebCamTexture.devices.Length} devices");
     foreach (WebCamDevice webcamDevice in WebCamTexture.devices)
     {
       Debug.Log($"{webcamDevice.name}...<color=green>[OK]</color>");

+ 15 - 2
Materials/OpenCV/面部识别模块/WebCamTextureProcesser.cs

@@ -89,6 +89,7 @@ public class WebCamTextureProcesser : MonoBehaviour
       Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
 
       transform.GetComponent<FacialRecognitionHandler>().DetectFace(rgbaMat); // 传入mat 检测人脸 // 会导致原数据反转?
+
       UpdateOriginalPreview();
     }
     return;
@@ -125,8 +126,8 @@ public class WebCamTextureProcesser : MonoBehaviour
   // ==================================================
   #region 创建、配置WebCamera
   [SerializeField] private string requestedDeviceName = "MX Brio"; // "GC21 Video" // "Logitech BRIO"
-  private int requestedWidth = 440;
-  private int requestedHeight = 440;
+  private int requestedWidth = 480; // 440
+  private int requestedHeight = 270; // 440
   private int requestedFPS = 30;
   private void CreateCamera()
   {
@@ -148,10 +149,22 @@ public class WebCamTextureProcesser : MonoBehaviour
 
   // ==================================================
   #region 预览画面
+  private bool allowPreview = false;
+  public void SwitchPreview(bool value)
+  {
+    allowPreview = value;
+    return;
+  }
+
   [SerializeField] private Image originalPreviewImage;
   private Texture2D orginalPreviewTexture2D;
   private void UpdateOriginalPreview()
   {
+    if (!allowPreview)
+    {
+      return;
+    }
+
     if (originalPreviewImage == null)
     {
       return;

+ 1 - 0
Materials/OpenCV/面部识别模块/readme.txt

@@ -0,0 +1 @@
+unitypackage非最新版