Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using Emgu.CV.CvEnum;
- using Image = UnityEngine.UI.Image;
- using System.Drawing;
- using System.Collections;
- using System.Runtime.InteropServices;
- using NumSharp;
- using static Tensorflow.Binding;
- using Tensorflow.Keras;
- using Tensorflow;
- using System.IO;
- using System;
- using Unity.Profiling;
- public class EMGUTest : MonoBehaviour
- {
- public Text resultText;
- //camera stuff
- BackgroundSubtractorKNN backgroundsub = new BackgroundSubtractorKNN(1000,50,false);
- Session sess;
- private WebCamTexture webcamTexture;
- private Texture2D resultTexture;
- private Color32[] data;
- private byte[] bytes;
- public int cameraCount = 0;
- private Quaternion baseRotation;
- private Graph graph;
- static ProfilerMarker s_PreparePerfMarker = new ProfilerMarker("MySystem.Prepare");
- static ProfilerMarker s_SimulatePerfMarker = new ProfilerMarker("MySystem.Simulate");
- static ProfilerMarker s_SimulateAIMarker = new ProfilerMarker("MySystem.AI");
- // Use this for initialization
- void Start()
- {
- var model_path = Application.dataPath + "/Resources/NN Models/frozen1812/frozen_graph.pb";
- var graph_def = GraphDef.Parser.ParseFrom(File.ReadAllBytes(model_path));
- var graph = new Graph().as_default();
- tf.import_graph_def(graph_def);
- //byte[] model = File.ReadAllBytes(Application.dataPath + "/Resources/NN Models/frozen1812/frozen_graph.pb");
- //graph = new Graph();
- //print(model);
- //graph.Import(model,"");
- sess = new Session(graph);
- StartCoroutine(LateStart());
- }
- IEnumerator LateStart() {
- yield return new WaitForSeconds(1);
- webcamTexture = new WebCamTexture();
- webcamTexture.Play();
- //data = new Color32[webcamTexture.width * webcamTexture.height];
- CvInvoke.CheckLibraryLoaded();
- }
- void Update()
- {
- s_PreparePerfMarker.Begin();
- if (webcamTexture != null && webcamTexture.didUpdateThisFrame)
- {
- if (data == null || (data.Length != webcamTexture.width * webcamTexture.height))
- {
- data = new Color32[webcamTexture.width * webcamTexture.height];
- }
- webcamTexture.GetPixels32(data);
- if (bytes == null || bytes.Length != data.Length * 3)
- {
- bytes = new byte[data.Length * 3];
- }
- GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
- GCHandle resultHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
- using (Mat bgra = new Mat(new Size(webcamTexture.width, webcamTexture.height), DepthType.Cv8U, 4, handle.AddrOfPinnedObject(), webcamTexture.width * 4))
- using (Mat bgr = new Mat(webcamTexture.height, webcamTexture.width, DepthType.Cv8U, 3, resultHandle.AddrOfPinnedObject(), webcamTexture.width * 3))
- {
- #region do some image processing here
- CvInvoke.CvtColor(bgra, bgr, ColorConversion.Bgra2Gray);
- backgroundsub.Apply(bgr, bgr, .2);
- CvInvoke.Blur(bgr, bgr, new Size(5, 5), new Point(2, 2));
- CvInvoke.Dilate(bgr, bgr, new Mat(), new Point(1, 1),5, BorderType.Constant, new MCvScalar());
- CvInvoke.Blur(bgr, bgr, new Size(2, 2), new Point(1, 1));
- CvInvoke.Threshold(bgr, bgr, 240, 255, ThresholdType.Binary);
- CvInvoke.Blur(bgr, bgr, new Size(3, 3), new Point(1, 1));
- CvInvoke.Blur(bgr, bgr, new Size(5, 5), new Point(3, 3));
- CvInvoke.Flip(bgr, bgr, FlipType.Vertical);
- resultTexture = TextureConvert.InputArrayToTexture2D(bgr);
- s_PreparePerfMarker.End();
- RenderTexture(resultTexture);
- #endregion
- handle.Free();
- resultHandle.Free();
- transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);
- //this.GetComponent<GUITexture>().texture = resultTexture;
- AIProcessing(bgr);
- //count++;
- }
- }
- }
- private void RenderTexture(Texture2D tex)
- {
- Image image = this.GetComponent<Image>();
- image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
- }
- private void ResizeTexture(Texture2D texture)
- {
- Image image = this.GetComponent<Image>();
- var transform = image.rectTransform;
- transform.sizeDelta = new Vector2(texture.width, texture.height);
- }
- public void AIProcessing(Mat image) {
- Shape shape = new Shape(1, image.Width, image.Height, 1);
- NDArray img = new NDArray(image.GetRawData(), shape);
- using (s_SimulatePerfMarker.Auto()) {
- //Tensor tensor = new Tensor(image.GetRawData(),tf.float16);
- var input_operation = tf.get_default_graph().get_operation_by_name("import/x");
- var output_operation = tf.get_default_graph().get_operation_by_name("import/Identity");
- FeedItem item = new FeedItem(input_operation.outputs[0], img);
- s_SimulateAIMarker.Begin();
- var results = sess.run(output_operation.outputs[0], item);
- s_SimulateAIMarker.End();
- results = np.squeeze(results);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement