Unity+OpenCV 人脸识别追踪

2022-09-02 11:27:07 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript复制

项目需要一个人脸识别追踪的效果,所以查找了一些资料,自己做了一个功能,基本效果已经实现了。

首先项目需要OpenCV的开发环境,所以首先一定要在开发电脑上装上OpenCV的开发环境,流程很简单,直接去http://opencv.org/downloads.html官网下载OpenCV的安装文件就可以了,然后配置电脑的环境变量。我的电脑是windows操作系统

配置好就是这个样子,然后要把用到的dll文件导入进unity工程中,然后下面附上主要代码

代码语言:javascript复制
using UnityEngine;
using System.Collections;
using OpenCvSharp;

public class VideoTest : MonoBehaviour
{
    private Camera _camera;
    public GameObject Slice;
    Material m_material;
    public GameObject m_Cube;
    public WebCamTexture cameraTexture;
    Texture2D rt;
    private string cameraName = "";
    private bool isPlay = true;
    static int mPreviewWidth = 320;//(这个分辨率可以自己调,分辨率越高越卡,我的电脑这个就刚刚好)
    static int mPreviewHeight = 240;
    bool state = true;
    CascadeClassifier haarCascade;
    WebCamDevice[] devices;
    // Use this for initialization
    void Start()
    {
        m_material = Slice.GetComponent<MeshRenderer>().material;
        rt = new Texture2D(mPreviewWidth, mPreviewHeight, TextureFormat.RGB565, false);
        temp = new Texture2D(mPreviewWidth, mPreviewHeight, TextureFormat.RGB565, false);
        StartCoroutine(Test());
        haarCascade = new CascadeClassifier(Application.streamingAssetsPath   "/haarcascades/haarcascade_frontalface_alt2.xml");
        _camera = Camera.main;
    }

    // Update is called once per frame
    float timer;
    void Update()
    {
        timer  = Time.deltaTime;
        if(cameraTexture!=null)
        {
            haarResult = DetectFace(haarCascade, GetTexture2D(cameraTexture));
            bs = haarResult.ToBytes(".png");
            rt.LoadImage(bs);
            rt.Apply();
            m_material.mainTexture = rt;
代码语言:javascript复制
            //这里的面部跟随坐标计算是我根据分辨率自己算的(不精确),当然肯定有更好的算法实现。
代码语言:javascript复制
            m_Cube.transform.localPosition = Vector3.Slerp(m_Cube.transform.localPosition, new Vector3(center.X / 16, -center.Y / 21.8f, 0), 0.3f);
        }
    }

    IEnumerator Test()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//调用外部摄像头
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            cameraTexture = new WebCamTexture(cameraName, mPreviewWidth, mPreviewHeight, 30);
            cameraTexture.Play();
            isPlay = true;
        }
    }
    Mat haarResult;
    byte[] bs;
  

    Mat result;
    OpenCvSharp.Rect[] faces;
    Mat src;
    Mat gray = new Mat();
    Size axes = new Size();
    Point center = new Point();

    private Mat DetectFace(CascadeClassifier cascade, Texture2D t)
    {
        src = Mat.FromImageData(t.EncodeToPNG(), ImreadModes.Color);
        result = src.Clone();
        Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
        src = null;
        // Detect faces
        faces = cascade.DetectMultiScale(gray, 1.08, 2, HaarDetectionType.ScaleImage, new Size(30, 30));

        // Render all detected faces
        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);//绘制脸部范围
        }
        return result;
    }
    Texture2D temp;
    Texture2D GetTexture2D(WebCamTexture wct)
    {
        temp.SetPixels(wct.GetPixels());
        temp.Apply();
        return temp;
    }
}

unity演示工程地址 http://download.csdn.net/detail/truck_truck/9816238

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139500.html原文链接:https://javaforall.cn

0 人点赞