TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

Load in Memory Image either from Bitmap or from jpeg Byte array

Open sportbilly21 opened this issue 2 years ago • 1 comments

Hi I am trying to run a inference with an image from memory but I getting different errors. I have tried to convert the Bitmap to NDArray with NumSharp I am getting some issues with memory copy, I have tried creating a new Tensor from the byte array but the Tensor is never initialised is alwas null var image = new Tensor(img, new long[] { 1, colourjpeg.Height, colourjpeg.Width, }); I have manage to read the byte[] with np.frombuffer but then I cannot make it a 4D array. tf.image.decode_jpeg accept only Tensor contents as string type. Can you please let me know if there is a way of loading the image. The jpeg Byte array is created with the following code byte[] jpgByteArr; using (MemoryStream strm = new MemoryStream()) { img.Save(strm, ImageFormat.Jpeg); jpgByteArr = strm.ToArray(); }

If I load a jpeg from a file following your examples everything is working with the GPU or CPU package so no issues with nuget packages or CUDA missing. Many thanks

sportbilly21 avatar Apr 01 '22 08:04 sportbilly21

Have your tried something like this?

private Tensor BitmapToTensor(Bitmap bitmap)
{
    var bytes = bitmap.ToRawPixelsByteArray(pixelFormat);
    var tensor = new NDArray(bytes, new Shape(1, bitmap.Height, bitmap.Width, tensorDepth), TF_DataType.TF_UINT8);
    return tensor;
}

pantokrr avatar Sep 02 '22 13:09 pantokrr

@sportbilly21 Just simply use var img = cv2.imread("solar.jpg") reference to SharpCV.

Oceania2018 avatar Jan 31 '23 02:01 Oceania2018