IMLFeatureProvider CreateInput(UIImage image)
        {
            var pixelBuffer = image.Resize(new CGSize(227, 227)).ToPixelBuffer();

            var imageValue = MLFeatureValue.FromPixelBuffer(pixelBuffer);

            var inputs = new NSDictionary <NSString, NSObject> (new NSString("image"), imageValue);

            return(new MLDictionaryFeatureProvider(inputs, out var error));
        }
Ejemplo n.º 2
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "audioSamples":
                return(MLFeatureValue.Create(AudioSamples));

            default:
                return(null);
            }
        }
Ejemplo n.º 3
0
        private List <Prediction> Classify(MLModel model, UIImage source)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var scaled      = source.Scale(new CGSize(227, 227));
            var pixelBuffer = ToCVPixelBuffer(scaled);
            var imageValue  = MLFeatureValue.Create(pixelBuffer);

            var inputs = new NSDictionary <NSString, NSObject>(new NSString("data"), imageValue);

            NSError error, error2;
            var     inputFp = new MLDictionaryFeatureProvider(inputs, out error);

            if (error != null)
            {
                System.Diagnostics.Debug.WriteLine(error);
                throw new InvalidOperationException(error.Description);
            }
            var outFeatures = model.GetPrediction(inputFp, out error2);

            if (error2 != null)
            {
                System.Diagnostics.Debug.WriteLine(error2);
                throw new InvalidOperationException(error2.Description);
            }

            var predictionsDictionary = outFeatures.GetFeatureValue("loss").DictionaryValue;
            var byProbability         = new List <Tuple <double, string> >();

            foreach (var key in predictionsDictionary.Keys)
            {
                var description = (string)(NSString)key;
                var prob        = (double)predictionsDictionary[key];
                byProbability.Add(new Tuple <double, string>(prob, description));
            }

            byProbability.Sort((t1, t2) => t1.Item1.CompareTo(t2.Item1) * -1);

            var result = byProbability
                         .Select(p => new Prediction {
                Label = p.Item2, Probability = p.Item1
            })
                         .ToList();

            return(result);
        }
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "image":
                return(MLFeatureValue.Create(Image));

            default:
                return(null);
            }
        }
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "output1":
                return(MLFeatureValue.Create(Output1));

            default:
                return(null);
            }
        }
Ejemplo n.º 6
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "data":
                return(MLFeatureValue.Create(Data));

            default:
                return(null);
            }
        }
 public MLFeatureValue GetFeatureValue(string featureName)
 {
     if (inputFeatureName.Equals(featureName))
     {
         return(MLFeatureValue.Create(this.ImagePixelData));
     }
     else
     {
         return(MLFeatureValue.Create(0));
     }
 }
Ejemplo n.º 8
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "confidence":
                return(MLFeatureValue.Create(Confidence));

            case "coordinates":
                return(MLFeatureValue.Create(Coordinates));

            default:
                return(null);
            }
        }
Ejemplo n.º 9
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "target":
                return(MLFeatureValue.Create(Target));

            case "classProbability":
                return(MLFeatureValue.Create(ClassProbability));

            default:
                return(null);
            }
        }
Ejemplo n.º 10
0
        private IEnumerable <Recognition> Recognize(Stream source)
        {
            var byProbability = new List <Recognition>();
            var image         = UIImage.LoadFromData(NSData.FromStream(source));

            if (model == null)
            {
                //ErrorOccurred(this, new EventArgsT<string>(error.ToString()));
                return(byProbability);
            }

            var pixelBuffer = image.Scale(ImageSize).ToCVPixelBuffer();
            var imageValue  = MLFeatureValue.Create(pixelBuffer);

            var inputs = new NSDictionary <NSString, NSObject>(new NSString(INPUT_NAME), imageValue);

            var inputFeatureProvider = new MLDictionaryFeatureProvider(inputs, out var error1);

            if (error1 != null)
            {
                //ErrorOccurred(this, new EventArgs<string>(error1.ToString()));
                return(byProbability);
            }

            var outFeatures = model.GetPrediction(inputFeatureProvider, out var error2);

            if (error2 != null)
            {
                //ErrorOccurred(this, new EventArgs<string>(error2.ToString()));
                return(byProbability);
            }

            var predictionsDictionary = outFeatures.GetFeatureValue(OUTPUT_NAME).DictionaryValue;

            foreach (var key in predictionsDictionary.Keys)
            {
                var description = (string)(NSString)key;
                var prob        = (double)predictionsDictionary[key];
                byProbability.Add(new Recognition
                {
                    Tag         = description,
                    Probability = prob
                });
            }

            //Sort descending
            byProbability.Sort((t1, t2) => (t1.Probability.CompareTo(t2.Probability)) * -1);

            return(byProbability);
        }
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "solarPanels":
                return(MLFeatureValue.Create(SolarPanels));

            case "greenhouses":
                return(MLFeatureValue.Create(Greenhouses));

            case "size":
                return(MLFeatureValue.Create(Size));

            default:
                return(MLFeatureValue.Create(0));
            }
        }
