Пример #1
0
        /// <summary>
        /// Perform Coco Ssd Mobilenet detection
        /// </summary>
        /// <param name="image">The image where we will ran the network through</param>
        /// <param name="scoreThreshold">If non-positive, will return all results. If positive, we will only return results with score larger than this value</param>
        /// <returns>The result of the detection.</returns>
        public RecognitionResult[] Recognize(NSImage image, float scoreThreshold = 0.0f)
        {
            int height = _inputTensor.Dims[1];
            int width  = _inputTensor.Dims[2];

            NativeImageIO.ReadImageToTensor <byte>(image, _inputTensor.DataPointer, height, width, 0.0f, 1.0f);

            _interpreter.Invoke();

            return(GetResults(scoreThreshold));
        }
Пример #2
0
        /// <summary>
        /// Read image files, covert the data and save it to the native pointer
        /// </summary>
        /// <typeparam name="T">The type of the data to covert the image pixel values to. e.g. "float" or "byte"</typeparam>
        /// <param name="fileNames">The name of the image files</param>
        /// <param name="inputHeight">The height of the image, must match the height requirement for the tensor</param>
        /// <param name="inputWidth">The width of the image, must match the width requirement for the tensor</param>
        /// <param name="inputMean">The mean value, it will be subtracted from the input image pixel values</param>
        /// <param name="scale">The scale, after mean is subtracted, the scale will be used to multiply the pixel values</param>
        /// <param name="flipUpSideDown">If true, the image needs to be flipped up side down</param>
        /// <param name="swapBR">If true, will flip the Blue channel with the Red. e.g. If false, the tensor's color channel order will be RGB. If true, the tensor's color channle order will be BGR </param>
        /// <param name="status">Tensorflow status</param>
        /// <returns>The tensor that contains all the image files</returns>
        private static Tensor NativeReadTensorFromImageFiles <T>(
            String[] fileNames,
            int inputHeight     = -1,
            int inputWidth      = -1,
            float inputMean     = 0.0f,
            float scale         = 1.0f,
            bool flipUpSideDown = false,
            bool swapBR         = false,
            Status status       = null) where T : struct
        {
            if (fileNames.Length == 0)
            {
                throw new ArgumentException("Intput file names do not contain any files");
            }

            String fileName = fileNames[0];

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(String.Format("File {0} do not exist.", fileName));
            }

            IntPtr dataPtr;
            int    step;
            Tensor t;

#if __ANDROID__
            using (BitmapFactory.Options options = new BitmapFactory.Options())
            {
                options.InPreferredConfig = Android.Graphics.Bitmap.Config.Argb8888; //Prefer ARGB8888 format
                using (Android.Graphics.Bitmap bmp0 = Android.Graphics.BitmapFactory.DecodeFile(fileName, options))
                {
                    if (inputHeight <= 0)
                    {
                        inputHeight = bmp0.Height;
                    }
                    if (inputWidth <= 0)
                    {
                        inputWidth = bmp0.Width;
                    }
                    if (typeof(T) == typeof(float))
                    {
                        t = new Tensor(DataType.Float,
                                       new int[] { fileNames.Length, (int)inputHeight, (int)inputWidth, 3 });
                    }
                    else if (typeof(T) == typeof(byte))
                    {
                        t = new Tensor(DataType.Uint8,
                                       new int[] { fileNames.Length, (int)inputHeight, (int)inputWidth, 3 });
                    }
                    else
                    {
                        throw new Exception(String.Format("Conversion to tensor of type {0} is not implemented",
                                                          typeof(T)));
                    }

                    dataPtr = t.DataPointer;
                    step    = NativeImageIO.ReadBitmapToTensor <T>(
                        bmp0,
                        dataPtr,
                        inputHeight,
                        inputWidth,
                        inputMean,
                        scale,
                        flipUpSideDown,
                        swapBR);
                    dataPtr = new IntPtr(dataPtr.ToInt64() + step);
                }

                for (int i = 1; i < fileNames.Length; i++)
                {
                    fileName = fileNames[i];
                    if (!File.Exists(fileName))
                    {
                        throw new FileNotFoundException(String.Format("File {0} do not exist.", fileName));
                    }

                    //Read the file using Bitmap class
                    using (Android.Graphics.Bitmap bmp = Android.Graphics.BitmapFactory.DecodeFile(fileName, options))
                    {
                        step = NativeImageIO.ReadBitmapToTensor <T>(
                            bmp,
                            dataPtr,
                            inputHeight,
                            inputWidth,
                            inputMean,
                            scale,
                            flipUpSideDown,
                            swapBR);

                        dataPtr = new IntPtr(dataPtr.ToInt64() + step);
                    }
                }
            }
