Implement OCR functionality for bib numbers using Google Cloud Vision. Introduce new methods for enabling OCR, processing images, and extracting numbers from OCR text. Update JavaScript to handle cloud-based OCR processing and improve error handling. Increment version code to 4.72.798 to reflect these changes.
This commit is contained in:
@ -755,7 +755,11 @@
|
||||
if (self.ocrBusy || activeScan !== self) {
|
||||
return;
|
||||
}
|
||||
if (!self.workerReady || !self.video) {
|
||||
if (!self.video) {
|
||||
self.setStatus(scanMsg(self.panel, 'data-msg-loading', 'Initialisation (1re fois)...', 'Initializing (first time)...'), true);
|
||||
return;
|
||||
}
|
||||
if (!cfg.bibOcrCloud && !self.workerReady) {
|
||||
self.setStatus(scanMsg(self.panel, 'data-msg-loading', 'Initialisation (1re fois)...', 'Initializing (first time)...'), true);
|
||||
return;
|
||||
}
|
||||
@ -848,7 +852,10 @@
|
||||
|
||||
BibScanSession.prototype.runOcr = function () {
|
||||
var self = this;
|
||||
if (self.ocrBusy || !self.video || activeScan !== self || !self.workerReady) {
|
||||
if (self.ocrBusy || !self.video || activeScan !== self) {
|
||||
return;
|
||||
}
|
||||
if (!cfg.bibOcrCloud && !self.workerReady) {
|
||||
return;
|
||||
}
|
||||
self.ocrBusy = true;
|
||||
@ -858,6 +865,62 @@
|
||||
}
|
||||
self.setStatus(scanMsg(self.panel, 'data-msg-scanning', 'Lecture en cours...', 'Reading...'), true);
|
||||
|
||||
// MSIN-4401 — Chemin produit : photo → Google Vision (serveur). Plus fiable que Tesseract mobile.
|
||||
if (cfg.bibOcrCloud) {
|
||||
grabOcrCanvas(self).then(function (canvas) {
|
||||
if (!canvas) {
|
||||
throw new Error('capture');
|
||||
}
|
||||
var dataUrl = canvas.toDataURL('image/jpeg', 0.92);
|
||||
return fetch((cfg.domain || '') + '/ajax_inscr_gestion.php', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
|
||||
body: 'a=bib_ocr&lang=' + encodeURIComponent(cfg.lang || 'fr')
|
||||
+ '&image=' + encodeURIComponent(dataUrl)
|
||||
}).then(function (res) {
|
||||
return res.json();
|
||||
});
|
||||
}).then(function (result) {
|
||||
if (fxInscrRedirectIfSessionExpired(result)) {
|
||||
return;
|
||||
}
|
||||
if (result && result.state === 'ok' && result.number) {
|
||||
self.applyNumber(String(result.number));
|
||||
return;
|
||||
}
|
||||
if (result && result.message === 'ocr_not_configured') {
|
||||
self.setStatus(
|
||||
(document.documentElement.lang === 'en')
|
||||
? 'OCR not configured (Vision API key missing).'
|
||||
: 'OCR non configuré (clé Vision manquante).',
|
||||
false
|
||||
);
|
||||
} else {
|
||||
var base = scanMsg(self.panel, 'data-msg-invalid', 'Numéro illisible. Cadrez un seul numéro.', 'Unreadable number. Frame a single number.');
|
||||
if (result && result.seen) {
|
||||
base += ' (' + result.seen + ')';
|
||||
}
|
||||
self.setStatus(base, false);
|
||||
}
|
||||
window.setTimeout(function () {
|
||||
if (activeScan === self) {
|
||||
self.showReady();
|
||||
}
|
||||
}, 2200);
|
||||
}).catch(function () {
|
||||
self.setStatus(scanMsg(self.panel, 'data-msg-invalid', 'Numéro illisible. Cadrez un seul numéro.', 'Unreadable number. Frame a single number.'), false);
|
||||
window.setTimeout(function () {
|
||||
if (activeScan === self) {
|
||||
self.showReady();
|
||||
}
|
||||
}, 2200);
|
||||
}).then(function () {
|
||||
self.ocrBusy = false;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
grabOcrCanvas(self).then(function (canvas) {
|
||||
if (!canvas) {
|
||||
throw new Error('capture');
|
||||
@ -939,14 +1002,16 @@
|
||||
},
|
||||
audio: false
|
||||
});
|
||||
var workerPromise = getGlobalOcrWorker();
|
||||
var readyPromise = cfg.bibOcrCloud
|
||||
? cameraPromise.then(function (stream) { return [stream, null]; })
|
||||
: Promise.all([cameraPromise, getGlobalOcrWorker()]);
|
||||
|
||||
Promise.all([cameraPromise, workerPromise]).then(function (results) {
|
||||
readyPromise.then(function (results) {
|
||||
if (activeScan !== self) {
|
||||
results[0].getTracks().forEach(function (track) { track.stop(); });
|
||||
return;
|
||||
}
|
||||
self.workerReady = true;
|
||||
self.workerReady = cfg.bibOcrCloud ? true : !!results[1];
|
||||
self.stream = results[0];
|
||||
self.video = document.createElement('video');
|
||||
self.video.setAttribute('playsinline', 'true');
|
||||
@ -970,7 +1035,7 @@
|
||||
});
|
||||
};
|
||||
|
||||
if (document.querySelector('.inscr-gestion-bib-scan-toggle')) {
|
||||
if (document.querySelector('.inscr-gestion-bib-scan-toggle') && !cfg.bibOcrCloud) {
|
||||
preloadOcrWorker();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user