Update onnx.js
Browse files
onnx.js
CHANGED
@@ -128,3 +128,37 @@ function ObjectDetection() {
|
|
128 |
}
|
129 |
|
130 |
export default ObjectDetection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
}
|
129 |
|
130 |
export default ObjectDetection;
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
const preprocessImage = async (imageSrc) => {
|
136 |
+
const img = new Image();
|
137 |
+
img.src = imageSrc;
|
138 |
+
await new Promise((resolve) => (img.onload = resolve));
|
139 |
+
|
140 |
+
const canvas = document.createElement('canvas');
|
141 |
+
const context = canvas.getContext('2d');
|
142 |
+
|
143 |
+
// Resize to model input size
|
144 |
+
const modelInputWidth = 320; // Replace with your model's input width
|
145 |
+
const modelInputHeight = 320; // Replace with your model's input height
|
146 |
+
canvas.width = modelInputWidth;
|
147 |
+
canvas.height = modelInputHeight;
|
148 |
+
|
149 |
+
context.drawImage(img, 0, 0, modelInputWidth, modelInputHeight);
|
150 |
+
|
151 |
+
const imageData = context.getImageData(0, 0, modelInputWidth, modelInputHeight);
|
152 |
+
|
153 |
+
// Convert RGBA to RGB
|
154 |
+
const rgbData = new Uint8Array((imageData.data.length / 4) * 3); // 3 channels for RGB
|
155 |
+
for (let i = 0, j = 0; i < imageData.data.length; i += 4) {
|
156 |
+
rgbData[j++] = imageData.data[i]; // R
|
157 |
+
rgbData[j++] = imageData.data[i + 1]; // G
|
158 |
+
rgbData[j++] = imageData.data[i + 2]; // B
|
159 |
+
// Skip A (alpha) channel
|
160 |
+
}
|
161 |
+
|
162 |
+
// Create a tensor with shape [1, 320, 320, 3]
|
163 |
+
return new ort.Tensor('uint8', rgbData, [1, modelInputHeight, modelInputWidth, 3]);
|
164 |
+
};
|