#elif __MACOS__
            using (NSImage image0 = new NSImage(fileName))
            {
                if (inputHeight <= 0)
                {
                    inputHeight = (int)image0.Size.Height;
                }
                if (inputWidth <= 0)
                {
                    inputWidth = (int)image0.Size.Width;
                }
                if (typeof(T) == typeof(float))
                {
                    t = new Tensor(DataType.Float,
                                   new int[] { fileNames.Length, (int)inputHeight, (int)inputWidth, 3 });
                }
                else if (typeof(T) == typeof(byte))
                {
                    t = new Tensor(DataType.Uint8,
                                   new int[] { fileNames.Length, (int)inputHeight, (int)inputWidth, 3 });
                }
                else
                {
                    throw new Exception(String.Format("Conversion to tensor of type {0} is not implemented",
                                                      typeof(T)));
                }

                dataPtr = t.DataPointer;
                step    = NativeImageIO.ReadImageToTensor <T>(
                    image0, dataPtr, inputHeight, inputWidth, inputMean, scale,
                    flipUpSideDown, swapBR);
                dataPtr = new IntPtr(dataPtr.ToInt64() + step);
            }

            for (int i = 1; i < fileNames.Length; i++)
            {
                fileName = fileNames[i];
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(String.Format("File {0} do not exist.", fileName));
                }

                //Read the file using Bitmap class
                using (NSImage image = new NSImage(fileName))
                {
                    step = NativeImageIO.ReadImageToTensor <T>(image, dataPtr, inputHeight, inputWidth, inputMean, scale,
                                                               flipUpSideDown, swapBR);

                    dataPtr = new IntPtr(dataPtr.ToInt64() + step);
                }
            }
#else
            using (System.Drawing.Bitmap bmp0 = new System.Drawing.Bitmap(fileName))
            {
                if (inputHeight <= 0)
                {
                    inputHeight = bmp0.Height;
                }
                if (inputWidth <= 0)
                {
                    inputWidth = bmp0.Width;
                }
                if (typeof(T) == typeof(float))
                {
                    t = new Tensor(DataType.Float,
                                   new int[] { fileNames.Length, (int)inputHeight, (int)inputWidth, 3 });
                }
                else if (typeof(T) == typeof(byte))
                {
                    t = new Tensor(DataType.Uint8,
                                   new int[] { fileNames.Length, (int)inputHeight, (int)inputWidth, 3 });
                }
                else
                {
                    throw new Exception(String.Format("Conversion to tensor of type {0} is not implemented",
                                                      typeof(T)));
                }

                dataPtr = t.DataPointer;
                step    = NativeImageIO.ReadBitmapToTensor <T>(bmp0, dataPtr, inputHeight, inputWidth, inputMean, scale,
                                                               flipUpSideDown, swapBR);
                dataPtr = new IntPtr(dataPtr.ToInt64() + step);
            }

            for (int i = 1; i < fileNames.Length; i++)
            {
                fileName = fileNames[i];
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException(String.Format("File {0} do not exist.", fileName));
                }

                //Read the file using Bitmap class
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fileName))
                {
                    step = NativeImageIO.ReadBitmapToTensor <T>(bmp, dataPtr, inputHeight, inputWidth, inputMean, scale,
                                                                flipUpSideDown, swapBR);

                    dataPtr = new IntPtr(dataPtr.ToInt64() + step);
                }
            }
#endif
            return(t);
        }