Update onnx_time_Inferance.js
Browse files- onnx_time_Inferance.js +116 -0
onnx_time_Inferance.js
CHANGED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useRef, useState } from 'react';
|
| 2 |
+
import Webcam from 'react-webcam';
|
| 3 |
+
import * as ort from 'onnxruntime-web';
|
| 4 |
+
|
| 5 |
+
function ObjectDetection() {
|
| 6 |
+
const [averageTime, setAverageTime] = useState(null);
|
| 7 |
+
const [loading, setLoading] = useState(false);
|
| 8 |
+
const webcamRef = useRef(null);
|
| 9 |
+
|
| 10 |
+
const runBenchmark = async () => {
|
| 11 |
+
if (!webcamRef.current) return;
|
| 12 |
+
setLoading(true);
|
| 13 |
+
|
| 14 |
+
const repetitions = 50;
|
| 15 |
+
const imageCount = 10;
|
| 16 |
+
let totalInferenceTime = 0;
|
| 17 |
+
|
| 18 |
+
try {
|
| 19 |
+
// Load the ONNX model once before the loop
|
| 20 |
+
const model = await ort.InferenceSession.create('./model.onnx');
|
| 21 |
+
|
| 22 |
+
for (let rep = 0; rep < repetitions; rep++) {
|
| 23 |
+
console.log(`Repetition ${rep + 1} of ${repetitions}`);
|
| 24 |
+
|
| 25 |
+
// Capture 10 images and measure inference time
|
| 26 |
+
for (let i = 0; i < imageCount; i++) {
|
| 27 |
+
const startTime = performance.now();
|
| 28 |
+
|
| 29 |
+
// Capture image from webcam
|
| 30 |
+
const imageSrc = webcamRef.current.getScreenshot();
|
| 31 |
+
|
| 32 |
+
// Preprocess the image
|
| 33 |
+
const inputTensor = await preprocessImage(imageSrc);
|
| 34 |
+
|
| 35 |
+
// Define model input
|
| 36 |
+
const feeds = { input: inputTensor };
|
| 37 |
+
|
| 38 |
+
// Run inference
|
| 39 |
+
await model.run(feeds);
|
| 40 |
+
|
| 41 |
+
const endTime = performance.now();
|
| 42 |
+
totalInferenceTime += endTime - startTime;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const avgInferenceTime = totalInferenceTime / (repetitions * imageCount);
|
| 47 |
+
setAverageTime(avgInferenceTime);
|
| 48 |
+
} catch (error) {
|
| 49 |
+
console.error('Error running inference:', error);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
setLoading(false);
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
const preprocessImage = async (imageSrc) => {
|
| 56 |
+
const img = new Image();
|
| 57 |
+
img.src = imageSrc;
|
| 58 |
+
await new Promise((resolve) => (img.onload = resolve));
|
| 59 |
+
|
| 60 |
+
const canvas = document.createElement('canvas');
|
| 61 |
+
const context = canvas.getContext('2d');
|
| 62 |
+
|
| 63 |
+
// Resize to model input size
|
| 64 |
+
const modelInputWidth = 320; // Replace with your model's input width
|
| 65 |
+
const modelInputHeight = 320; // Replace with your model's input height
|
| 66 |
+
canvas.width = modelInputWidth;
|
| 67 |
+
canvas.height = modelInputHeight;
|
| 68 |
+
|
| 69 |
+
context.drawImage(img, 0, 0, modelInputWidth, modelInputHeight);
|
| 70 |
+
|
| 71 |
+
const imageData = context.getImageData(0, 0, modelInputWidth, modelInputHeight);
|
| 72 |
+
|
| 73 |
+
// Convert RGBA to RGB
|
| 74 |
+
const rgbData = new Uint8Array((imageData.data.length / 4) * 3); // 3 channels for RGB
|
| 75 |
+
for (let i = 0, j = 0; i < imageData.data.length; i += 4) {
|
| 76 |
+
rgbData[j++] = imageData.data[i]; // R
|
| 77 |
+
rgbData[j++] = imageData.data[i + 1]; // G
|
| 78 |
+
rgbData[j++] = imageData.data[i + 2]; // B
|
| 79 |
+
// Skip A (alpha) channel
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
// Create a tensor with shape [1, 320, 320, 3]
|
| 83 |
+
return new ort.Tensor('uint8', rgbData, [1, modelInputHeight, modelInputWidth, 3]);
|
| 84 |
+
};
|
| 85 |
+
|
| 86 |
+
return React.createElement(
|
| 87 |
+
'div',
|
| 88 |
+
null,
|
| 89 |
+
React.createElement('h1', null, 'Object Detection Benchmark'),
|
| 90 |
+
React.createElement(Webcam, {
|
| 91 |
+
audio: false,
|
| 92 |
+
ref: webcamRef,
|
| 93 |
+
screenshotFormat: 'image/jpeg',
|
| 94 |
+
width: 320,
|
| 95 |
+
height: 320,
|
| 96 |
+
}),
|
| 97 |
+
React.createElement(
|
| 98 |
+
'button',
|
| 99 |
+
{ onClick: runBenchmark, disabled: loading },
|
| 100 |
+
loading ? 'Running Benchmark...' : 'Start Benchmark'
|
| 101 |
+
),
|
| 102 |
+
React.createElement(
|
| 103 |
+
'div',
|
| 104 |
+
null,
|
| 105 |
+
averageTime !== null
|
| 106 |
+
? React.createElement(
|
| 107 |
+
'h2',
|
| 108 |
+
null,
|
| 109 |
+
`Average Inference Time: ${averageTime.toFixed(2)} ms`
|
| 110 |
+
)
|
| 111 |
+
: null
|
| 112 |
+
)
|
| 113 |
+
);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
export default ObjectDetection;
|