From 48ecd57782f39793cd9383507c0880f3e074e171 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 25 Oct 2025 08:49:19 +0530 Subject: [PATCH] Added logic in ocr for lens --- .../views/fields/camera-capture.blade.php | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/resources/views/fields/camera-capture.blade.php b/resources/views/fields/camera-capture.blade.php index 1fef686..3a1410a 100644 --- a/resources/views/fields/camera-capture.blade.php +++ b/resources/views/fields/camera-capture.blade.php @@ -307,6 +307,7 @@ function cameraCapture() { currentFacingMode: 'user', photoTaken: false, photo1: '', + textDetectionInterval: null, async initCamera() { @@ -318,7 +319,14 @@ function cameraCapture() { }); this.$refs.video.srcObject = this.stream; - this.startDetection(); + await video.play(); // ✅ ensure camera actually starts + + const overlay = this.$refs.overlay; + overlay.width = video.videoWidth; + overlay.height = video.videoHeight; + + setTimeout(() => this.startDetection(), 300); + //this.startDetection(); } catch (err) { console.error("Camera error:", err); alert("Cannot access camera. Enable permissions or use HTTPS."); @@ -493,19 +501,18 @@ function cameraCapture() { await this.initCamera(); }, - async detectText() { + async detectText() { const video = this.$refs.video; const overlay = this.$refs.overlay; const ctx = overlay.getContext("2d"); - // Draw video frame to temp canvas + // Temp image from video const tempCanvas = document.createElement("canvas"); tempCanvas.width = video.videoWidth; tempCanvas.height = video.videoHeight; const tempCtx = tempCanvas.getContext("2d"); tempCtx.drawImage(video, 0, 0); - // OCR detection const { data: { words } } = await Tesseract.recognize( tempCanvas.toDataURL(), "eng" @@ -513,8 +520,8 @@ function cameraCapture() { ctx.clearRect(0, 0, overlay.width, overlay.height); - ctx.strokeStyle = "red"; - ctx.lineWidth = 2; + ctx.strokeStyle = "lime"; + ctx.lineWidth = 3; words.forEach(word => { if (word.bbox) { @@ -523,12 +530,16 @@ function cameraCapture() { } }); }, - + // startDetection() { + // setInterval(() => this.detectText(), 700); + // }, startDetection() { - setInterval(() => this.detectText(), 700); + if (this.textDetectionInterval) { + clearInterval(this.textDetectionInterval); + } + this.textDetectionInterval = setInterval(() => this.detectText(), 500); }, - // Initialize camera and detection async init() { await this.initCamera();