I am trying to use ONNX.js to run a ONNX object detection model in browser. I know that in Tensorflow.js you have to pass only an Image Object to model and Tensorflow automatically create Tensor required by the model, but in ONNX we have to create Tensors from image and then pass it to model.
I read the offical Repository - https://github.com/Microsoft/onnxjs but i can't find any resource on how to create Tensors from image in Javascript.
If anyone knows anything please help ?
I am using following code to run Object Detection in Browser using ONNX.js
<html>
<head> </head>
<body>
<div onclick="runOD()">
Run Model
</div>
<img src="box.jpg" id="image">
<!-- Load ONNX.js -->
<script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script>
<!-- Code that consume ONNX.js -->
<script>
async function runOD() {
// Creat the session and load the pre-trained model
const session = new onnx.InferenceSession({
backendHint: 'webgl'
});
await session.loadModel("./model.onnx");
//Function to load image to Canvas and Convert it to Tensor
const preprocessedData = loadImage();
const inputTensor = new onnx.Tensor(preprocessedData, 'float32', [1, 3, 244, 244]);
// Run model with Tensor inputs and get the result.
const outputMap = await session.run([inputTensor]);
const outputData = outputMap.values().next().value.data;
console.log(`model output tensor: ${outputData.data}.`);
}
</script>
</body>
</html>