Decode JPEG from byte[]?

I am having raw JPEG bytes (as byte[]), which I am trying to decode for inference.

I was thinking of loading them into a TString tensor and using tf.image.DecodeImage. Or is there a better way?

I’m trying to read bytes content to a Tensor. Looking at legacy samples it used to be possible to simply do:

byte[] bytes = Files.readAllBytes(Paths.get(filename));
try (Tensor<String> input = Tensors.create(bytes);

However on newer versions there doesn’t seem to be Tensor.create. Is there a newer equivalent?

Hi Sebastian,

There are different ways to achieve this in new TF Java but the first that comes to my mind is that, since the DecodeImage expects a String scalar value in input, you can simply convert your byte[] to a String and create a scalar:

try (var tensor = TString.scalarOf(new String(bytes))) { 

I’m not sure I’ve tried it myself before. If conversion to a String must be avoided, then pass the tensor data as an scalar of byte[] like this:

try (var tensor = TString.tensorOfBytes(NdArrays.scalarOfObject(bytes))) {

It is also possible to read the source image using AWT and convert it to a tensor within your app, in case you don’t want to use DecodeImage anymore (though it should work), check this example.

Hi Karl,
thank you for the response and the links. I will give it a try.