123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using OpenCVForUnity.ObjdetectModule;
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.ImgprocModule;
- using OpenCVForUnity.UnityUtils;
- namespace OpenCVForUnityExample
- {
- /// <summary>
- /// Face Detection Example
- /// An example of human face detection using the CascadeClassifier class.
- /// http://docs.opencv.org/3.2.0/db/d28/tutorial_cascade_classifier.html
- /// </summary>
- public class FaceDetectionExample : MonoBehaviour
- {
- CascadeClassifier cascade;
- /// <summary>
- /// HAAR_CASCADE_FILENAME
- /// </summary>
- protected static readonly string HAAR_CASCADE_FILENAME = "OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml";
- #if UNITY_WEBGL
- IEnumerator getFilePath_Coroutine;
- #endif
- // Use this for initialization
- void Start()
- {
- #if UNITY_WEBGL
- getFilePath_Coroutine = Utils.getFilePathAsync (HAAR_CASCADE_FILENAME,
- (result) => {
- getFilePath_Coroutine = null;
- if (string.IsNullOrEmpty(result))
- {
- Debug.LogError(HAAR_CASCADE_FILENAME + " is not loaded. Please move from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/” to “Assets/StreamingAssets/OpenCVForUnity/” folder.");
- }
- else
- {
- cascade = new CascadeClassifier(result);
- }
- Run ();
- },
- (result, progress) => {
- Debug.Log ("getFilePathAsync() progress : " + result + " " + Mathf.CeilToInt (progress * 100) + "%");
- });
- StartCoroutine (getFilePath_Coroutine);
- #else
- string cascade_filepath = Utils.getFilePath(HAAR_CASCADE_FILENAME);
- if (string.IsNullOrEmpty(cascade_filepath))
- {
- Debug.LogError(HAAR_CASCADE_FILENAME + " is not loaded. Please move from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/” to “Assets/StreamingAssets/OpenCVForUnity/” folder.");
- }
- else
- {
- cascade = new CascadeClassifier(cascade_filepath);
- }
- Run();
- #endif
- }
- private void Run()
- {
- Texture2D imgTexture = Resources.Load("face") as Texture2D;
- Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC4);
- Utils.texture2DToMat(imgTexture, imgMat);
- Debug.Log("imgMat.ToString() " + imgMat.ToString());
- if (cascade == null)
- {
- Imgproc.putText(imgMat, "model file is not loaded.", new Point(5, imgMat.rows() - 30), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
- Imgproc.putText(imgMat, "Please read console message.", new Point(5, imgMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
- Texture2D _texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
- Utils.matToTexture2D(imgMat, _texture);
- gameObject.GetComponent<Renderer>().material.mainTexture = _texture;
- return;
- }
- Mat grayMat = new Mat();
- Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
- Imgproc.equalizeHist(grayMat, grayMat);
- MatOfRect faces = new MatOfRect();
- if (cascade != null)
- cascade.detectMultiScale(grayMat, faces, 1.1, 2, 2,
- new Size(20, 20), new Size());
- OpenCVForUnity.CoreModule.Rect[] rects = faces.toArray();
- for (int i = 0; i < rects.Length; i++)
- {
- Debug.Log("detect faces " + rects[i]);
- Imgproc.rectangle(imgMat, 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);
- }
- Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
- Utils.matToTexture2D(imgMat, texture);
- gameObject.GetComponent<Renderer>().material.mainTexture = texture;
- }
- // Update is called once per frame
- void Update()
- {
- }
- /// <summary>
- /// Raises the destroy event.
- /// </summary>
- void OnDestroy()
- {
- #if UNITY_WEBGL
- if (getFilePath_Coroutine != null) {
- StopCoroutine (getFilePath_Coroutine);
- ((IDisposable)getFilePath_Coroutine).Dispose ();
- }
- #endif
- }
- /// <summary>
- /// Raises the back button click event.
- /// </summary>
- public void OnBackButtonClick()
- {
- SceneManager.LoadScene("OpenCVForUnityExample");
- }
- }
- }
|