refactor: update ocr

This commit is contained in:
Methapon Metanipat 2024-10-02 16:50:51 +07:00
parent 1ea507583d
commit 5798c6f56b
2 changed files with 22 additions and 21 deletions

View file

@ -139,6 +139,13 @@ function mrzCleanResult(obj: Record<string, string>) {
return obj;
}
function mrzFieldExtract(expression: RegExp, line: string) {
if (expression.test(line)) {
return expression.exec(line)?.groups || {};
}
return {};
}
export function checkSum(data: string) {
if (!/[0-9A-Z<]/.test(data)) return null;
@ -159,11 +166,9 @@ export function checkSum(data: string) {
export function parseType1(mrz: MRZ) {
const result: Record<string, string> = {};
mrz.zone.forEach((line, i) => {
if (MRZ_TD_1[i].test(line)) {
Object.assign(result, MRZ_TD_1[i].exec(line)?.groups);
}
});
mrz.zone.forEach((line, i) =>
Object.assign(result, mrzFieldExtract(MRZ_TD_1[i], line)),
);
return { mrz, result: mrzCleanResult(result) };
}
@ -171,11 +176,9 @@ export function parseType1(mrz: MRZ) {
export function parseType2(mrz: MRZ) {
const result: Record<string, string> = {};
mrz.zone.forEach((line, i) => {
if (MRZ_TD_2[i].test(line)) {
Object.assign(result, MRZ_TD_2[i].exec(line)?.groups);
}
});
mrz.zone.forEach((line, i) =>
Object.assign(result, mrzFieldExtract(MRZ_TD_2[i], line)),
);
return { mrz, result: mrzCleanResult(result) };
}
@ -183,11 +186,9 @@ export function parseType2(mrz: MRZ) {
export function parseType3(mrz: MRZ) {
const result: Record<string, string> = {};
mrz.zone.forEach((line, i) => {
if (MRZ_TD_3[i].test(line)) {
Object.assign(result, MRZ_TD_3[i].exec(line)?.groups);
}
});
mrz.zone.forEach((line, i) =>
Object.assign(result, mrzFieldExtract(MRZ_TD_3[i], line)),
);
return { mrz, result: mrzCleanResult(result) };
}

View file

@ -26,13 +26,13 @@ export function parseResultMRZ(result: RecognizeResult) {
.split(/[\s\r\n]+/)
.filter((v) => /[A-Z0-9<]{30,}/.test(v));
if (zone.length === 3 && zone[0].length === 30) {
return parseMRZ({ type: 'TD1', zone });
} else if (zone.length === 2 && zone[0].length === 36) {
return parseMRZ({ type: 'TD2', zone });
} else if (zone.length === 2 && zone[0].length === 44) {
if (zone.length === 2 && zone[0].length >= 44) {
return parseMRZ({ type: 'TD3', zone });
} else if (zone.length === 2 && zone[0].length >= 36) {
return parseMRZ({ type: 'TD2', zone });
} else if (zone.length === 3 && zone[0].length >= 30) {
return parseMRZ({ type: 'TD1', zone });
} else {
return null;
return { mrz: { type: 'UNKNOWN', zone }, result: null };
}
}