public void ScanImage(byte[] bytes)
        {
            var tensor = _interpreter.GetInputTensor(0);
            var shape  = tensor.Shape();

            var width  = shape[1];
            var height = shape[2];

            var byteBuffer = ConvertImageByteArrayToByteBuffer(bytes, width, height);

            var outputLocation = new float[1][] { new float[_labels.Count] };
            var outputs        = Java.Lang.Object.FromArray(outputLocation);

            _interpreter.Run(byteBuffer, outputs);

            var classificationResult = outputs.ToArray <float[]>();
            var result = new List <ImageClassification>();

            for (var i = 0; i < _labels.Count; i++)
            {
                var label = _labels[i];
                if (classificationResult != null)
                {
                    result.Add(new ImageClassification(label, classificationResult[0][i]));
                }
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
        }
        private void HandleVNRequest(VNRequest request, NSError error)
        {
            if (error != null)
            {
                ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(new List <Classification>()));
            }

            var result          = request.GetResults <VNClassificationObservation>();
            var classifications = result.OrderByDescending(x => x.Confidence).Select(x => new Classification(x.Identifier, x.Confidence)).ToList();

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(classifications));
        }
        private void HandleVNRequest(VNRequest request, NSError error)
        {
            if (error != null)
            {
                ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(new Dictionary <string, float>()));
            }

            var result          = request.GetResults <VNClassificationObservation>();
            var classifications = result.OrderByDescending(x => x.Confidence).ToDictionary(x => x.Identifier, x => x.Confidence);

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(classifications));
        }
Exemple #4
0
        public async Task Classify(byte[] bytes)
        {
            var modelByteBuffer = LoadModel("dog-identification.tflite");
            var interpreter     = new Xamarin.TensorFlow.Lite.Interpreter(modelByteBuffer);

            var inputTensor = interpreter.GetInputTensor(0);

            var shape = inputTensor.Shape();

            var width  = shape[1];
            var height = shape[2];

            List <String> labels = new List <String>();

            var labelsStream = Application.Context.Assets.Open("labels.txt");

            using (StreamReader sr = new StreamReader(labelsStream))
            {
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    labels.Add(line);
                }
            }

            var input = GetPhotoAsByteBuffer(bytes, width, height);


            var outputLocations = new float[1][] { new float[labels.Count] };
            var outputs         = Java.Lang.Object.FromArray(outputLocations);

            interpreter.Run(input, outputs);

            var classificationResults = outputs.ToArray <float[]>();

            System.Console.WriteLine("I got results: ");
            for (int i = 0; i < classificationResults[0].Length; i++)
            {
                System.Console.WriteLine($"{labels[i]} : {classificationResults[0][i]}");
            }

            var results = new List <Classification>();

            for (var i = 0; i < labels.Count; i++)
            {
                var label = labels[i];
                results.Add(new Classification(label, classificationResults[0][i]));
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(results));
        }
