Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for blank pages #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions node-zerox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
extractPagesFromStructuredDataFile,
getTesseractScheduler,
isCompletionResponse,
isImageEmpty,
isStructuredDataFile,
prepareWorkersForImageProcessing,
runRetries,
Expand All @@ -25,9 +26,7 @@ import {
} from "./utils";
import { createModel } from "./models";
import {
CompletionResponse,
ErrorMode,
ExtractionResponse,
ModelOptions,
ModelProvider,
OperationMode,
Expand Down Expand Up @@ -107,19 +106,15 @@ export const zerox = async ({

if (extractOnly) directImageExtraction = true;

let scheduler: Tesseract.Scheduler | null = null;
// Add initial tesseract workers if we need to correct orientation
if (correctOrientation) {
scheduler = await getTesseractScheduler();
const workerCount =
maxTesseractWorkers !== -1 && maxTesseractWorkers < NUM_STARTING_WORKERS
? maxTesseractWorkers
: NUM_STARTING_WORKERS;
await addWorkersToTesseractScheduler({
numWorkers: workerCount,
scheduler,
});
}
let scheduler: Tesseract.Scheduler = await getTesseractScheduler();
const workerCount =
maxTesseractWorkers !== -1 && maxTesseractWorkers < NUM_STARTING_WORKERS
? maxTesseractWorkers
: NUM_STARTING_WORKERS;
await addWorkersToTesseractScheduler({
numWorkers: workerCount,
scheduler,
});

try {
// Ensure temp directory exists + create temp folder
Expand Down Expand Up @@ -207,13 +202,11 @@ export const zerox = async ({
imagePaths = await Promise.all(compressPromises);
}

if (correctOrientation) {
await prepareWorkersForImageProcessing({
maxTesseractWorkers,
numImages: imagePaths.length,
scheduler,
});
}
await prepareWorkersForImageProcessing({
maxTesseractWorkers,
numImages: imagePaths.length,
scheduler,
});

// Start processing OCR using LLM
const modelInstance = createModel({
Expand All @@ -239,49 +232,56 @@ export const zerox = async ({

let page: Page;
try {
let rawResponse: CompletionResponse | ExtractionResponse;
if (customModelFunction) {
rawResponse = await runRetries(
const [isEmpty, rawResponse] = await Promise.all([
isImageEmpty({ imageBuffer: correctedBuffer, scheduler }),
runRetries(
() =>
customModelFunction({
buffer: correctedBuffer,
image: imagePath,
maintainFormat,
priorPage,
}),
customModelFunction
? customModelFunction({
buffer: correctedBuffer,
image: imagePath,
maintainFormat,
priorPage,
})
: modelInstance.getCompletion(OperationMode.OCR, {
image: correctedBuffer,
maintainFormat,
priorPage,
prompt,
}),
maxRetries,
pageNumber
);
),
]);

if (isEmpty) {
page = {
content: "",
contentLength: 0,
inputTokens: 0,
outputTokens: 0,
page: pageNumber,
status: PageStatus.SUCCESS,
};
} else {
rawResponse = await runRetries(
() =>
modelInstance.getCompletion(OperationMode.OCR, {
image: correctedBuffer,
maintainFormat,
priorPage,
prompt,
}),
maxRetries,
pageNumber
const response = CompletionProcessor.process(
OperationMode.OCR,
rawResponse
);
}
const response = CompletionProcessor.process(
OperationMode.OCR,
rawResponse
);

inputTokenCount += response.inputTokens;
outputTokenCount += response.outputTokens;
inputTokenCount += response.inputTokens;
outputTokenCount += response.outputTokens;

if (isCompletionResponse(OperationMode.OCR, response)) {
priorPage = response.content;
}
if (isCompletionResponse(OperationMode.OCR, response)) {
priorPage = response.content;
}

page = {
...response,
page: pageNumber,
status: PageStatus.SUCCESS,
};
page = {
...response,
page: pageNumber,
status: PageStatus.SUCCESS,
};
}
numSuccessfulOCRRequests++;
} catch (error) {
console.error(`Failed to process image ${imagePath}:`, error);
Expand Down
30 changes: 27 additions & 3 deletions node-zerox/src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const cleanupImage = async ({
image.trim();
}

// scheduler would always be non-null if correctOrientation is true
// Scheduler would always be non-null if correctOrientation is true
// Adding this check to satisfy typescript
if (correctOrientation && scheduler) {
const optimalRotation = await determineOptimalRotation({
Expand All @@ -43,8 +43,13 @@ export const cleanupImage = async ({
return correctedBuffer;
};

// Determine the optimal image orientation based on OCR confidence
// Run Tesseract on 4 image orientations and compare the outputs
/**
* Determine the optimal image orientation based on OCR confidence
* Runs Tesseract on 4 image orientations and compares the outputs
* @param image - The image to analyze
* @param scheduler - The Tesseract scheduler for OCR operations
* @returns The degrees to rotate the image
*/
const determineOptimalRotation = async ({
image,
scheduler,
Expand Down Expand Up @@ -109,3 +114,22 @@ export const compressImage = async (
return image;
}
};

/**
* Checks if an image contains meaningful text using OCR
* @param imageBuffer - The image buffer to analyze
* @param scheduler - The Tesseract scheduler for OCR operations
* @returns True if the image is empty, otherwise false
*/
export const isImageEmpty = async ({
imageBuffer,
scheduler,
}: {
imageBuffer: Buffer;
scheduler: Tesseract.Scheduler;
}): Promise<boolean> => {
const {
data: { text },
} = await scheduler.addJob("recognize", imageBuffer);
return !text.trim();
};