void MakePrediction(UIImage image)
        {
            //
            // Get an input from the image
            //
            IMLFeatureProvider input = CreateInput(image);

            //
            // Predict what's in the image
            //
            var output = model.GetPrediction(input, out var error);

            if (error != null)
            {
                Console.WriteLine($"Error predicting: {error}");
                return;
            }

            var classLabel = output.GetFeatureValue("classLabel").StringValue;

            //
            // Display everything
            //
            Console.WriteLine($"Recognized:");
            foreach (NSString n in output.FeatureNames)
            {
                var value = output.GetFeatureValue(n);
                Console.WriteLine($"  {n} == {value}");
            }

            var message = $"{classLabel.ToUpperInvariant ()} with probability {output.GetFeatureValue ("classLabelProbs").DictionaryValue[classLabel]}";

            Console.WriteLine(message);

            BeginInvokeOnMainThread(() => {
                var alert = UIAlertController.Create(message, "I hope it's right!", UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (obj) => {
                    DismissViewController(true, null);
                }));
                PresentViewController(alert, true, null);
            });
        }
Exemplo n.º 2
0
        public double UpdateEngagement(FaceAnchorReading f)
        {
            blink.Push((f.FacialExpressions["EyeBlinkLeft"].Value + f.FacialExpressions["EyeBlinkRight"].Value) / 2);
            smile.Push((f.FacialExpressions["MouthSmileLeft"].Value + f.FacialExpressions["MouthSmileRight"].Value) / 2);
            frown.Push((f.FacialExpressions["MouthFrownLeft"].Value + f.FacialExpressions["MouthFrownRight"].Value) / 2);
            squint.Push((f.FacialExpressions["EyeSquintLeft"].Value + f.FacialExpressions["EyeSquintRight"].Value) / 2);
            gazeIn.Push((f.FacialExpressions["EyeLookInLeft"].Value + f.FacialExpressions["EyeLookInRight"].Value) / 2);
            gazeOut.Push((f.FacialExpressions["EyeLookOutLeft"].Value + f.FacialExpressions["EyeLookOutRight"].Value) / 2);

            if (previousReading == null)
            {
                headSpeed.Push(0);

                eyeDwell.Push(0);
                headTilt.Push(0);
            }
            else
            {
                var deltaTime = f.ReadingTimestamp.Subtract(previousReading.ReadingTimestamp);
                var deltaHead = VectorMagnitude(VectorDifference(previousReading.LookAtPointTransform, f.LookAtPointTransform));
                headSpeed.Push((float)(deltaHead / deltaTime.TotalMilliseconds));

                var leftEyeDisplacement  = CoordinateDisplacement(f.LeftEyeTransform);
                var rightEyeDisplacement = CoordinateDisplacement(f.RightEyeTransform);
                var eyeDisplacement      = Math.Abs(leftEyeDisplacement[0] + rightEyeDisplacement[0]) / 2;
                eyeDwell.Push((float)(eyeDisplacement > 0 ? 1 / eyeDisplacement : 0));

                headTilt.Push((float)(Math.Abs(leftEyeDisplacement[1] + rightEyeDisplacement[1]) / 2));
            }

            previousReading = f;

            var modelInput = new CoreMLPathwayInput
            {
                PPI       = PPI,
                Blink     = blink.Mean,
                Smile     = smile.Mean,
                Frown     = frown.Mean,
                Squint    = squint.Mean,
                GazeIn    = gazeIn.Mean,
                GazeOut   = gazeOut.Mean,
                HeadSpeed = headSpeed.Mean,
                EyeDwell  = eyeDwell.Mean,
                HeadTilt  = headTilt.Mean
            };
            IMLFeatureProvider predictionOut = model.GetPrediction(modelInput, out _);

            var targetFeatureValue = predictionOut.GetFeatureValue("target");
            var prediction         = targetFeatureValue.Int64Value;

            var classProbabilityFeatureValue = predictionOut.GetFeatureValue("classProbability");
            var probabilityx = classProbabilityFeatureValue.DictionaryValue.Values[0];
            var probabilityy = classProbabilityFeatureValue.DictionaryValue.Values[1];

            //// this is a bit hacky, but it's all relative
            //value = (probabilityy.FloatValue - probabilityx.FloatValue - 0.5f) * 2f;
            //value = (value < 0f) ? 0f : value;

            value = probabilityy.FloatValue - probabilityx.FloatValue;

            if (value < 0f)
            {
                Console.WriteLine(value + " negative !");
                value = 0f;
            }
            else
            {
                Console.WriteLine(value);
            }

            return(value);
            //return probabilityy.FloatValue;
            //return prediction;
        }