Ejemplo n.º 1
0
    private void Update()
    {
        if (this.model != null && this.texture != null)
        {
            CoachResult prediction;
            if (sync)
            {
                prediction = model.Predict(this.texture);
            }
            else
            {
                prediction = model.GetPredictionResultAsync();
                StartCoroutine(model.PredictAsync(this.texture));
            }

            if (prediction != null)
            {
                var result        = prediction.Best();
                var printedResult = $"{result.Label}: {result.Confidence.ToString()}";
                Debug.Log((sync ? "sync" : "async") + " || " + printedResult);
                resultLabel.text = printedResult;
            }
        }
    }
    void Update()
    {
        // Skip making adjustment for incorrect camera data
        if (activeCameraTexture.width < 100)
        {
            return;
        }

        // Rotate image to show correct orientation
        rotationVector.z = -activeCameraTexture.videoRotationAngle;
        image.rectTransform.localEulerAngles = rotationVector;

        //image.rectTransform.sizeDelta = new Vector2(image.rectTransform.sizeDelta.x, Screen.height);

        // Set AspectRatioFitter's ratio
        float videoRatio =
            (float)activeCameraTexture.width / (float)activeCameraTexture.height;

        imageFitter.aspectRatio = videoRatio;

        // Unflip if vertically flipped
        image.uvRect =
            activeCameraTexture.videoVerticallyMirrored ? fixedRect : defaultRect;

        // Mirror front-facing camera's image horizontally to look more natural
        imageParent.localScale =
            activeCameraDevice.isFrontFacing ? fixedScale : defaultScale;

        if (model != null)
        {
            // Every once in a while when the workers aren't busy
            aTime += Time.deltaTime;
            if (aTime >= 0.5f && model.WorkerAvailable())
            {
                aTime = 0;
                StartCoroutine(model.PredictAsync(GetWebcamPhoto()));
            }

            /* Check for results every Update
             * var modelResult = model.GetPredictionResultAsync(true);
             * if (modelResult != null)
             * {
             *  var best = modelResult.Best();
             *
             *  Debug.Log("We have a result: " + best.Label + ": " + best.Confidence);
             *  predictionResult.text = best.Label + ": " + best.Confidence;
             * }
             */

            // Example of using cumulative:
            model.CumulativeConfidenceAsync(5f, ref results, true);
            if (results.LastResult != null)
            {
                var best = results.LastResult.Best();
                if (results.IsPassedThreshold())
                {
                    Debug.LogWarning("Passed the threshold");

                    var result = $"{best.Label}: {best.Confidence}";
                    predictionResult.text = result;
                }
                Debug.Log("We have a result: " + best.Label + ": " + best.Confidence);
            }
        }
    }