Example #1
0
        int Recognize(string imageFilePath)
        {
            int result = 0;

            try
            {
                var    imageFile = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + imageFilePath);
                Bitmap bitmap    = BitmapFactory.DecodeFile(imageFile.AbsolutePath);
                var    info      = bitmap.GetBitmapInfo();
                var    pixels    = new int[info.Width * info.Height];
                bitmap.GetPixels(pixels, 0, (int)info.Width, 0, 0, (int)info.Width, (int)info.Height);
                float[] pixelsT = new float[pixels.Length];
                for (int i = 0; i < pixels.Length; ++i)
                {
                    int pix = pixels[i];
                    int b   = pix & 0xff;
                    pixelsT[i] = 0xff - b;
                }

                InferenceInterface.Feed(INPUT_NAME, pixelsT, new long[] { INPUT_SIZE *INPUT_SIZE });

                string[] outputNames = new string[] { OUTPUT_NAME };
                InferenceInterface.Run(outputNames, false);

                float[] outputs = new float[_numClasses.Value];
                InferenceInterface.Fetch(OUTPUT_NAME, outputs);

                result = Array.IndexOf(outputs, outputs.Max());
            }
            catch (Exception ex)
            {
            }
            return(result);
        }
Example #2
0
        public int?Initialize()
        {
            int?numClasses = null;

            try
            {
                var graph     = InferenceInterface.Graph();
                var operation = graph?.Operation(OUTPUT_NAME);
                var output    = operation?.Output(0);
                var shape     = output?.Shape();
                _numClasses = numClasses = (int)shape?.Size(1);
            }
            catch (Exception)
            {
                return(null);
            }
            return(numClasses);
        }