Unity使用OpenCvSharp人脸识别

来源:互联网 发布:php 教程 编辑:程序博客网 时间:2024/05/16 08:39

效果图:

OpenCvSharp

代码:

using UnityEngine;using System.Collections;using OpenCvSharp;public class VideoTest : MonoBehaviour{    public WebCamTexture cameraTexture;    Texture2D rt;    public string cameraName = "";    private bool isPlay = false;    static int mPreviewWidth = 320;    static int mPreviewHeight = 240;    bool state = false;    CascadeClassifier haarCascade;    WebCamDevice[] devices;    // Use this for initialization    void Start()    {        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");    }    // 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;            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 (GUI.Button(new UnityEngine.Rect(0, 240, 80, 80), "START"))            {                if (state)                {                    state = false;                    return;                }                else                {                    state = true;                }            }            if (state)            {                // Detect faces                haarResult = DetectFace(haarCascade, GetTexture2D(cameraTexture));                bs = haarResult.ToBytes(".png");                rt.LoadImage(bs);                rt.Apply();                GUI.DrawTexture(new UnityEngine.Rect(mPreviewWidth, 0, mPreviewWidth, mPreviewHeight), rt, ScaleMode.StretchToFill);            }        }    }    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);        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;    }} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116

OpenCvSharp github地址

点击下载工程工(unity5.3.5f1&win10x64)