Skip to content

Commit

Permalink
Fixed bug with OCR comparison requiring image; added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Aug 20, 2024
1 parent a13c484 commit 90fe37e
Show file tree
Hide file tree
Showing 11 changed files with 1,566 additions and 36 deletions.
2 changes: 1 addition & 1 deletion js/containers/imageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class ImageCache {
page: n + 1, dpi, color, skipText: skipTextMode,
}).then((res) => new ImageWrapper(n, res, color ? 'color' : 'gray'));
}
throw new Error('No input mode set');
throw new Error('Attempted to render image without image input provided.');
};

/**
Expand Down
8 changes: 4 additions & 4 deletions js/export/exportDebugCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const escapeCSVField = (field) => {
};

/**
* Converts an array of objects with atomic properties (string, number, boolean) to a CSV string.
* @param {Array<Object>} data - The array of data objects.
* @returns {string} - The CSV string.
*/
* Converts an array of objects with atomic properties (string, number, boolean) to a CSV string.
* @param {Array<Object>} data - The array of data objects.
* @returns {string} - The CSV string.
*/
export const convertToCSV = (data) => {
if (data.length === 0) {
return '';
Expand Down
4 changes: 3 additions & 1 deletion js/import/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ export async function importFilesSupp(files, ocrName) {

const ocrData = await importOCRFiles(ocrFilesAll);

const scribeMode = ocrData.scribeMode;

const pageCountHOCR = ocrData.hocrRaw.length;

// If both OCR data and image data are present, confirm they have the same number of pages
Expand All @@ -478,5 +480,5 @@ export async function importFilesSupp(files, ocrName) {
if (ocrData.abbyyMode) format = 'abbyy';
if (ocrData.stextMode) format = 'stext';

convertOCRAll(ocrData.hocrRaw, false, format, ocrName);
await convertOCRAll(ocrData.hocrRaw, false, format, ocrName, scribeMode);
}
16 changes: 14 additions & 2 deletions js/recognizeConvert.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ import { replaceObjectProperties } from './utils/miscUtils.js';
*/
export const compareOCRPage = async (pageA, pageB, options) => {
const func = typeof process !== 'undefined' ? (await import('./worker/compareOCRModule.js')).compareOCRPageImp : gs.scheduler.compareOCRPageImp;
const binaryImage = await ImageCache.getBinary(pageA.n);

// Some combinations of options require the image to be provided, and some do not.
// We skip sending the image for those that do not, as in addition to helping performance,
// this is also necessary to run basic comparison scripts (e.g. benchmarking accuracy) without providing the image.
// TODO: Rework the options so this works better with types.
// At present TypeScript has no way of knowing that certain combinations of options go with each other.
const mode = options?.mode || 'stats';
const evalConflicts = options?.evalConflicts ?? true;
const supplementComp = options?.supplementComp ?? false;
const skipImage = (mode === 'stats' && !supplementComp) || (mode === 'comb' && !evalConflicts && !supplementComp);

const binaryImage = skipImage ? null : await ImageCache.getBinary(pageA.n);

const pageMetricsObj = pageMetricsArr[pageA.n];
return func({
pageA, pageB, binaryImage, pageMetricsObj, options,
Expand All @@ -51,7 +63,7 @@ export const evalOCRPage = async (params) => {
* Compare two sets of OCR data.
* @param {Array<OcrPage>} ocrA
* @param {Array<OcrPage>} ocrB
* @param {Parameters<import('./worker/compareOCRModule.js').compareOCRPageImp>[0]['options']} options
* @param {Parameters<import('./worker/compareOCRModule.js').compareOCRPageImp>[0]['options']} [options]
*/
export const compareOCR = async (ocrA, ocrB, options) => {
/** @type {Parameters<typeof compareOCRPage>[2]} */
Expand Down
17 changes: 13 additions & 4 deletions js/worker/compareOCRModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,19 @@ async function penalizeWord(wordObjs) {
export async function compareOCRPageImp({
pageA, pageB, binaryImage, pageMetricsObj, options = {},
}) {
const binaryImageBit = binaryImage.imageBitmap || await getImageBitmap(binaryImage.src);

const imageUpscaled = binaryImage.upscaled;
const imageRotated = binaryImage.rotated;
// The `binaryImage` argument is not sent for certain operations, which do not require it.
// For example, running a basic comparison between a page and the ground truth does not require having the image.
// The types do not currently reflect this, so this should be reworked at some point.
/** @type {?ImageBitmap} */
let binaryImageBit = null;
let imageUpscaled = false;
let imageRotated = false;

if (binaryImage) {
binaryImageBit = binaryImage.imageBitmap || await getImageBitmap(binaryImage.src);
imageUpscaled = binaryImage.upscaled;
imageRotated = binaryImage.rotated;
}

const mode = options?.mode === undefined ? 'stats' : options?.mode;
const editConf = options?.editConf === undefined ? false : options?.editConf;
Expand Down
46 changes: 24 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"canvas": "^2.11.2",
"commander": "^11.1.0",
"puppeteer": "^22.13.0",
"tesseract.js": "scribeocr/tesseract.js#2065fd6",
"@scribe.js/tesseract.js": "^5.0.4",
"web-worker": "~1.2.0"
}
}
4 changes: 3 additions & 1 deletion scribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ImageCache } from './js/containers/imageContainer.js';
import coords from './js/coordinates.js';
import { drawDebugImages } from './js/debug.js';
import { download, exportData } from './js/export/export.js';
import { writeDebugCsv } from './js/export/exportDebugCsv.js';
import { writeDebugCsv, convertToCSV } from './js/export/exportDebugCsv.js';
import { extractSingleTableContent } from './js/export/exportWriteTabular.js';
import { loadBuiltInFontsRaw, enableFontOpt } from './js/fontContainerMain.js';
import { gs } from './js/generalWorkerMain.js';
Expand Down Expand Up @@ -131,6 +131,8 @@ class utils {
// Misc utils
static calcBoxOverlap = calcBoxOverlap;

static convertToCSV = convertToCSV;

static replaceSmartQuotes = replaceSmartQuotes;

static getRandomAlphanum = getRandomAlphanum;
Expand Down
Loading

0 comments on commit 90fe37e

Please sign in to comment.