Example #1
0
    private void RecognizeAndUpdateText(Texture2D texture)
    {
        if (_inceptionGraph == null)
        {
            return;
        }
        Tensor imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f, true);

        float[] probability = _inceptionGraph.Recognize(imageTensor);

        //String resStr = String.Empty;
        if (probability != null)
        {
            float maxVal = 0;
            int   maxIdx = 0;
            for (int i = 0; i < probability.Length; i++)
            {
                if (probability[i] > maxVal)
                {
                    maxVal = probability[i];
                    maxIdx = i;
                }
            }
            DisplayText.text = String.Format("Object is {0} with {1}% probability.", _inceptionLabels[maxIdx], maxVal * 100);
        }
    }
Example #2
0
    private void RecognizeAndUpdateText(Texture2D texture)
    {
        if (!_inceptionGraph.Imported)
        {
            return;
        }
        Tensor imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f, true);

        Inception.RecognitionResult[] results = _inceptionGraph.Recognize(imageTensor);
        _displayMessage = String.Format("Object is {0} with {1}% probability.", results[0].Label, results[0].Probability * 100);
    }
Example #3
0
    // Use this for initialization
    void Start()
    {
        //Warning: The following code is used to get around a https certification issue for downloading tesseract language files from Github
        //Do not use this code in a production environment. Please make sure you understand the security implication from the following code before using it
        ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
            HttpWebRequest webRequest = sender as HttpWebRequest;
            if (webRequest != null)
            {
                String requestStr = webRequest.Address.AbsoluteUri;
                if (requestStr.StartsWith(@"https://github.com/") || requestStr.StartsWith(@"https://raw.githubusercontent.com/"))
                {
                    return(true);
                }
            }
            return(false);
        };

        TfInvoke.CheckLibraryLoaded();

        WebCamDevice[] devices     = WebCamTexture.devices;
        int            cameraCount = devices.Length;

        if (cameraCount == 0)
        {
            _liveCameraView = false;
            Texture2D texture     = Resources.Load <Texture2D>("surfers");
            Tensor    imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f / 128.0f, true);

            //byte[] raw = ImageIO.EncodeJpeg(imageTensor, 128.0f, 128.0f);
            //System.IO.File.WriteAllBytes("surfers_out.jpg", raw);

            _multiboxGraph = new MultiboxGraph();
            MultiboxGraph.Result results = _multiboxGraph.Detect(imageTensor);

            drawableTexture = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
            drawableTexture.SetPixels(texture.GetPixels());
            MultiboxGraph.DrawResults(drawableTexture, results, 0.1f);

            this.GetComponent <GUITexture>().texture    = drawableTexture;
            this.GetComponent <GUITexture>().pixelInset = new Rect(-texture.width / 2, -texture.height / 2, texture.width, texture.height);
        }
        else
        {
            _liveCameraView = true;
            webcamTexture   = new WebCamTexture(devices[0].name);
            _multiboxGraph  = new MultiboxGraph();
            baseRotation    = transform.rotation;
            webcamTexture.Play();
            //data = new Color32[webcamTexture.width * webcamTexture.height];
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!_multiboxGraph.Imported)
        {
            _displayMessage = String.Format("Downloading multibox model files, {0} % of file {1}...", _multiboxGraph.DownloadProgress * 100, _multiboxGraph.DownloadFileName);
        }
        else if (_liveCameraView)
        {
            if (webcamTexture != null && webcamTexture.didUpdateThisFrame)
            {
                #region convert the webcam texture to RGBA bytes

                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 * 4)
                {
                    bytes = new byte[data.Length * 4];
                }
                GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                Marshal.Copy(handle.AddrOfPinnedObject(), bytes, 0, bytes.Length);
                handle.Free();

                #endregion

                #region convert the RGBA bytes to texture2D

                if (resultTexture == null || resultTexture.width != webcamTexture.width ||
                    resultTexture.height != webcamTexture.height)
                {
                    resultTexture = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32,
                                                  false);
                }

                resultTexture.LoadRawTextureData(bytes);
                resultTexture.Apply();

                #endregion

                if (!_textureResized)
                {
                    this.GetComponent <GUITexture>().pixelInset = new Rect(-webcamTexture.width / 2,
                                                                           -webcamTexture.height / 2, webcamTexture.width, webcamTexture.height);
                    _textureResized = true;
                }

                transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);


                Tensor imageTensor           = ImageIO.ReadTensorFromTexture2D(resultTexture, 224, 224, 128.0f, 1.0f / 128.0f, true);
                MultiboxGraph.Result results = _multiboxGraph.Detect(imageTensor);

                if (drawableTexture == null || drawableTexture.width != resultTexture.width ||
                    drawableTexture.height != resultTexture.height)
                {
                    drawableTexture = new Texture2D(resultTexture.width, resultTexture.height, TextureFormat.ARGB32, false);
                }
                drawableTexture.SetPixels(resultTexture.GetPixels());
                MultiboxGraph.DrawResults(drawableTexture, results, 0.2f);

                this.GetComponent <GUITexture>().texture = drawableTexture;
                //count++;
            }
        }
        else if (!_staticViewRendered)
        {
            Texture2D texture     = Resources.Load <Texture2D>("surfers");
            Tensor    imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f / 128.0f, true);

            //byte[] raw = ImageIO.EncodeJpeg(imageTensor, 128.0f, 128.0f);
            //System.IO.File.WriteAllBytes("surfers_out.jpg", raw);


            MultiboxGraph.Result results = _multiboxGraph.Detect(imageTensor);

            drawableTexture = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
            drawableTexture.SetPixels(texture.GetPixels());
            MultiboxGraph.DrawResults(drawableTexture, results, 0.1f);

            this.GetComponent <GUITexture>().texture    = drawableTexture;
            this.GetComponent <GUITexture>().pixelInset = new Rect(-texture.width / 2, -texture.height / 2, texture.width, texture.height);

            _displayMessage     = String.Empty;
            _staticViewRendered = true;
        }

        DisplayText.text = _displayMessage;
    }
