Added verify method in ocr
This commit is contained in:
@@ -1213,6 +1213,87 @@ function cameraCapture() {
|
||||
this.isDetecting = false;
|
||||
},
|
||||
|
||||
|
||||
async verifyPhoto() {
|
||||
if (!this.capturedPhoto) {
|
||||
alert("Please capture a photo first!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.isWorkerReady) {
|
||||
alert("OCR worker not ready yet!");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const img = new Image();
|
||||
img.src = this.capturedPhoto;
|
||||
|
||||
img.onload = async () => {
|
||||
|
||||
// Draw image to a temp canvas for OCR
|
||||
this.tempCanvas.width = img.width;
|
||||
this.tempCanvas.height = img.height;
|
||||
this.tempCtx.drawImage(img, 0, 0);
|
||||
|
||||
// OCR using worker
|
||||
const result = await this.ocrWorker.recognize(this.tempCanvas);
|
||||
const detectedText = result.data.text.trim();
|
||||
console.log("Detected OCR Text:", detectedText);
|
||||
|
||||
let serials = [];
|
||||
|
||||
// 1️⃣ Look for pattern “Serial No: ABC123”
|
||||
const serialWithLabelRegex = /Serial\s*No[:\-]?\s*([A-Za-z0-9]+)/i;
|
||||
const match = detectedText.match(serialWithLabelRegex);
|
||||
|
||||
if (match && match[1]) {
|
||||
serials = [match[1].trim()];
|
||||
console.log("Found labeled serial:", serials[0]);
|
||||
} else {
|
||||
// 2️⃣ No label found → extract 4+ char alphanumeric (first 4 items)
|
||||
const generalNums = detectedText.match(/[A-Za-z0-9]{4,}/g) || [];
|
||||
|
||||
serials = generalNums.slice(0, 4);
|
||||
|
||||
if (serials.length === 0) {
|
||||
alert("No serial numbers detected in the photo!");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("Extracted possible serials:", serials);
|
||||
}
|
||||
|
||||
this.serialNumbers = serials;
|
||||
|
||||
// Save to hidden input for form submit
|
||||
this.$refs.hiddenInputSerials.value = JSON.stringify(this.serialNumbers);
|
||||
|
||||
// POST to Laravel session
|
||||
const response = await fetch('/save-serials-to-session', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
serial_numbers: this.serialNumbers,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
console.log("Session update result:", data);
|
||||
alert("✅ Serial numbers saved:\n" + JSON.stringify(this.serialNumbers, null, 2));
|
||||
};
|
||||
|
||||
} catch (err) {
|
||||
console.error("OCR verify error:", err);
|
||||
alert("OCR verify failed:\n" + (err.message || err));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
startDetection() {
|
||||
if (this.textDetectionInterval) {
|
||||
clearInterval(this.textDetectionInterval);
|
||||
|
||||
Reference in New Issue
Block a user