modified logic in ocr
This commit is contained in:
@@ -386,75 +386,47 @@ function cameraCapture() {
|
|||||||
// await this.verifyOCR(dataUrl);
|
// await this.verifyOCR(dataUrl);
|
||||||
// },
|
// },
|
||||||
|
|
||||||
// async verify() {
|
|
||||||
// const filePath = this.$refs.hiddenInput.value; // e.g., "temp/capture_1760764396.jpeg"
|
|
||||||
|
|
||||||
// if (!filePath) {
|
|
||||||
// alert("No captured image found!");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// const response = await fetch('/verify-ocr', {
|
|
||||||
// method: 'POST',
|
|
||||||
// headers: {
|
|
||||||
// 'Content-Type': 'application/json',
|
|
||||||
// 'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
||||||
// },
|
|
||||||
// body: JSON.stringify({ path: filePath })
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const data = await response.json();
|
|
||||||
|
|
||||||
// console.log(data);
|
|
||||||
|
|
||||||
// if (data.success) {
|
|
||||||
// alert("OCR Result: " + data.text);
|
|
||||||
// console.error(data.text);
|
|
||||||
// } else {
|
|
||||||
// alert("OCR Failed: " + data.error);
|
|
||||||
// console.error(data.error);
|
|
||||||
// }
|
|
||||||
// } catch (err) {
|
|
||||||
// console.error(err.message);
|
|
||||||
// alert("OCR request failed: " + err.message);
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
|
|
||||||
|
|
||||||
async verify() {
|
async verify() {
|
||||||
const filePath = this.$refs.hiddenInput.value; // e.g., "temp/capture_1760764396.jpeg"
|
const filePath = this.$refs.hiddenInput.value; // e.g., "temp/capture_1760764396.jpeg"
|
||||||
|
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
alert("No captured image found!");
|
alert("No captured image found!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/verify-ocr', {
|
const response = await fetch('/verify-ocr', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ path: filePath })
|
body: JSON.stringify({ path: filePath })
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
// if (data.success) {
|
||||||
|
// alert("OCR Result: " + data.text);
|
||||||
|
// console.error(data.text);
|
||||||
|
// }
|
||||||
|
if (data.success) {
|
||||||
|
const serials = Array.isArray(data.text) ? data.text.join("\n") : data.text;
|
||||||
|
alert("OCR Result:\n" + serials);
|
||||||
|
console.log(serials);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
alert("OCR Failed: " + data.error);
|
||||||
|
console.error(data.error);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err.message);
|
||||||
|
alert("OCR request failed: " + err.message);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
if (data.success) {
|
|
||||||
// data.text is now an array of serials
|
|
||||||
console.log("Serials:", data.text);
|
|
||||||
alert("OCR Result:\n" + data.text.join("\n")); // show nicely
|
|
||||||
} else {
|
|
||||||
console.error("OCR Error:", data.error);
|
|
||||||
alert("OCR Failed: " + data.error);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error("OCR request failed:", err.message);
|
|
||||||
alert("OCR request failed: " + err.message);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async retakePhoto() {
|
async retakePhoto() {
|
||||||
this.photoTaken = false;
|
this.photoTaken = false;
|
||||||
|
|||||||
@@ -49,18 +49,14 @@ use thiagoalessio\TesseractOCR\TesseractOCR;
|
|||||||
->executable('/usr/bin/tesseract')
|
->executable('/usr/bin/tesseract')
|
||||||
->lang('eng')
|
->lang('eng')
|
||||||
->psm(6) // treats the image as a block of text
|
->psm(6) // treats the image as a block of text
|
||||||
//->config(['tessedit_char_whitelist' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'])
|
->config(['tessedit_char_whitelist' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'])
|
||||||
->run();
|
->run();
|
||||||
|
|
||||||
return response()->json(['success' => true, 'text' => $text]);
|
//return response()->json(['success' => true, 'text' => $text]);
|
||||||
// $lines = preg_split('/\r\n|\r|\n/', $text);
|
$lines = preg_split('/\r\n|\r|\n/', $text);
|
||||||
// $serials = array_filter(array_map('trim', $lines), fn($line) => preg_match('/^[A-Z0-9]+$/', $line));
|
$serials = array_filter(array_map('trim', $lines), fn($line) => preg_match('/^[A-Z0-9]+$/i', $line));
|
||||||
|
|
||||||
//return response()->json(['success' => true, 'text' => $serials]);
|
return response()->json(['success' => true, 'text' => array_values($serials)]);
|
||||||
// return response()->json([
|
|
||||||
// 'success' => true,
|
|
||||||
// 'text' => array_values($serials), // send as array
|
|
||||||
// ]);
|
|
||||||
}
|
}
|
||||||
//catch (\Exception $e) {
|
//catch (\Exception $e) {
|
||||||
// return response()->json(['success' => false, 'error' => $e->getMessage()]);
|
// return response()->json(['success' => false, 'error' => $e->getMessage()]);
|
||||||
|
|||||||
Reference in New Issue
Block a user