123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #if !UNITY_WSA_10_0
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.DnnModule;
- using OpenCVForUnity.ImgprocModule;
- using OpenCVForUnity.UnityUtils;
- using OpenCVForUnity.UnityUtils.Helper;
- namespace OpenCVForUnityExample
- {
- /// <summary>
- /// TensorFlow WebCamTexture Example
- /// An example of using OpenCV dnn module with Tensorflow Inception model.
- /// Referring to https://github.com/opencv/opencv/blob/master/samples/dnn/tf_inception.cpp.
- /// </summary>
- [RequireComponent (typeof(WebCamTextureToMatHelper))]
- public class TensorFlowWebCamTextureExample : MonoBehaviour
- {
- /// <summary>
- /// The texture.
- /// </summary>
- Texture2D texture;
- /// <summary>
- /// The webcam texture to mat helper.
- /// </summary>
- WebCamTextureToMatHelper webCamTextureToMatHelper;
- /// <summary>
- /// The bgr mat.
- /// </summary>
- Mat bgrMat;
- /// <summary>
- /// The BLOB.
- /// </summary>
- Mat blob;
- /// <summary>
- /// The net.
- /// </summary>
- Net net;
- /// <summary>
- /// The classes.
- /// </summary>
- List<string> classes;
- /// <summary>
- /// The FPS monitor.
- /// </summary>
- FpsMonitor fpsMonitor;
- string tensorflow_inception_graph_pb_filepath;
- string imagenet_comp_graph_label_strings_txt_filepath;
- #if UNITY_WEBGL && !UNITY_EDITOR
- IEnumerator getFilePath_Coroutine;
- #endif
- // Use this for initialization
- void Start ()
- {
- fpsMonitor = GetComponent<FpsMonitor> ();
- webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper> ();
- #if UNITY_WEBGL && !UNITY_EDITOR
- getFilePath_Coroutine = GetFilePath ();
- StartCoroutine (getFilePath_Coroutine);
- #else
- tensorflow_inception_graph_pb_filepath = Utils.getFilePath ("dnn/tensorflow_inception_graph.pb");
- imagenet_comp_graph_label_strings_txt_filepath = Utils.getFilePath ("dnn/imagenet_comp_graph_label_strings.txt");
- Run ();
- #endif
- }
- #if UNITY_WEBGL && !UNITY_EDITOR
- private IEnumerator GetFilePath()
- {
- var getFilePathAsync_0_Coroutine = Utils.getFilePathAsync ("dnn/tensorflow_inception_graph.pb", (result) => {
- tensorflow_inception_graph_pb_filepath = result;
- });
- yield return getFilePathAsync_0_Coroutine;
- var getFilePathAsync_1_Coroutine = Utils.getFilePathAsync ("dnn/imagenet_comp_graph_label_strings.txt", (result) => {
- imagenet_comp_graph_label_strings_txt_filepath = result;
- });
- yield return getFilePathAsync_1_Coroutine;
- getFilePath_Coroutine = null;
- Run ();
- }
- #endif
- // Use this for initialization
- void Run ()
- {
- //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
- Utils.setDebugMode (true);
- net = Dnn.readNetFromTensorflow (tensorflow_inception_graph_pb_filepath);
- #if !UNITY_WSA_10_0
- if (net.empty ()) {
- Debug.LogError ("model file is not loaded. The model and class names list can be downloaded here: \"https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip\". Please copy to “Assets/StreamingAssets/dnn/” folder. ");
- }
- #endif
- classes = readClassNames (imagenet_comp_graph_label_strings_txt_filepath);
- #if !UNITY_WSA_10_0
- if (classes == null) {
- Debug.LogError ("class names list file is not loaded. The model and class names list can be downloaded here: \"https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip\". Please copy to “Assets/StreamingAssets/dnn/” folder. ");
- }
- #endif
-
- #if UNITY_ANDROID && !UNITY_EDITOR
- // Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
- webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
- #endif
- webCamTextureToMatHelper.Initialize ();
- }
- /// <summary>
- /// Raises the webcam 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);
- if (fpsMonitor != null) {
- fpsMonitor.Add ("width", webCamTextureMat.width ().ToString ());
- fpsMonitor.Add ("height", webCamTextureMat.height ().ToString ());
- fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
- }
-
- 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;
- }
- bgrMat = new Mat (webCamTextureMat.rows (), webCamTextureMat.cols (), CvType.CV_8UC3);
- }
- /// <summary>
- /// Raises the webcam texture to mat helper disposed event.
- /// </summary>
- public void OnWebCamTextureToMatHelperDisposed ()
- {
- Debug.Log ("OnWebCamTextureToMatHelperDisposed");
- if (bgrMat != null)
- bgrMat.Dispose ();
- if (texture != null) {
- Texture2D.Destroy (texture);
- texture = null;
- }
- }
- /// <summary>
- /// Raises the webcam 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
- void Update ()
- {
- if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) {
- Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
- if (net.empty () || classes == null) {
- Imgproc.putText (rgbaMat, "model file is not loaded.", new Point (5, rgbaMat.rows () - 30), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
- Imgproc.putText (rgbaMat, "Please read console message.", new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
- } else {
- Imgproc.cvtColor (rgbaMat, bgrMat, Imgproc.COLOR_RGBA2BGR);
- blob = Dnn.blobFromImage (bgrMat, 1, new Size (224, 224), new Scalar (104, 117, 123), false, true);
- net.setInput (blob);
- Mat prob = net.forward ();
- Core.MinMaxLocResult minmax = Core.minMaxLoc (prob.reshape (1, 1));
- // Debug.Log ("Best match " + (int)minmax.maxLoc.x);
- // Debug.Log ("Best match class " + classes [(int)minmax.maxLoc.x]);
- // Debug.Log ("Probability: " + minmax.maxVal * 100 + "%");
- prob.Dispose ();
- //Imgproc.putText (rgbaMat, "Best match class " + classes [(int)minmax.maxLoc.x], new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
- if (fpsMonitor != null) {
- fpsMonitor.consoleText = "Best match class " + classes [(int)minmax.maxLoc.x];
- }
- }
- Utils.fastMatToTexture2D (rgbaMat, texture);
- }
- }
- /// <summary>
- /// Raises the disable event.
- /// </summary>
- void OnDisable ()
- {
- webCamTextureToMatHelper.Dispose ();
- if (blob != null)
- blob.Dispose ();
- if (net != null)
- net.Dispose ();
- Utils.setDebugMode (false);
- #if UNITY_WEBGL && !UNITY_EDITOR
- 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");
- }
- /// <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 List<string> readClassNames (string filename)
- {
- List<string> classNames = new List<string> ();
- System.IO.StreamReader cReader = null;
- try {
- cReader = new System.IO.StreamReader (filename, System.Text.Encoding.Default);
- while (cReader.Peek () >= 0) {
- string name = cReader.ReadLine ();
- classNames.Add (name);
- }
- } catch (System.Exception ex) {
- Debug.LogError (ex.Message);
- return null;
- } finally {
- if (cReader != null)
- cReader.Close ();
- }
- return classNames;
- }
- }
- }
- #endif
|