bytedeco/javacv

JavaCV Mat to Byfebuffer

Open

#1,749 opened on Feb 11, 2022

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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?

Contributor guide