You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
257 lines
8.5 KiB
257 lines
8.5 KiB
using System.Collections;
|
|
using OpenCVForUnity.CoreModule;
|
|
using OpenCVForUnity.ImgprocModule;
|
|
using OpenCVForUnity.ObjdetectModule;
|
|
using OpenCVForUnity.UnityUtils;
|
|
using OpenCVForUnity.UnityUtils.Helper;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scripts
|
|
{
|
|
[RequireComponent(typeof(WebCamTextureToMatHelper))]
|
|
internal class FaceDetectionController : MonoBehaviour
|
|
{
|
|
private GUIStyle fontSytle;
|
|
|
|
/// <summary>
|
|
/// The gray mat.
|
|
/// </summary>
|
|
private Mat grayMat;
|
|
|
|
/// <summary>
|
|
/// The texture.
|
|
/// </summary>
|
|
private Texture2D texture;
|
|
|
|
/// <summary>
|
|
/// The cascade.
|
|
/// </summary>
|
|
private CascadeClassifier cascade;
|
|
|
|
/// <summary>
|
|
/// The faces.
|
|
/// </summary>
|
|
private MatOfRect faces;
|
|
|
|
/// <summary>
|
|
/// The webcam texture to mat helper.
|
|
/// </summary>
|
|
private WebCamTextureToMatHelper webCamTextureToMatHelper;
|
|
|
|
private bool _isChecking;
|
|
private static object _lockObject = new object();
|
|
|
|
/// <summary>
|
|
/// LBP_CASCADE_FILENAME
|
|
/// </summary>
|
|
protected static readonly string LBP_CASCADE_FILENAME = "lbpcascade_frontalface.xml";
|
|
|
|
public Text text;
|
|
|
|
// Use this for initialization
|
|
private void Start()
|
|
{
|
|
webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper>();
|
|
cascade = new CascadeClassifier();
|
|
cascade.load(Utils.getFilePath(LBP_CASCADE_FILENAME));
|
|
// cascade.load (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. ");
|
|
}
|
|
|
|
webCamTextureToMatHelper.Initialize();
|
|
webCamTextureToMatHelper.requestedIsFrontFacing = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the web cam texture to mat helper initialized event.
|
|
/// </summary>
|
|
public void OnWebCamTextureToMatHelperInitialized()
|
|
{
|
|
Debug.Log("OnWebCamTextureToMatHelperInitialized");
|
|
|
|
Mat webCamTextureMat = webCamTextureToMatHelper.GetMat();
|
|
|
|
texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false);
|
|
|
|
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
|
|
|
|
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 = webCamTextureMat.width();
|
|
float height = webCamTextureMat.height();
|
|
|
|
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;
|
|
}
|
|
|
|
grayMat = new Mat(webCamTextureMat.rows(), webCamTextureMat.cols(), CvType.CV_8UC1);
|
|
|
|
faces = new MatOfRect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the web cam texture to mat helper disposed event.
|
|
/// </summary>
|
|
public void OnWebCamTextureToMatHelperDisposed()
|
|
{
|
|
Debug.Log("OnWebCamTextureToMatHelperDisposed");
|
|
|
|
if (grayMat != null)
|
|
{
|
|
grayMat.Dispose();
|
|
}
|
|
|
|
if (texture != null)
|
|
{
|
|
Texture2D.Destroy(texture);
|
|
texture = null;
|
|
}
|
|
|
|
if (faces != null)
|
|
{
|
|
faces.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the web cam texture to mat helper error occurred event.
|
|
/// </summary>
|
|
/// <param name="errorCode">Error code.</param>
|
|
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
|
|
{
|
|
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private 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, new Size(grayMat.cols() * 0.2, grayMat.rows() * 0.2), new Size());
|
|
}
|
|
|
|
OpenCVForUnity.CoreModule.Rect[] rects = faces.toArray();
|
|
|
|
for (int i = 0; i < rects.Length; 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);
|
|
}
|
|
|
|
Utils.fastMatToTexture2D(rgbaMat, texture);
|
|
if (rects.Length > 0)
|
|
{
|
|
StartCoroutine(this.FindFaceFromServer(texture.EncodeToJPG()));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the destroy event.
|
|
/// </summary>
|
|
private void OnDestroy()
|
|
{
|
|
webCamTextureToMatHelper.Dispose();
|
|
|
|
if (cascade != null)
|
|
cascade.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the back button click event.
|
|
/// </summary>
|
|
public void OnBackButtonClick()
|
|
{
|
|
SceneManager.LoadScene(0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the play button click event.
|
|
/// </summary>
|
|
public void OnPlayButtonClick()
|
|
{
|
|
webCamTextureToMatHelper.Play();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the pause button click event.
|
|
/// </summary>
|
|
public void OnPauseButtonClick()
|
|
{
|
|
webCamTextureToMatHelper.Pause();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the stop button click event.
|
|
/// </summary>
|
|
public void OnStopButtonClick()
|
|
{
|
|
webCamTextureToMatHelper.Stop();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the change camera button click event.
|
|
/// </summary>
|
|
public void OnChangeCameraButtonClick()
|
|
{
|
|
webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing();
|
|
}
|
|
|
|
private IEnumerator FindFaceFromServer(byte[] jpgBytes)//Mat grayMat)
|
|
{
|
|
if (!this._isChecking)
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
if (!this._isChecking)
|
|
{
|
|
this._isChecking = true;
|
|
}
|
|
{
|
|
//var tempTexture = new Texture2D(grayMat.cols(), grayMat.rows(), TextureFormat.RGBA32, false);
|
|
//var jpgBytes = tempTexture.EncodeToJPG();
|
|
var form = new WWWForm();
|
|
form.AddBinaryData("face", jpgBytes);
|
|
using (var request = UnityWebRequest.Post("http://192.168.253.1:8000/Account/FaceLogin", form))
|
|
{
|
|
yield return request.SendWebRequest();
|
|
if (request.isNetworkError || request.isHttpError)
|
|
{
|
|
Debug.Log(request.error);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log(request.downloadHandler.text);
|
|
var result = JsonUtility.FromJson<LoginResult>(request.downloadHandler.text);
|
|
if (result.Success)
|
|
{
|
|
text.text = result.NickName;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this._isChecking = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |