changed logic in ocr

This commit is contained in:
dhanabalan
2025-10-25 16:40:22 +05:30
parent 72c94f170f
commit f8fda97181

View File

@@ -820,33 +820,58 @@ function cameraCapture() {
}, },
async detectText() { async detectText() {
if (this.textDetectionRunning) return;
this.textDetectionRunning = true;
const video = this.$refs.video; const video = this.$refs.video;
const overlay = this.$refs.overlay; const overlay = this.$refs.overlay;
const ctx = overlay.getContext('2d');
if (!video.videoWidth) return; if (!video.videoWidth) {
this.textDetectionRunning = false;
return;
}
// Clear old highlights
overlay.innerHTML = '';
// Use small canvas for faster detection
const tempCanvas = document.createElement('canvas'); const tempCanvas = document.createElement('canvas');
tempCanvas.width = video.videoWidth; const scale = 0.5;
tempCanvas.height = video.videoHeight; tempCanvas.width = video.videoWidth * scale;
const tempCtx = tempCanvas.getContext('2d'); tempCanvas.height = video.videoHeight * scale;
tempCtx.drawImage(video, 0, 0); tempCanvas.getContext('2d').drawImage(video, 0, 0, tempCanvas.width, tempCanvas.height);
try { try {
const result = await Tesseract.recognize(tempCanvas, 'eng'); const result = await this.worker.recognize(tempCanvas);
const words = result.data.words; const words = result.data.words;
ctx.clearRect(0, 0, overlay.width, overlay.height);
ctx.strokeStyle = 'lime';
ctx.lineWidth = 2;
words.forEach(w => { words.forEach(w => {
if (!w.bbox || w.confidence < 50) return; if (!w.bbox || w.confidence < 50) return;
const { x0, y0, x1, y1 } = w.bbox;
ctx.strokeRect(x0, y0, x1 - x0, y1 - y0); // Scale back to full video
const scaleX = 1 / scale;
const scaleY = 1 / scale;
const div = document.createElement('div');
div.textContent = w.text;
div.style.position = 'absolute';
div.style.left = `${w.bbox.x0 * scaleX}px`;
div.style.top = `${w.bbox.y0 * scaleY}px`;
div.style.width = `${(w.bbox.x1 - w.bbox.x0) * scaleX}px`;
div.style.height = `${(w.bbox.y1 - w.bbox.y0) * scaleY}px`;
div.style.backgroundColor = 'rgba(0,255,0,0.3)';
div.style.color = 'black';
div.style.fontSize = `${12 * scaleX}px`;
div.style.pointerEvents = 'auto';
div.style.userSelect = 'text';
div.style.overflow = 'hidden';
overlay.appendChild(div);
}); });
} catch (err) { } catch (err) {
console.error("Live OCR error:", err); console.error("OCR error:", err);
} finally {
this.textDetectionRunning = false;
} }
}, },