Skip to content

Commit

Permalink
fix(core): catch unhandled errors during ocr
Browse files Browse the repository at this point in the history
fixes #166
  • Loading branch information
marcincichocki authored Aug 26, 2021
1 parent 0927d44 commit a32c7a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/core/ocr/buffer-size-trim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ import { BreachProtocolBufferSizeFragmentResult } from './buffer-size';
export class BreachProtocolBufferSizeTrimFragment<
TImage
> extends BreachProtocolBufferSizeBase<TImage> {
override readonly fragment = this.container.processBufferSizeFragment(this.boundingBox);
override readonly fragment = this.container.processBufferSizeFragment(
this.boundingBox
);

// Ensure compatibility with current api.
async recognize(
threshold?: number
): Promise<BreachProtocolBufferSizeFragmentResult> {
const { buffer, width } = await this.container.trim(this.fragment);
const { buffer, width } = await this.trimFragment();
const bufferSize = await this.getBufferSizeFromPixels(width);

return this.getFragmentResult(null, bufferSize, buffer, null);
}

private async trimFragment() {
try {
return await this.container.trim(this.fragment);
} catch (e) {
const buffer = await this.container.toBuffer(this.fragment);

return { buffer, width: 0 };
}
}

private async getBufferSizeFromPixels(width: number) {
const { innerWidth } = this.boundingBox;

Expand Down
2 changes: 1 addition & 1 deletion src/core/ocr/buffer-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class BreachProtocolBufferSizeFragment<
// Second control group has some white pixels, threshold is too low.
start = m + 1;
}
} while (i++ < Math.log2(base) + 1);
} while (++i < Math.log2(base));

// No threshold found.
return base;
Expand Down

0 comments on commit a32c7a6

Please sign in to comment.