bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.42k stars 1.57k forks source link

JavaCV Mat to Byfebuffer #1749

Open akshyasingh-mset opened 2 years ago

akshyasingh-mset commented 2 years ago

Hi, I am trying to run a custom model which expects input image shape to be : float32[1,3,320,320] and i am converting JavaCV mat to bytebuffer using following code

Mat floatMat = new Mat();
resized.convertTo(floatMat, opencv_core.CV_32FC3);
FloatBuffer floatBuffer = floatMat.createBuffer();
float[] floatArray = new float[floatBuffer.capacity()];
floatBuffer.get(floatArray);

ByteBuffer byteBufferInverted = ByteBuffer.allocateDirect(floatArray.length*4);
byteBufferInverted.order(ByteOrder.nativeOrder());
byteBufferInverted.asFloatBuffer().put(floatArray);
Object[] inputs = new Object[]{byteBufferInverted};
Map<Integer, Object> outputMapOcrCraft = new HashMap<>();
outputMapOcrCraft.put(0, outputOcrCraftY);
outputMapOcrCraft.put(1, outputOcrCraftFeature);
ocrCraftTflite.runForMultipleInputsOutputs(inputs, outputMapOcrCraft);

I am getting the wrong output as floatMat shape is [320,320,3]. I am stuck in how to convert [320,320,3] to [3,320,320], how to achieve the above mentioned?

saudet commented 2 years ago

OpenCV doesn't support that format, you might need to do it manually, but it's easy to do with indexers: http://bytedeco.org/news/2014/12/23/third-release/