Unity+OpenCV 人脸识别追踪

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

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

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

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;
            //这里的面部跟随坐标计算是我根据分辨率自己算的(不精确),当然肯定有更好的算法实现。
            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

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Unity结合OpenCV进行图片人脸识别合成是一种常见的应用场景。首先,我们需要导入OpenCV库到Unity项目中。可以使用Unity的插件系统或者第三方的OpenCVUnity插件来实现。 接下来,根据项目需求,我们可以使用Unity的游戏对象来创建一个平面,作为展示图片人脸识别合成的背景。然后,利用Unity的渲染纹理功能,将OpenCV处理过的图像合成到该平面上。 图片人脸识别通常需要使用OpenCV提供的人脸检测功能。在Unity中,可以使用OpenCVUnity插件提供的接口来调用OpenCV的人脸检测算法,获取到检测到的人脸位置信息。 接着,我们可以利用Unity的纹理功能将需要合成的人脸图像加载到游戏对象上,并根据得到的人脸位置信息进行缩放、旋转和偏移等操作,使得合成的人脸图像能够与背景平面中的人脸位置对齐。 最后,通过Unity的摄像机设置,可以调整摄像机的视角和位置,以便实现更好的展示效果。我们可以添加一些特效,如光照效果、模糊等,使得合成的结果看起来更加真实。 总结起来,通过Unity结合OpenCV,我们可以实现图片人脸识别合成的效果。在Unity中,我们可以利用OpenCV提供的人脸检测功能,结合Unity的渲染纹理和摄像机设置,使得合成的效果更加逼真。使用Unity的开发环境,可以方便地进行设计和调试,并且还可以添加各种特效来增加合成图像的艺术效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值