using UnityEngine;
using System.Collections;
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
using UnityEngine.SceneManagement;
#endif
using OpenCVForUnity;
namespace OpenCVForUnitySample
{
///
/// WebCamTexture detect face sample.
///
public class WebCamTextureDetectFaceSample : MonoBehaviour
{
///
/// The colors.
///
Color32[] colors;
///
/// The gray mat.
///
Mat grayMat;
///
/// The texture.
///
Texture2D texture;
///
/// The cascade.
///
CascadeClassifier cascade;
///
/// The faces.
///
MatOfRect faces;
///
/// The web cam texture to mat helper.
///
WebCamTextureToMatHelper webCamTextureToMatHelper;
// Use this for initialization
void Start ()
{
webCamTextureToMatHelper = gameObject.GetComponent ();
webCamTextureToMatHelper.Init ();
}
///
/// Raises the web cam texture to mat helper inited event.
///
public void OnWebCamTextureToMatHelperInited ()
{
Debug.Log ("OnWebCamTextureToMatHelperInited");
Mat webCamTextureMat = webCamTextureToMatHelper.GetMat ();
colors = new Color32[webCamTextureMat.cols () * webCamTextureMat.rows ()];
texture = new Texture2D (webCamTextureMat.cols (), webCamTextureMat.rows (), TextureFormat.RGBA32, false);
grayMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC1);
cascade = new CascadeClassifier (Utils.getFilePath ("lbpcascade_frontalface.xml"));
//cascade = new CascadeClassifier (Utils.getFilePath ("haarcascade_frontalface_alt.xml"));
if (cascade.empty ()) {
Debug.LogError ("cascade file is not loaded.Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
}
faces = new MatOfRect ();
gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1);
Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
float width = 0;
float height = 0;
width = gameObject.transform.localScale.x;
height = gameObject.transform.localScale.y;
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
if (widthScale < heightScale) {
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
} else {
Camera.main.orthographicSize = height / 2;
}
gameObject.GetComponent ().material.mainTexture = texture;
}
///
/// Raises the web cam texture to mat helper disposed event.
///
public void OnWebCamTextureToMatHelperDisposed ()
{
Debug.Log ("OnWebCamTextureToMatHelperDisposed");
if (grayMat != null)
grayMat.Dispose ();
if (cascade != null)
cascade.Dispose ();
if (faces != null)
faces.Dispose ();
}
// Update is called once per frame
void Update ()
{
if (webCamTextureToMatHelper.isPlaying () && webCamTextureToMatHelper.didUpdateThisFrame ()) {
Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
Imgproc.cvtColor (rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
Imgproc.equalizeHist (grayMat, grayMat);
if (cascade != null)
cascade.detectMultiScale (grayMat, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size (grayMat.cols () * 0.2, grayMat.rows () * 0.2), new Size ());
OpenCVForUnity.Rect[] rects = faces.toArray ();
for (int i = 0; i < rects.Length; i++) {
// Debug.Log ("detect faces " + rects [i]);
Imgproc.rectangle (rgbaMat, new Point (rects [i].x, rects [i].y), new Point (rects [i].x + rects [i].width, rects [i].y + rects [i].height), new Scalar (255, 0, 0, 255), 2);
}
// Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
Utils.matToTexture2D (rgbaMat, texture, colors);
}
}
///
/// Raises the disable event.
///
void OnDisable ()
{
webCamTextureToMatHelper.Dispose ();
}
///
/// Raises the back button event.
///
public void OnBackButton ()
{
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
SceneManager.LoadScene ("OpenCVForUnitySample");
#else
Application.LoadLevel ("OpenCVForUnitySample");
#endif
}
///
/// Raises the play button event.
///
public void OnPlayButton ()
{
webCamTextureToMatHelper.Play ();
}
///
/// Raises the pause button event.
///
public void OnPauseButton ()
{
webCamTextureToMatHelper.Pause ();
}
///
/// Raises the stop button event.
///
public void OnStopButton ()
{
webCamTextureToMatHelper.Stop ();
}
///
/// Raises the change camera button event.
///
public void OnChangeCameraButton ()
{
webCamTextureToMatHelper.Init (null, webCamTextureToMatHelper.requestWidth, webCamTextureToMatHelper.requestHeight, !webCamTextureToMatHelper.requestIsFrontFacing);
}
}
}