Esempio n. 1
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);
        }
        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));
        }
Esempio n. 3
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());
        }