Example #5
0
    // Update is called once per frame
    void Update()
    {
        if (_liveCameraView)
        {
            if (webcamTexture != null && webcamTexture.didUpdateThisFrame)
            {
                #region convert the webcam texture to RGBA bytes

                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 * 4)
                {
                    bytes = new byte[data.Length * 4];
                }
                GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                Marshal.Copy(handle.AddrOfPinnedObject(), bytes, 0, bytes.Length);
                handle.Free();

                #endregion

                #region convert the RGBA bytes to texture2D

                if (resultTexture == null || resultTexture.width != webcamTexture.width ||
                    resultTexture.height != webcamTexture.height)
                {
                    resultTexture = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32,
                                                  false);
                }

                resultTexture.LoadRawTextureData(bytes);
                resultTexture.Apply();

                #endregion

                if (!_textureResized)
                {
                    this.GetComponent <GUITexture>().pixelInset = new Rect(-webcamTexture.width / 2,
                                                                           -webcamTexture.height / 2, webcamTexture.width, webcamTexture.height);
                    _textureResized = true;
                }

                transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);


                Tensor imageTensor           = ImageIO.ReadTensorFromTexture2D(resultTexture, 224, 224, 128.0f, 1.0f / 128.0f, true);
                MultiboxGraph.Result results = _multiboxGraph.Detect(imageTensor);

                if (drawableTexture == null || drawableTexture.width != resultTexture.width ||
                    drawableTexture.height != resultTexture.height)
                {
                    drawableTexture = new Texture2D(resultTexture.width, resultTexture.height, TextureFormat.ARGB32, false);
                }
                drawableTexture.SetPixels(resultTexture.GetPixels());
                MultiboxGraph.DrawResults(drawableTexture, results, 0.1f);

                this.GetComponent <GUITexture>().texture = drawableTexture;
                //count++;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (_multiboxGraph == null)
        {
            _multiboxGraph = new MultiboxGraph();
            StartCoroutine(_multiboxGraph.Init());
        }
        else if (!_multiboxGraph.Imported)
        {
            _displayMessage = String.Format("Downloading multibox model files, {0} % of file {1}...", _multiboxGraph.DownloadProgress * 100, _multiboxGraph.DownloadFileName);
        }
        else if (_liveCameraView)
        {
            _displayMessage = String.Empty;

            if (_webcamTexture != null && _webcamTexture.didUpdateThisFrame)
            {
                int webcamWidth  = _webcamTexture.width;
                int webcamHeight = _webcamTexture.height;
                #region convert the webcam texture to RGBA bytes

                if (_data == null || (_data.Length != webcamWidth * webcamHeight))
                {
                    _data = new Color32[webcamWidth * webcamHeight];
                }
                _webcamTexture.GetPixels32(_data);

                if (_bytes == null || _bytes.Length != _data.Length * 4)
                {
                    _bytes = new byte[_data.Length * 4];
                }
                GCHandle handle = GCHandle.Alloc(_data, GCHandleType.Pinned);
                Marshal.Copy(handle.AddrOfPinnedObject(), _bytes, 0, _bytes.Length);
                handle.Free();

                #endregion

                #region convert the RGBA bytes to texture2D
                if (_drawableTexture == null || _drawableTexture.width != webcamWidth ||
                    _drawableTexture.height != webcamHeight)
                {
                    _drawableTexture = new Texture2D(webcamWidth, webcamHeight, TextureFormat.RGBA32,
                                                     false);
                }

                _drawableTexture.LoadRawTextureData(_bytes);
                _drawableTexture.Apply();

                #endregion

                //Tensor imageTensor = ImageIO.ReadTensorFromTexture2D(_drawableTexture, 224, 224, 128.0f, 1.0f/128.0f, true);
                Tensor imageTensor = ImageIO.ReadTensorFromColor32(
                    _data,
                    webcamWidth,
                    webcamHeight,
                    224,
                    224,
                    128.0f,
                    1.0f / 128.0f,
                    true);
                MultiboxGraph.Result[] results = _multiboxGraph.Detect(imageTensor);

                MultiboxGraph.DrawResults(_drawableTexture, results, 0.2f, true);

                if (!_textureResized)
                {
                    ResizeTexture(_drawableTexture);
                    _textureResized = true;
                }

                this.transform.rotation = _baseRotation * Quaternion.AngleAxis(_webcamTexture.videoRotationAngle, Vector3.up);
                RenderTexture(_drawableTexture);
                //count++;
            }
        }
        else if (!_staticViewRendered)
        {
            Texture2D texture     = Resources.Load <Texture2D>("surfers");
            Tensor    imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f / 128.0f, true);

            //byte[] raw = ImageIO.EncodeJpeg(imageTensor, 128.0f, 128.0f);
            //System.IO.File.WriteAllBytes("surfers_out.jpg", raw);

            MultiboxGraph.Result[] results = _multiboxGraph.Detect(imageTensor);

            _drawableTexture = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
            _drawableTexture.SetPixels32(texture.GetPixels32());
            MultiboxGraph.DrawResults(_drawableTexture, results, 0.1f, true);

            RenderTexture(_drawableTexture);
            ResizeTexture(_drawableTexture);

            _displayMessage     = String.Empty;
            _staticViewRendered = true;
        }

        DisplayText.text = _displayMessage;
    }