11// Configuration
2- const MODEL_PATH = 'document_detector.onnx' ;
2+ const QUANTIZED_MODEL_PATH = 'document_detector_quant.onnx' ;
3+ const FP32_MODEL_PATH = 'document_detector.onnx' ;
34const INPUT_SIZE = 384 ;
45const MEAN = [ 0.485 , 0.456 , 0.406 ] ;
56const STD = [ 0.229 , 0.224 , 0.225 ] ;
@@ -21,27 +22,49 @@ const canvas = document.getElementById('output-canvas');
2122const ctx = canvas . getContext ( '2d' ) ;
2223const webcamVideo = document . getElementById ( 'webcam-video' ) ;
2324const sourceImage = document . getElementById ( 'source-image' ) ;
25+ const backendSelect = document . getElementById ( 'backend-select' ) ;
2426
2527// Metrics Elements
2628const preprocessEl = document . getElementById ( 'preprocess-time' ) ;
2729const inferenceEl = document . getElementById ( 'inference-time' ) ;
2830const postprocessEl = document . getElementById ( 'postprocess-time' ) ;
2931const totalEl = document . getElementById ( 'total-time' ) ;
3032const fpsEl = document . getElementById ( 'fps-counter' ) ;
31-
3233// Initialization
33- async function init ( ) {
34+ async function init ( backend = 'wasm' ) {
3435 try {
35- updateStatus ( 'Loading ONNX Runtime...' , 'loading' ) ;
36+ webcamBtn . disabled = true ;
37+ updateStatus ( `Initializing ${ backend } ...` , 'loading' ) ;
3638
3739 // Initialize ONNX Runtime
3840 const option = {
39- executionProviders : [ 'wasm' ] ,
41+ executionProviders : [ backend ] ,
4042 graphOptimizationLevel : 'all'
4143 } ;
4244
43- updateStatus ( 'Loading Model...' , 'loading' ) ;
44- session = await ort . InferenceSession . create ( MODEL_PATH , option ) ;
45+ // Optimization for WASM
46+ if ( backend === 'wasm' ) {
47+ option . executionMode = 'parallel' ;
48+ option . intraOpNumThreads = navigator . hardwareConcurrency || 4 ;
49+ }
50+
51+ // Select model based on backend
52+ // WASM -> Quantized (INT8) for CPU speed
53+ // WebGL/WebGPU -> FP32 for GPU shader compatibility
54+ const modelPath = backend === 'wasm' ? QUANTIZED_MODEL_PATH : FP32_MODEL_PATH ;
55+
56+ updateStatus ( `Loading Model (${ backend } )...` , 'loading' ) ;
57+
58+ // Release existing session if any
59+ if ( session ) {
60+ session = null ;
61+ }
62+
63+ session = await ort . InferenceSession . create ( modelPath , option ) ;
64+
65+ // Log the execution provider
66+ console . log ( 'Inference Session created with provider:' , session . handler . backendName ) ;
67+ document . getElementById ( 'backend-type' ) . textContent = session . handler . backendName ;
4568
4669 updateStatus ( 'Ready' , 'ready' ) ;
4770 webcamBtn . disabled = false ;
@@ -59,7 +82,13 @@ async function init() {
5982 }
6083}
6184
62- // Start initialization immediately (no waiting for OpenCV)
85+ // Handle Backend Change
86+ backendSelect . addEventListener ( 'change' , ( e ) => {
87+ init ( e . target . value ) ;
88+ } ) ;
89+
90+ // Start initialization immediately
91+ init ( backendSelect . value ) ;
6392init ( ) ;
6493
6594// Helper: Update Status
0 commit comments