chnage dlogic in ocr

This commit is contained in:
dhanabalan
2025-10-25 09:45:30 +05:30
parent 0d9df371ee
commit 67b40a5350

View File

@@ -568,17 +568,26 @@ function cameraCapture() {
{{-- //.. --}} {{-- //.. --}}
<div x-data="cameraCapture()" x-init="initCamera()" class="relative w-80 h-auto"> <div x-data="cameraCapture()" x-init="initCamera()" class="relative w-80 h-auto">
<!-- Video Stream -->
<video x-ref="video" autoplay playsinline class="border rounded w-80 h-auto"></video> <video x-ref="video" autoplay playsinline class="border rounded w-80 h-auto"></video>
<!-- Overlay for OCR highlights -->
<canvas x-ref="overlay" class="absolute top-0 left-0 w-80 h-auto pointer-events-none"></canvas> <canvas x-ref="overlay" class="absolute top-0 left-0 w-80 h-auto pointer-events-none"></canvas>
<!-- Switch Camera Button -->
<div class="mt-2">
<button @click="switchCamera" class="px-4 py-2 bg-blue-600 text-white rounded">Switch Camera</button>
</div>
</div> </div>
<!-- Tesseract.js CDN -->
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@4.1.3/dist/tesseract.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/tesseract.js@4.1.3/dist/tesseract.min.js"></script>
<script> <script>
function cameraCapture() { function cameraCapture() {
return { return {
stream: null, stream: null,
currentFacingMode: 'user', currentFacingMode: 'user', // 'user' = front, 'environment' = rear
worker: null, worker: null,
textDetectionInterval: null, textDetectionInterval: null,
@@ -593,7 +602,6 @@ function cameraCapture() {
}); });
video.srcObject = this.stream; video.srcObject = this.stream;
await new Promise(resolve => video.onloadedmetadata = resolve); await new Promise(resolve => video.onloadedmetadata = resolve);
await video.play(); await video.play();
@@ -602,7 +610,7 @@ function cameraCapture() {
overlay.width = video.videoWidth; overlay.width = video.videoWidth;
overlay.height = video.videoHeight; overlay.height = video.videoHeight;
// Init Tesseract worker once // Initialize Tesseract Worker once
if (!this.worker) { if (!this.worker) {
this.worker = Tesseract.createWorker({ logger: m => console.log(m) }); this.worker = Tesseract.createWorker({ logger: m => console.log(m) });
await this.worker.load(); await this.worker.load();
@@ -618,9 +626,19 @@ function cameraCapture() {
}, },
async switchCamera() { async switchCamera() {
// Toggle facing mode
this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user'; this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user';
// Stop previous detection interval
if (this.textDetectionInterval) clearInterval(this.textDetectionInterval); if (this.textDetectionInterval) clearInterval(this.textDetectionInterval);
await new Promise(r => setTimeout(r, 300)); // small delay
// Stop all tracks
if (this.stream) this.stream.getTracks().forEach(track => track.stop());
// Small delay to avoid browser issues
await new Promise(r => setTimeout(r, 300));
// Restart camera
await this.initCamera(); await this.initCamera();
}, },
@@ -631,6 +649,7 @@ function cameraCapture() {
if (!video.videoWidth) return; if (!video.videoWidth) return;
// Draw current frame on temp canvas
const tempCanvas = document.createElement('canvas'); const tempCanvas = document.createElement('canvas');
tempCanvas.width = video.videoWidth; tempCanvas.width = video.videoWidth;
tempCanvas.height = video.videoHeight; tempCanvas.height = video.videoHeight;
@@ -640,7 +659,9 @@ function cameraCapture() {
try { try {
const { data: { words } } = await this.worker.recognize(tempCanvas); const { data: { words } } = await this.worker.recognize(tempCanvas);
// Clear overlay
ctx.clearRect(0, 0, overlay.width, overlay.height); ctx.clearRect(0, 0, overlay.width, overlay.height);
ctx.strokeStyle = 'lime'; ctx.strokeStyle = 'lime';
ctx.lineWidth = 2; ctx.lineWidth = 2;
@@ -665,3 +686,5 @@ function cameraCapture() {