コード例 #1
0
        public TensorFloat ConvertPixelsByteToTensor(byte[] imagePixels, BitmapPixelFormat bitmapPixelFormat)
        {
            if (imagePixels == null)
            {
                throw new NullReferenceException();
            }
            var          pixelsWithAlpha = imagePixels;
            List <float> reds            = new List <float>();
            List <float> greens          = new List <float>();
            List <float> blues           = new List <float>();

            for (int i = 0; i < pixelsWithAlpha.Length; ++i)
            {
                switch (i % 4)
                {
                case 0:
                    if (bitmapPixelFormat == BitmapPixelFormat.Rgba8)
                    {
                        reds.Add((float)pixelsWithAlpha[i] / 255);
                    }
                    else if (bitmapPixelFormat == BitmapPixelFormat.Bgra8)
                    {
                        blues.Add((float)pixelsWithAlpha[i] / 255);
                    }
                    break;

                case 1:
                    greens.Add((float)pixelsWithAlpha[i] / 255);
                    break;

                case 2:
                    if (bitmapPixelFormat == BitmapPixelFormat.Rgba8)
                    {
                        blues.Add((float)pixelsWithAlpha[i] / 255);
                    }
                    else if (bitmapPixelFormat == BitmapPixelFormat.Bgra8)
                    {
                        reds.Add((float)pixelsWithAlpha[i] / 255);
                    }
                    break;
                }
            }
            List <float> sortedPixels = new List <float>();

            sortedPixels.AddRange(reds);
            sortedPixels.AddRange(greens);
            sortedPixels.AddRange(blues);

            long[] dimensions = { 1, 3, 416, 416 };
            //Tensor<float> pixelTensor = new DenseTensor<float>(dimensions);
            var inputTesnor = TensorFloat.CreateFromShapeArrayAndDataArray(dimensions, sortedPixels.ToArray());

            //sortedPixels = null;
            //sortedPixels.Clear();
            //sortedPixels.TrimExcess();
            return(inputTesnor);
        }