Example #1
0
    // Use this for initialization
    IEnumerator Start()
    {
        bool tryUseCamera = true;
        bool loaded       = Emgu.TF.Lite.TfLiteInvoke.CheckLibraryLoaded();

        _mobilenet = new Emgu.TF.Lite.Models.Mobilenet();

        WebCamDevice[] devices = WebCamTexture.devices;

        if (tryUseCamera && devices.Length != 0)
        {
            yield return(Application.RequestUserAuthorization(UserAuthorization.WebCam));

            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                UnityEngine.Debug.Log("webcam use authorized");
                _webcamTexture = new WebCamTexture(devices[0].name);

                RawImage image = this.GetComponent <RawImage>();
                image.texture = _webcamTexture;

                _webcamTexture.Play();
            }
        }
        DisplayText.text = "Downloading model, please wait...";
        StartCoroutine(_mobilenet.Init());
    }
Example #2
0
    // Use this for initialization
    void Start()
    {
        bool loaded = Emgu.TF.Lite.TfLiteInvoke.CheckLibraryLoaded();

        _mobilenet = new Emgu.TF.Lite.Models.Mobilenet();

        _liveCameraView = false;

        /*
         * WebCamDevice[] devices = WebCamTexture.devices;
         * cameraCount = devices.Length;
         *
         * if (cameraCount == 0)
         * {
         *  _liveCameraView = false;
         * }
         * else
         * {
         *  _liveCameraView = true;
         *  webcamTexture = new WebCamTexture(devices[0].name);
         *
         *  baseRotation = transform.rotation;
         *  webcamTexture.Play();
         *  //data = new Color32[webcamTexture.width * webcamTexture.height];
         * }*/
        DisplayText.text = "Downloading model, please wait...";
        StartCoroutine(_mobilenet.Init());
    }
Example #3
0
        public async Task TestMobilenet()
        {
            using (Mobilenet mobilenet = new Mobilenet())
            {
                await mobilenet.Init();

                var result = mobilenet.Recognize("grace_hopper.jpg")[0];
            }
        }
Example #4
0
        public MobilenetPage()
            : base()
        {
            var button = this.TopButton;

            button.Text     = "Perform Image Classification";
            button.Clicked += OnButtonClicked;

            _mobilenet = new Mobilenet();
            _mobilenet.OnDownloadProgressChanged += onDownloadProgressChanged;
        }
Example #5
0
    // Use this for initialization
    void Start()
    {
        bool loaded = Emgu.TF.Lite.TfLiteInvoke.CheckLibraryLoaded();

        _mobilenet = new Emgu.TF.Lite.Models.Mobilenet();

        WebCamDevice[] devices = WebCamTexture.devices;
        if (false)
        //if (devices.Length != 0)
        {
            _webcamTexture = new WebCamTexture(devices[0].name);
            baseRotation   = transform.rotation;
            _webcamTexture.Play();
        }
        DisplayText.text = "Downloading model, please wait...";
        StartCoroutine(_mobilenet.Init());
    }
Example #6
0
        public void TestMobilenet()
        {
            using (Mobilenet mobilenet = new Mobilenet())
            {
                bool processCompleted = false;
                mobilenet.OnDownloadCompleted += (sender, e) =>
                {
                    var result = mobilenet.MostLikely("grace_hopper.jpg");

                    processCompleted = true;
                };

                mobilenet.Init();
                while (!processCompleted)
                {
                    Thread.Sleep(1000);
                }
            }
        }
Example #7
0
        public MobilenetPage()
            : base()
        {
            var button = this.GetButton();

            button.Text     = "Perform Image Classification";
            button.Clicked += OnButtonClicked;

            _mobilenet = new Mobilenet();

            OnImagesLoaded += (sender, image) =>
            {
                SetMessage("Please wait...");
                SetImage();
                _image = image;

#if !DEBUG
                try
#endif
                {
                    if (_mobilenet.Imported)
                    {
                        onDownloadCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(null, false, null));
                    }
                    else
                    {
                        SetMessage("Please wait while the Mobilenet Model is being downloaded...");
                        _mobilenet.OnDownloadProgressChanged += onDownloadProgressChanged;
                        _mobilenet.OnDownloadCompleted       += onDownloadCompleted;
                        _mobilenet.Init();
                    }
                }
#if !DEBUG
                catch (Exception e)
                {
                    String msg = e.Message.Replace(System.Environment.NewLine, " ");
                    SetMessage(msg);
                }
#endif
            };
        }
Example #8
0
        public MobilenetPage()
            : base()
        {
            var button = this.TopButton;

            button.Text     = "Perform Mobilenet Image Classification";
            button.Clicked += OnButtonClicked;

            var picker = this.Picker;

            picker.Title = "TF Lite backend";


            picker.Items.Add("CPU");

            #if __ANDROID__
            if (TfLiteInvoke.DefaultNnApiDelegate != null)
            {
                picker.Items.Add("NNAPI");
            }
            #endif


            if (TfLiteInvoke.DefaultGpuDelegateV2 != null)
            {
                picker.Items.Add("GPU");
            }

            picker.IsVisible = picker.Items.Count > 1;

            picker.SelectedIndexChanged += (sender, args) =>
            {
                if (_mobilenet != null)
                {
                    _mobilenet.Dispose();
                    _mobilenet = null;
                }
            };
        }
Example #9
0
        private async void OnButtonClicked(Object sender, EventArgs args)
        {
            SetImage();
            String[] imageFiles = await LoadImages(new string[] { "space_shuttle.jpg" });

            //handle user cancel
            if (imageFiles == null || (imageFiles.Length > 0 && imageFiles[0] == null))
            {
                SetMessage("");
                return;
            }

            SetMessage("Please wait while the Mobilenet Model is being downloaded...");

            if (_mobilenet == null)
            {
                _mobilenet = new Mobilenet();
                _mobilenet.OnDownloadProgressChanged += onDownloadProgressChanged;
            }

            await _mobilenet.Init();

            var picker = this.Picker;

            if (picker.SelectedIndex > 0)
            {
                String selectedBackend = picker.SelectedItem.ToString();
                if (selectedBackend.Equals("NNAPI"))
                {
                    try
                    {
                        Status addNNApiDelegateStatus = _mobilenet.Interpreter.ModifyGraphWithDelegate(TfLiteInvoke.DefaultNnApiDelegate);
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.WriteLine(e);
                        throw;
                    }
                }
                else if (selectedBackend.Equals("GPU"))
                {
                    try
                    {
                        Status addGpuDelegateStatus = _mobilenet.Interpreter.ModifyGraphWithDelegate(TfLiteInvoke.DefaultGpuDelegateV2);
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.WriteLine(e);
                        throw;
                    }
                }
            }

            Stopwatch watch  = Stopwatch.StartNew();
            var       result = _mobilenet.Recognize(imageFiles[0]);

            watch.Stop();
            String resStr = String.Format("Object is {0} with {1}% probability. Recognition completed in {2} milliseconds.", result[0].Label, result[0].Probability * 100, watch.ElapsedMilliseconds);

            SetImage(imageFiles[0]);
            SetMessage(resStr);
        }