Unity 捕获出现在摄像头面前信息

Posted clhxxlcj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 捕获出现在摄像头面前信息相关的知识,希望对你有一定的参考价值。

using UnityEngine;
using System.Collections;
using OpenCvSharp;
using UnityEngine.UI;
using System.IO;

public class FaceDetection : MonoBehaviour

    public WebCamTexture cameraTexture;//摄像头对象
    public string cameraName = "";
    Texture2D rt;

    [Header("五官模型")]
    public GameObject FacePhoto;
    public GameObject EyeLeft;
    public GameObject EyeRight;
    public GameObject Nose;
    public GameObject Mouth;

    [Header("五官")]
    public Texture2D FaceTexture;
    public Texture2D EyeLeftTexture;
    public Texture2D EyeRightTexture;
    public Texture2D NoseTexture;
    public Texture2D MouthTexture;


    private bool isPlay = false;
    static int mPreviewWidth = 320;
    static int mPreviewHeight = 240;
    bool state = false;
    CascadeClassifier haarCascade;
    WebCamDevice[] devices;//描述网络摄像头设备的结构。
    Texture2D temp;
    // 用于初始化
    void Start()
    
        rt = new Texture2D(mPreviewWidth, mPreviewHeight, TextureFormat.RGB565, false);
        temp = new Texture2D(mPreviewWidth, mPreviewHeight, TextureFormat.RGB565, false);
        StartCoroutine(Test());//

        //CascadeClassifier级联分类器,可用来对象检测 
        haarCascade = new CascadeClassifier(Application.streamingAssetsPath + "/haarcascades/haarcascade_frontalface_alt.xml");//haarcascade_frontalface_alt2.xml//haarcascade_frontalface_alt.xml
        
    

    public void StartDetectionFace()
    
        this.state = true;
    

    public void StopDetectionFace()
    
        this.state = false;
    

    // Update is called once per frame
    void Update()
    

    


    //开启协程,获取摄像头图像数据
    IEnumerator Test()
    
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//等待用户允许访问
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))//如果用户允许访问,开始获取图像   
        
            devices = WebCamTexture.devices;//先获取设备
            cameraName = devices[0].name;
            Debug.Log("Camera:" + cameraName);
            cameraTexture = new WebCamTexture(cameraName, mPreviewWidth, mPreviewHeight, 15);//然后获取图像
            cameraTexture.Play();//开始实施获取
            isPlay = true;
         
    
    Mat haarResult;
    byte[] bs;

    //显示摄像头看到的对象
    void OnGUI()
    
        if (isPlay)
        
           //GUI.DrawTexture(new UnityEngine.Rect(0, 0, mPreviewWidth, mPreviewHeight), cameraTexture, ScaleMode.ScaleToFit);

            if (state)
            
                // 人脸识别
                haarResult = DetectFace(haarCascade, GetTexture2D(cameraTexture));
                bs = haarResult.ToBytes(".png");

                //oadImage将PNG / JPG图像字节数组加载到纹理中。
                rt.LoadImage(bs);
                rt.Apply();

              //System.IO.File.WriteAllBytes(@"d:\temp\1.jpg", bs);

             //GUI.DrawTexture(new UnityEngine.Rect(mPreviewWidth, 0, mPreviewWidth, mPreviewHeight), rt, ScaleMode.StretchToFill);

            
            else
            
                StopCam();
            

        

    

    Mat result;
    OpenCvSharp.Rect[] faces;
    Mat src;
    Mat gray = new Mat();
    Size axes = new Size();
    Point center = new Point();
    /// <summary>
     
    /// </summary>
    /// <param name="cascade"></param>
    /// <returns></returns>
    private Mat DetectFace(CascadeClassifier cascade, Texture2D t)
    
        src = Mat.FromImageData(t.EncodeToPNG(), ImreadModes.Color);// 读取图片并提取Mat中data数据
        result = src.Clone();//复制图像
        Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);//颜色空间转换
        src = null;

        #region Detect Face
        //检测人脸
        //DetectMultiScale可以检测出图片中所有的人脸,并将人脸用vector保存各个人脸的坐标、大小(用矩形表示)
        //DetectMultiScale函数由分类器对象(CascadeClassifier)调用
        faces = cascade.DetectMultiScale(gray, 2f, 2, HaarDetectionType.DoCannyPruning, new Size(80, 80));
        Debug.Log("face count:" + faces.Length);
        //渲染所有检测到的人脸
        for (int i = 0; i < faces.Length; i++)
        
            center.X = (int)(faces[i].X + faces[i].Width * 0.5);
            center.Y = (int)(faces[i].Y + faces[i].Height * 0.5);
            axes.Width = (int)(faces[i].Width * 0.5);
            axes.Height = (int)(faces[i].Height * 0.5);
            //Cv2.Ellipse(result, center, axes, 0, 0, 360, new Scalar(255, 0, 255), 4);

            Debug.Log("width:" + faces[i].Width + " Height:" + faces[i].Height);

            //Mat roi_gray_img = new Mat(grayMat, new OpenCVForUnity.Rect(0, 0, rects[i].x + rects[i].width, rects[i].y + rects[i].height));
            //Mat roi_img = new Mat(result, new OpenCVForUnity.Rect(0, 0, rects[i].x + rects[i].width, rects[i].y + rects[i].height));
            //MatOfRect eyes = new MatOfRect();
            //CascadeClassifier eyecascade = new CascadeClassifier(Application.streamingAssetsPath + "/haarcascades/haarcascade_eye.xml");
            //eyecascade.detectMultiScale(roi_gray_img, eyes, 1.3d, 5, 2, new Size(20, 20), new Size());


            OpenCvSharp.Rect rect = new OpenCvSharp.Rect(faces[i].X, faces[i].Y, faces[i].Width, faces[i].Height);
            Mat mat = new Mat(result, rect);
            byte[] bytes = mat.ToBytes(".png");


            Texture2D screenShot = new Texture2D((int)axes.Width, (int)axes.Height, TextureFormat.RGB24, false);
            //LoadImage 加载图像,(将PNG / JPG图像字节数组加载到纹理中)
            screenShot.LoadImage(bytes, false);

            screenShot.Apply();
           

          // screenShot = SetSharp(screenShot);


           // string filename = @"d:\temp\Screenshot.png";
          //  System.IO.File.WriteAllBytes(filename, bytes);

            FacePhoto.GetComponent<RawImage>().texture = screenShot;
        
        #endregion


        return result;
    
    
    Texture2D GetTexture2D(WebCamTexture wct)
    
        //SetPixels设置像素块 (此函数采用颜色数组并更改纹理的整个mip级别的像素颜色) 
        //GetPixels获取像素颜色块 (此函数返回纹理的整个mip级别的像素颜色数组)
        temp.SetPixels(wct.GetPixels());

        //应用
        temp.Apply();
        return temp;
    
  public void StopCam()
    
        if(cameraTexture.isPlaying)
        
            cameraTexture.Stop();
            isPlay = false;
            state = false;
        
    

  

以上是关于Unity 捕获出现在摄像头面前信息的主要内容,如果未能解决你的问题,请参考以下文章

Unity中timeline出现脚本错误怎么解决

Unity3D基础知识之Camera摄像机及其属性

iOS 上的 Unity3D,在 Obj-C 中检查设备摄像头图像

从片段中的相机意图返回后屏幕变为白色

我想通过使用网络摄像头将捕获图像添加到我的 PyQt gui 窗口中,网络摄像头图像将出现在 gui 窗口中

异常和TCP通讯