From 477bcb3f9d3e42e82ff39ba327703fcfc72a9490 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 30 Oct 2025 14:49:11 +0530 Subject: [PATCH] change dlogic in ocr for verify --- .../views/fields/camera-capture.blade.php | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/resources/views/fields/camera-capture.blade.php b/resources/views/fields/camera-capture.blade.php index 0a4ae02..524acb3 100644 --- a/resources/views/fields/camera-capture.blade.php +++ b/resources/views/fields/camera-capture.blade.php @@ -754,6 +754,9 @@ function cameraCapture() { async initCamera() { try { + + await this.initWorker(); + if (this.stream) this.stream.getTracks().forEach(track => track.stop()); const video = this.$refs.video; @@ -778,6 +781,18 @@ function cameraCapture() { } }, + async initWorker() { + if (this.ocrWorker) return; + + // ✅ Create and load OCR worker once + this.ocrWorker = await Tesseract.createWorker('eng'); + await this.ocrWorker.loadLanguage('eng'); + await this.ocrWorker.initialize('eng'); + this.isWorkerReady = true; + + console.log("✅ OCR Worker Ready"); + }, + async switchCamera() { this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user'; await this.initCamera(); @@ -854,6 +869,11 @@ function cameraCapture() { return; } + if (!this.isWorkerReady) { + alert("OCR worker not ready yet!"); + return; + } + try { const img = new Image(); img.src = this.capturedPhoto; @@ -865,9 +885,15 @@ function cameraCapture() { const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); - const result = await Tesseract.recognize(canvas, 'eng', { - logger: m => console.log(m) - }); + // const result = await Tesseract.recognize(canvas, 'eng', { + // logger: m => console.log(m) + // }); + + // const result = await Tesseract.recognize(canvas, 'eng', { + // logger: m => console.log(m.status, m.progress) + // }); + + const result = await this.ocrWorker.recognize(img); const detectedText = result.data.text.trim(); // const matches = detectedText.match(/\d+/g) || []; @@ -964,7 +990,7 @@ function cameraCapture() { startDetection() { if (this.textDetectionInterval) clearInterval(this.textDetectionInterval); - this.textDetectionInterval = setInterval(() => this.detectText(), 1000); + this.textDetectionInterval = setInterval(() => this.detectText(), 700); }, stopDetection() {