Advertisement
Guest User

Untitled

a guest
Dec 20th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using Emgu.CV;
  4. using Emgu.CV.Structure;
  5. using Emgu.CV.CvEnum;
  6. using Image = UnityEngine.UI.Image;
  7. using System.Drawing;
  8. using System.Collections;
  9. using System.Runtime.InteropServices;
  10. using NumSharp;
  11. using static Tensorflow.Binding;
  12. using Tensorflow.Keras;
  13. using Tensorflow;
  14. using System.IO;
  15. using System;
  16. using Unity.Profiling;
  17.  
  18. public class EMGUTest : MonoBehaviour
  19. {
  20.  
  21. public Text resultText;
  22.  
  23. //camera stuff
  24.  
  25. BackgroundSubtractorKNN backgroundsub = new BackgroundSubtractorKNN(1000,50,false);
  26. Session sess;
  27. private WebCamTexture webcamTexture;
  28. private Texture2D resultTexture;
  29. private Color32[] data;
  30. private byte[] bytes;
  31. public int cameraCount = 0;
  32. private Quaternion baseRotation;
  33. private Graph graph;
  34. static ProfilerMarker s_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare");
  35. static ProfilerMarker s_SimulatePerfMarker = new ProfilerMarker("MySystem.Simulate");
  36. static ProfilerMarker s_SimulateAIMarker = new ProfilerMarker("MySystem.AI");
  37. // Use this for initialization
  38. void Start()
  39. {
  40.  
  41. var model_path = Application.dataPath + "/Resources/NN Models/frozen1812/frozen_graph.pb";
  42. var graph_def = GraphDef.Parser.ParseFrom(File.ReadAllBytes(model_path));
  43.  
  44. var graph = new Graph().as_default();
  45. tf.import_graph_def(graph_def);
  46.  
  47. //byte[] model = File.ReadAllBytes(Application.dataPath + "/Resources/NN Models/frozen1812/frozen_graph.pb");
  48. //graph = new Graph();
  49. //print(model);
  50. //graph.Import(model,"");
  51. sess = new Session(graph);
  52.  
  53.  
  54. StartCoroutine(LateStart());
  55. }
  56.  
  57.  
  58. IEnumerator LateStart() {
  59. yield return new WaitForSeconds(1);
  60.  
  61. webcamTexture = new WebCamTexture();
  62. webcamTexture.Play();
  63. //data = new Color32[webcamTexture.width * webcamTexture.height];
  64. CvInvoke.CheckLibraryLoaded();
  65.  
  66. }
  67. void Update()
  68. {
  69. s_PreparePerfMarker.Begin();
  70. if (webcamTexture != null && webcamTexture.didUpdateThisFrame)
  71. {
  72. if (data == null || (data.Length != webcamTexture.width * webcamTexture.height))
  73. {
  74. data = new Color32[webcamTexture.width * webcamTexture.height];
  75. }
  76. webcamTexture.GetPixels32(data);
  77.  
  78. if (bytes == null || bytes.Length != data.Length * 3)
  79. {
  80. bytes = new byte[data.Length * 3];
  81. }
  82. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  83. GCHandle resultHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  84. using (Mat bgra = new Mat(new Size(webcamTexture.width, webcamTexture.height), DepthType.Cv8U, 4, handle.AddrOfPinnedObject(), webcamTexture.width * 4))
  85. using (Mat bgr = new Mat(webcamTexture.height, webcamTexture.width, DepthType.Cv8U, 3, resultHandle.AddrOfPinnedObject(), webcamTexture.width * 3))
  86. {
  87. #region do some image processing here
  88. CvInvoke.CvtColor(bgra, bgr, ColorConversion.Bgra2Gray);
  89. backgroundsub.Apply(bgr, bgr, .2);
  90. CvInvoke.Blur(bgr, bgr, new Size(5, 5), new Point(2, 2));
  91. CvInvoke.Dilate(bgr, bgr, new Mat(), new Point(1, 1),5, BorderType.Constant, new MCvScalar());
  92. CvInvoke.Blur(bgr, bgr, new Size(2, 2), new Point(1, 1));
  93. CvInvoke.Threshold(bgr, bgr, 240, 255, ThresholdType.Binary);
  94. CvInvoke.Blur(bgr, bgr, new Size(3, 3), new Point(1, 1));
  95. CvInvoke.Blur(bgr, bgr, new Size(5, 5), new Point(3, 3));
  96. CvInvoke.Flip(bgr, bgr, FlipType.Vertical);
  97. resultTexture = TextureConvert.InputArrayToTexture2D(bgr);
  98. s_PreparePerfMarker.End();
  99. RenderTexture(resultTexture);
  100. #endregion
  101.  
  102. handle.Free();
  103. resultHandle.Free();
  104. transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);
  105. //this.GetComponent<GUITexture>().texture = resultTexture;
  106. AIProcessing(bgr);
  107. //count++;
  108. }
  109. }
  110. }
  111. private void RenderTexture(Texture2D tex)
  112. {
  113. Image image = this.GetComponent<Image>();
  114. image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  115. }
  116.  
  117. private void ResizeTexture(Texture2D texture)
  118. {
  119. Image image = this.GetComponent<Image>();
  120. var transform = image.rectTransform;
  121. transform.sizeDelta = new Vector2(texture.width, texture.height);
  122.  
  123. }
  124.  
  125.  
  126. public void AIProcessing(Mat image) {
  127. Shape shape = new Shape(1, image.Width, image.Height, 1);
  128. NDArray img = new NDArray(image.GetRawData(), shape);
  129. using (s_SimulatePerfMarker.Auto()) {
  130.  
  131. //Tensor tensor = new Tensor(image.GetRawData(),tf.float16);
  132.  
  133.  
  134. var input_operation = tf.get_default_graph().get_operation_by_name("import/x");
  135. var output_operation = tf.get_default_graph().get_operation_by_name("import/Identity");
  136. FeedItem item = new FeedItem(input_operation.outputs[0], img);
  137. s_SimulateAIMarker.Begin();
  138. var results = sess.run(output_operation.outputs[0], item);
  139. s_SimulateAIMarker.End();
  140. results = np.squeeze(results);
  141.  
  142. }
  143. }
  144.  
  145.  
  146.  
  147.  
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement