added logic in ocr

This commit is contained in:
dhanabalan
2025-10-25 08:36:38 +05:30
parent 1922b9d7fc
commit 4dee5620d6

View File

@@ -266,7 +266,7 @@ document.addEventListener('DOMContentLoaded', () => {
{{-- //..Another Option --}}
<div x-data="cameraCapture()" x-init="initCamera()" wire:ignore class="space-y-2">
<div x-data="cameraCapture()" x-init="init()" wire:ignore class="space-y-2">
<video
x-ref="video"
autoplay
@@ -495,45 +495,46 @@ function cameraCapture() {
},
async detectText() {
// Draw video frame to temporary canvas
const tempCanvas = document.createElement('canvas');
const video = this.$refs.video;
const overlay = this.$refs.overlay;
const ctx = overlay.getContext("2d");
// Draw video frame to temp canvas
const tempCanvas = document.createElement("canvas");
tempCanvas.width = video.videoWidth;
tempCanvas.height = video.videoHeight;
const tempCtx = tempCanvas.getContext('2d');
tempCtx.drawImage(video, 0, 0, tempCanvas.width, tempCanvas.height);
const tempCtx = tempCanvas.getContext("2d");
tempCtx.drawImage(video, 0, 0);
// Run Tesseract.js
const { data: { words } } = await Tesseract.recognize(tempCanvas.toDataURL(), 'eng');
// OCR detection
const { data: { words } } = await Tesseract.recognize(
tempCanvas.toDataURL(),
"eng"
);
// Clear previous overlay
ctx.clearRect(0, 0, overlay.width, overlay.height);
// Draw bounding boxes for each detected word
ctx.strokeStyle = 'red';
ctx.strokeStyle = "red";
ctx.lineWidth = 2;
ctx.font = '18px Arial';
ctx.fillStyle = 'red';
words.forEach(word => {
if (word.bbox) {
const { x0, y0, x1, y1 } = word.bbox;
ctx.strokeRect(x0, y0, x1 - x0, y1 - y0);
// Optional: draw recognized text
// ctx.fillText(word.text, x0, y0 - 2);
}
});
},
// Periodically detect text
async startDetection() {
setInterval(detectText, 500); // every 500ms
startDetection() {
setInterval(() => this.detectText(), 700);
},
// Initialize camera and detection
(async () => {
await startCamera();
startDetection();
})();
async init() {
await this.initCamera();
this.startDetection();
}
}
}