Ejemplo n.º 12
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "image":
                return(MLFeatureValue.Create(Image));

            case "iouThreshold":
                return(MLFeatureValue.Create(IouThreshold));

            case "confidenceThreshold":
                return(MLFeatureValue.Create(ConfidenceThreshold));

            default:
                return(null);
            }
        }
Ejemplo n.º 13
0
        internal void Classify(UIImage source)
        {
            var model = models[currentModel];

            var pixelBuffer = source.Scale(sizeFor[model]).ToCVPixelBuffer();
            var imageValue  = MLFeatureValue.Create(pixelBuffer);

            var inputs = new NSDictionary <NSString, NSObject>(new NSString("image"), imageValue);

            NSError error, error2;
            var     inputFp = new MLDictionaryFeatureProvider(inputs, out error);

            if (error != null)
            {
                ErrorOccurred(this, new EventArgsT <string>(error.ToString()));
                return;
            }
            var outFeatures = model.GetPrediction(inputFp, out error2);

            if (error2 != null)
            {
                ErrorOccurred(this, new EventArgsT <string>(error2.ToString()));
                return;
            }

            var predictionsDictionary = outFeatures.GetFeatureValue("classLabelProbs").DictionaryValue;
            var byProbability         = new List <Tuple <double, string> >();

            foreach (var key in predictionsDictionary.Keys)
            {
                var description = (string)(NSString)key;
                var prob        = (double)predictionsDictionary[key];
                byProbability.Add(new Tuple <double, string>(prob, description));
            }
            //Sort descending
            byProbability.Sort((t1, t2) => t1.Item1.CompareTo(t2.Item1) * -1);

            var prediction = new ImageDescriptionPrediction();

            prediction.ModelName   = currentModel;
            prediction.predictions = byProbability;

            PredictionsUpdated(this, new EventArgsT <ImageDescriptionPrediction>(prediction));
        }
        public async Task <IEnumerable <Recognition> > RecognizeAsync(Stream source, params string[] parameters)
        {
            var results = new List <Recognition>();
            var image   = await UIImage.LoadFromData(NSData.FromStream(source)).ResizeImageAsync(INPUT_WIDTH, INPUT_HEIGHT);

            await Task.Run(() =>
            {
                var pixelBuffer = image.Scale(ImageSize).ToCVPixelBuffer();
                var imageValue  = MLFeatureValue.Create(pixelBuffer);

                var inputs = new NSDictionary <NSString, NSObject>(new NSString(INPUT_NAME), imageValue);

                var inputFeatureProvider = new MLDictionaryFeatureProvider(inputs, out var error1);
                if (error1 != null)
                {
                    throw new ClassifierException(error1.ToString());
                }

                var outFeatures = model.GetPrediction(inputFeatureProvider, out var error2);
                if (error2 != null)
                {
                    throw new ClassifierException(error2.ToString());
                }

                var predictionsDictionary = outFeatures.GetFeatureValue(OUTPUT_NAME).DictionaryValue;
                foreach (var key in predictionsDictionary.Keys)
                {
                    var description = (string)(NSString)key;
                    var probability = (double)predictionsDictionary[key];
                    results.Add(new Recognition
                    {
                        Tag         = description,
                        Probability = probability
                    });
                }

                // Sort descending.
                results.Sort((t1, t2) => (t1.Probability.CompareTo(t2.Probability)) * -1);
            });

            return(results);
        }
