效果图:
代码:
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;
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" );
}
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)
{
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();
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 ;
faces = cascade.DetectMultiScale(gray, 1.08 , 2 , HaarDetectionType.ScaleImage, new Size(30 , 30 ));
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)