Exemple #5
0
        public void Classify(byte[] bytes)
        {
            if ((interpreter == null || tensor == null) && !isInitingModel)
            {
                InitModel();
            }
            else
            {
                return;
            }
            if (isClassifying)
            {
                return;
            }
            isClassifying = true;
            new Thread(new ThreadStart(async() =>
            {
                var shape  = tensor.Shape();
                var width  = shape[1];
                var height = shape[2];

                var byteBuffer = await GetByteBufferFromPhoto(bytes, width, height);
                if (byteBuffer == null)
                {
                    ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(new List <Classification>()));
                    return;
                }
                if (List_Labels == null)
                {
                    ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(new List <Classification>()));
                    return;
                }
                var outputLocations = new float[1][] { new float[List_Labels.Count] };
                var outputs         = Java.Lang.Object.FromArray(outputLocations);
                interpreter.Run(byteBuffer, outputs);
                var classificationResult = outputs.ToArray <float[]>();
                var result = new List <Classification>();
                for (var i = 0; i < List_Labels.Count; i++)
                {
                    var label = List_Labels[i];
                    result.Add(new Classification {
                        TagName = label, Probability = classificationResult[0][i]
                    });
                }
                ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
                isClassifying = false;
            })).Start();
        }
        public void Classify(byte[] bytes)
        {
            var assets             = Application.Context.Assets;
            var inferenceInterface = new TensorFlowInferenceInterface(assets, "people-or-not-model.pb");

            var streamReader = new StreamReader(assets.Open("people-or-not-labels.txt"));

            var labels = streamReader
                         .ReadToEnd()
                         .Split('\n')
                         .Select(s => s.Trim())
                         .Where(s => !string.IsNullOrEmpty(s))
                         .ToList();

            //page 354
            var bitmap        = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
            var resizedBitMap = Bitmap.CreateScaledBitmap(bitmap, 227, 227, false).Copy(Bitmap.Config.Argb8888, false);

            var floatValues = new float[227 * 227 * 3];
            var intValues   = new int[227 * 227 * 3];

            resizedBitMap.GetPixels(intValues, 0, 227, 0, 0, 227, 227);

            for (int i = 0; i < intValues.Length; i++)
            {
                var val = intValues[i];
                floatValues[i * 3 + 0] = ((val & 0xFF) - 104);
                floatValues[i * 3 + 1] = ((val & 8) - 117);
                floatValues[i * 3 + 2] = ((val & 16) - 123);
            }

            var outputs = new float[labels.Count];

            inferenceInterface.Feed("Placeholder", floatValues, 1, 227, 227, 3);
            inferenceInterface.Run(new[] { "loss" });
            inferenceInterface.Fetch("loss", outputs);

            var results = new Dictionary <string, float>();

            for (var i = 0; i < labels.Count; i++)
            {
                var label = labels[i];
                results.Add(label, outputs[i]);
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(results));
        }
Exemple #7
0
        public async Task Classify(byte[] bytes)
        {
            var mappedByteBuffer = GetModelAsMappedByteBuffer();

            // The following line with fail with compilation error
            // Error CS0234  The type or namespace name 'GPU' does not exist in the namespace 'Xamarin.TensorFlow.Lite' (are you missing an assembly reference?)
            var gpuDelegate = new Xamarin.TensorFlow.Lite.GPU.GpuDelegate();

            Xamarin.TensorFlow.Lite.Interpreter.Options options = new Xamarin.TensorFlow.Lite.Interpreter.Options().AddDelegate(gpuDelegate);
            var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer, options);

            // If we comment 3 lines above and uncomment 1 line below everything works fine.
            //var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer);


            var tensor = interpreter.GetInputTensor(0);

            var shape = tensor.Shape();

            var width  = shape[1];
            var height = shape[2];

            var byteBuffer = GetPhotoAsByteBuffer(bytes, width, height);

            var sr     = new StreamReader(Application.Context.Assets.Open("labels.txt"));
            var labels = sr.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();

            var outputLocations = new float[1][] { new float[labels.Count] };

            var outputs = Java.Lang.Object.FromArray(outputLocations);

            interpreter.Run(byteBuffer, outputs);

            var classificationResult = outputs.ToArray <float[]>();

            var result = new List <Classification>();

            for (var i = 0; i < labels.Count; i++)
            {
                var label = labels[i];
                result.Add(new Classification(label, classificationResult[0][i]));
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
        }
        public void Classify(byte[] bytes)
        {
            var mappedByteBuffer = GetModelAsMappedByteBuffer();

            var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer);

            var tensor = interpreter.GetInputTensor(0);

            var shape = tensor.Shape();

            var width  = shape[1];
            var height = shape[2];



            var sr     = new StreamReader(Application.Context.Assets.Open("hotdog-or-not-labels.txt"));
            var labels = sr.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();

            var outputLocations = new float[1][] { new float[labels.Count] };

            var outputs = Java.Lang.Object.FromArray(outputLocations);

            var byteBuffer = GetPhotoAsByteBuffer(bytes, width, height);

            interpreter.Run(byteBuffer, outputs);

            var predictionResult = outputs.ToArray <float[]>();

            var result = new Dictionary <string, float>();

            for (var i = 0; i < labels.Count; i++)
            {
                var label = labels[i];
                result.Add(label, predictionResult[0][i]);
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
        }
        public async Task Classify(byte[] bytes)
        {
            var client = new HttpClient();

            client.Timeout = new TimeSpan(0, 0, 3);

            client.DefaultRequestHeaders.Add("Prediction-Key", "<insert-key-here>");

            string url = "https://southcentralus.api.cognitive.microsoft.com/customvision/v3.0/Prediction/80c96cf0-45ee-4887-9925-46422af8711d/classify/iterations/Iteration3/image";

            HttpResponseMessage response;

            using (var content = new ByteArrayContent(bytes))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response = await client.PostAsync(url, content);

                var json = await response.Content.ReadAsStringAsync();

                var result = JsonConvert.DeserializeObject <PredictionResult>(json);

                ClassificationCompleted.Invoke(this, new ClassificationEventArgs(result.Predictions));
            }
        }