Ejemplo n.º 15
0
        public ImageClassificationResult RecognizeImage(UIImage imageSource)
        {
            var pixelBuffer = imageSource.Scale(new CGSize(InputSize, InputSize)).ToCVPixelBuffer();
            var imageValue  = MLFeatureValue.Create(pixelBuffer);

            var inputs = new NSDictionary <NSString, NSObject>(new NSString(InputName), imageValue);

            NSError error, error2;
            var     inputFp = new MLDictionaryFeatureProvider(inputs, out error);

            if (error != null)
            {
                Console.WriteLine("RecognizeImage Error 1: {0}", error);
                return(null);
            }
            var outFeatures = _imageClassifierModel.GetPrediction(inputFp, out error2);

            if (error2 != null)
            {
                Console.WriteLine("RecognizeImage Error 2: {0}", error2);
                return(null);
            }

            var predictionsDictionary = outFeatures.GetFeatureValue(OutputName).DictionaryValue;

            var outputs = new List <Tuple <double, string> >();

            foreach (var key in predictionsDictionary.Keys)
            {
                var description = (string)(NSString)key;
                var prob        = (double)predictionsDictionary[key];
                outputs.Add(new Tuple <double, string>(prob, description));
            }

            var results = outputs
                          .Select((t1, t2) => new ImageClassificationResult(t1.Item2, t1.Item1))
                          .ToList();

            return(results.OrderByDescending(t => t.Probability).First());
        }
Ejemplo n.º 16
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            MLFeatureValue value;
            NSError        err;

            switch (featureName)
            {
            case "classLabelProbs":
                value = MLFeatureValue.Create(ClassLabelProbs, out err);
                if (err != null)
                {
                    err.Dispose();
                }
                return(value);

            case "classLabel":
                return(MLFeatureValue.Create(ClassLabel));

            default:
                return(null);
            }
        }
Ejemplo n.º 17
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "ppi":
                return(MLFeatureValue.Create(PPI));

            case "eye_blink":
                return(MLFeatureValue.Create(Blink));

            case "eye_squint":
                return(MLFeatureValue.Create(Squint));

            case "eye_gaze_inward":
                return(MLFeatureValue.Create(GazeIn));

            case "eye_gaze_outward":
                return(MLFeatureValue.Create(GazeOut));

            case "smile":
                return(MLFeatureValue.Create(Smile));

            case "frown":
                return(MLFeatureValue.Create(Frown));

            case "head_speed":
                return(MLFeatureValue.Create(HeadSpeed));

            case "eye_dwelling":
                return(MLFeatureValue.Create(EyeDwell));

            case "head_tilt":
                return(MLFeatureValue.Create(HeadTilt));

            default:
                return(MLFeatureValue.Create(0));    // default value
            }
        }
Ejemplo n.º 18
0
 public MLFeatureValue GetFeatureValue(string featureName) => _inputFeatureName.Equals(featureName)
         ? MLFeatureValue.Create(ImagePixelData)
         : MLFeatureValue.Create(0);
Ejemplo n.º 19
0
        public MLFeatureValue GetFeatureValue(string featureName)
        {
            switch (featureName)
            {
            case "BrowDownLeft": return(MLFeatureValue.Create(BrowDownLeft));

            case "BrowDownRight": return(MLFeatureValue.Create(BrowDownRight));

            case "BrowInnerUp": return(MLFeatureValue.Create(BrowInnerUp));

            case "BrowOuterUpLeft": return(MLFeatureValue.Create(BrowOuterUpLeft));

            case "BrowOuterUpRight": return(MLFeatureValue.Create(BrowOuterUpRight));

            case "CheekPuff": return(MLFeatureValue.Create(CheekPuff));

            case "CheekSquintLeft": return(MLFeatureValue.Create(CheekSquintLeft));

            case "CheekSquintRight": return(MLFeatureValue.Create(CheekSquintRight));

            case "EyeBlinkLeft": return(MLFeatureValue.Create(EyeBlinkLeft));

            case "EyeBlinkRight": return(MLFeatureValue.Create(EyeBlinkRight));

            case "EyeLookDownLeft": return(MLFeatureValue.Create(EyeLookDownLeft));

            case "EyeLookDownRight": return(MLFeatureValue.Create(EyeLookDownRight));

            case "EyeLookInLeft": return(MLFeatureValue.Create(EyeLookInLeft));

            case "EyeLookInRight": return(MLFeatureValue.Create(EyeLookInRight));

            case "EyeLookOutLeft": return(MLFeatureValue.Create(EyeLookOutRight));

            case "EyeLookOutRight": return(MLFeatureValue.Create(EyeLookOutRight));

            case "EyeLookUpLeft": return(MLFeatureValue.Create(EyeLookUpLeft));

            case "EyeLookUpRight": return(MLFeatureValue.Create(EyeLookUpRight));

            case "EyeSquintLeft": return(MLFeatureValue.Create(EyeSquintLeft));

            case "EyeSquintRight": return(MLFeatureValue.Create(EyeSquintRight));

            case "EyeWideLeft": return(MLFeatureValue.Create(EyeWideLeft));

            case "EyeWideRight": return(MLFeatureValue.Create(EyeWideRight));

            case "JawForward": return(MLFeatureValue.Create(JawForward));

            case "JawLeft": return(MLFeatureValue.Create(JawLeft));

            case "JawOpen": return(MLFeatureValue.Create(JawOpen));

            case "JawRight": return(MLFeatureValue.Create(JawRight));

            case "MouthClose": return(MLFeatureValue.Create(MouthClose));

            case "MouthDimpleLeft": return(MLFeatureValue.Create(MouthDimpleLeft));

            case "MouthDimpleRight": return(MLFeatureValue.Create(MouthDimpleRight));

            case "MouthFrownLeft": return(MLFeatureValue.Create(MouthFrownLeft));

            case "MouthFrownRight": return(MLFeatureValue.Create(MouthFrownRight));

            case "MouthFunnel": return(MLFeatureValue.Create(MouthFunnel));

            case "MouthLeft": return(MLFeatureValue.Create(MouthLeft));

            case "MouthLowerDownLeft": return(MLFeatureValue.Create(MouthLowerDownLeft));

            case "MouthLowerDownRight": return(MLFeatureValue.Create(MouthLowerDownRight));

            case "MouthPressLeft": return(MLFeatureValue.Create(MouthPressLeft));

            case "MouthPressRight": return(MLFeatureValue.Create(MouthPressRight));

            case "MouthPucker": return(MLFeatureValue.Create(MouthPucker));

            case "MouthRight": return(MLFeatureValue.Create(MouthRight));

            case "MouthRollLower": return(MLFeatureValue.Create(MouthRollLower));

            case "MouthRollUpper": return(MLFeatureValue.Create(MouthRollUpper));

            case "MouthShrugLower": return(MLFeatureValue.Create(MouthShrugLower));

            case "MouthShrugUpper": return(MLFeatureValue.Create(MouthShrugUpper));

            case "MouthSmileLeft": return(MLFeatureValue.Create(MouthSmileLeft));

            case "MouthSmileRight": return(MLFeatureValue.Create(MouthSmileRight));

            case "MouthStretchLeft": return(MLFeatureValue.Create(MouthStretchLeft));

            case "MouthStretchRight": return(MLFeatureValue.Create(MouthStretchRight));

            case "MouthUpperUpLeft": return(MLFeatureValue.Create(MouthUpperUpLeft));

            case "MouthUpperUpRight": return(MLFeatureValue.Create(MouthUpperUpRight));

            case "NoseSneerLeft": return(MLFeatureValue.Create(NoseSneerLeft));

            case "NoseSneerRight": return(MLFeatureValue.Create(NoseSneerRight));

            case "M11": return(MLFeatureValue.Create(M11));

            case "M12": return(MLFeatureValue.Create(M12));

            case "M13": return(MLFeatureValue.Create(M13));

            case "M14": return(MLFeatureValue.Create(M14));

            case "M21": return(MLFeatureValue.Create(M21));

            case "M22": return(MLFeatureValue.Create(M22));

            case "M23": return(MLFeatureValue.Create(M23));

            case "M24": return(MLFeatureValue.Create(M24));

            case "M31": return(MLFeatureValue.Create(M31));

            case "M32": return(MLFeatureValue.Create(M32));

            case "M33": return(MLFeatureValue.Create(M33));

            case "M34": return(MLFeatureValue.Create(M34));

            case "qW": return(MLFeatureValue.Create(qW));

            case "qX": return(MLFeatureValue.Create(qX));

            case "qY": return(MLFeatureValue.Create(qY));

            case "qZ": return(MLFeatureValue.Create(qZ));

            default:
                return(MLFeatureValue.Create(""));
            }
        }