Exemple #10
0
 public void _onHandleResults(object error, ClassificationResult[] result) => ClassificationCompleted?.Invoke(error, result);
        public async void Classification(byte[] bytes)
        {
            var assets             = Application.Context.Assets;
            var inferenceInterface = new TensorFlowInferenceInterface(assets, "model.pb");

            var inputSize = (int)inferenceInterface.GraphOperation("Placeholder").Output(0).Shape().Size(1);

            List <string> labels;

            using (var streamReader = new StreamReader(assets.Open("labels.txt")))
            {
                labels = streamReader.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s))
                         .ToList();
            }

            var bitmap = await BitmapFactory.DecodeByteArrayAsync(bytes, 0, bytes.Length);

            var resizedMap = Bitmap.CreateScaledBitmap(bitmap, inputSize, inputSize, false)
                             .Copy(Bitmap.Config.Argb8888, false);

            var floatValues = new float[inputSize * inputSize * 3];
            var intValues   = new int[inputSize * inputSize];

            resizedMap.GetPixels(intValues, 0, inputSize, 0, 0, inputSize, inputSize);

            for (var i = 0; i < intValues.Length; ++i)
            {
                var intValue = intValues[i];
                floatValues[i * 3 + 0] = (intValue & 0xFF) - 105f;
                floatValues[i * 3 + 1] = ((intValue >> 8) & 0xFF) - 117f;
                floatValues[i * 3 + 2] = ((intValue >> 16) & 0xFF) - 124f;
            }

            var operation = inferenceInterface.GraphOperation("loss");

            var outputs = new float[labels.Count];

            inferenceInterface.Feed("Placeholder", floatValues, 1, inputSize, inputSize, 3);
            inferenceInterface.Run(new[] { "loss" }, true);
            inferenceInterface.Fetch("loss", outputs);

            var result = new Dictionary <string, float>();

            for (var i = 0; i < labels.Count; i++)
            {
                var label = labels[i];
                result.Add(label, outputs[i]);
            }

            var maxConf = 0f;

            for (var i = 0; i < outputs.Length; ++i)
            {
                if (outputs[i] > maxConf)
                {
                    maxConf = outputs[i];
                }
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
        }
Exemple #12
0
        public async void Classification(byte[] bytes)
        {
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{modelFileName}"));

            petModel = await PetModelModel.CreateFromStreamAsync(file);

            try
            {
                var newBitmap = new WriteableBitmap(255, 255);

                using (var stream = new InMemoryRandomAccessStream())
                {
                    await stream.WriteAsync(bytes.AsBuffer());

                    stream.Seek(0);
                    await newBitmap.SetSourceAsync(stream);
                }

                var outputBitmap = SoftwareBitmap.CreateCopyFromBuffer(
                    newBitmap.PixelBuffer,
                    BitmapPixelFormat.Bgra8,
                    newBitmap.PixelWidth,
                    newBitmap.PixelHeight
                    );


                var frame = VideoFrame.CreateWithSoftwareBitmap(outputBitmap);


                if (frame != null)
                {
                    try
                    {
                        var inputData = new PetModelInput();
                        inputData.data = ImageFeatureValue.CreateFromVideoFrame(frame);
                        var results = await petModel.EvaluateAsync(inputData);

                        var loss = results.loss.ToList();

                        var catValue = loss.FirstOrDefault()["cat"];
                        var dogValue = loss.FirstOrDefault()["dog"];

                        var result = new Dictionary <string, float>();
                        if (catValue > dogValue)
                        {
                            result.Add("cat", catValue);
                        }
                        else
                        {
                            result.Add("dog", dogValue);
                        }

                        ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"error: {ex.Message}");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }