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

Add clearImage method to OCREngine/OCRClient #40

Merged
merged 1 commit into from
Jun 7, 2022
Merged
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
12 changes: 8 additions & 4 deletions src/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ class OCREngine {
return {};
}

void ClearImage() { tesseract_->Clear(); }
void ClearImage() {
tesseract_->Clear();
layout_analysis_done_ = false;
ocr_done_ = false;
}

std::vector<TextRect> GetBoundingBoxes(TextUnit unit) {
if (!layout_analysis_done_) {
Expand Down Expand Up @@ -304,12 +308,12 @@ EMSCRIPTEN_BINDINGS(ocrlib) {
class_<OCREngine>("OCREngine")
.constructor<>()
.function("clearImage", &OCREngine::ClearImage)
.function("loadModel", &OCREngine::LoadModel)
.function("loadImage", &OCREngine::LoadImage)
.function("getBoundingBoxes", &OCREngine::GetBoundingBoxes)
.function("getOrientation", &OCREngine::GetOrientation)
.function("getText", &OCREngine::GetText)
.function("getTextBoxes", &OCREngine::GetTextBoxes)
.function("getText", &OCREngine::GetText);
.function("loadImage", &OCREngine::LoadImage)
.function("loadModel", &OCREngine::LoadModel);

value_object<OCRResult>("OCRResult").field("error", &OCRResult::error);

Expand Down
15 changes: 15 additions & 0 deletions src/ocr-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ export class OCRClient {
return engine.loadImage(image);
}

/**
* Clear the current image and text recognition results.
*
* This will clear the loaded image data internally, but keep the text
* recognition model loaded.
*
* At present there is no way to shrink WebAssembly memory, so this will not
* return the memory used by the image to the OS/browser. To release memory,
* the web worker needs to be shut down via {@link destroy}.
*/
async clearImage(): Promise<void> {
const engine = await this._ocrEngine;
return engine.clearImage();
}

/**
* Perform layout analysis on the current image, if not already done, and
* return bounding boxes for a given unit of text.
Expand Down
15 changes: 15 additions & 0 deletions src/ocr-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ export class OCREngine {
this._imageLoaded = true;
}

/**
* Clear the current image and text recognition results.
*
* This will clear the loaded image data internally, but keep the text
* recognition model loaded.
*
* At present there is no way to shrink WebAssembly memory, so this will not
* return the memory used by the image to the OS/browser. To release memory,
* the `OCREngine` instance needs to be destroyed via {@link destroy}.
*/
clearImage() {
this._engine.clearImage();
this._imageLoaded = false;
}

/**
* Perform layout analysis on the current image, if not already done, and
* return bounding boxes for a given unit of text.
Expand Down
18 changes: 18 additions & 0 deletions test/ocr-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,22 @@ describe("OCRClient", () => {
assert.equal(orient.rotation, 0);
assert.equal(orient.confidence, 1.0);
});

it("clears the image", async () => {
const imageData = await loadImage(resolve("./small-test-page.jpg"));
await ocr.loadImage(imageData);
await ocr.getBoundingBoxes("word");

await ocr.clearImage();

let error;
try {
await ocr.getBoundingBoxes("word");
} catch (e) {
error = e;
}

assert.instanceOf(error, Error);
assert.equal(error.message, "No image loaded");
});
});
11 changes: 11 additions & 0 deletions test/ocr-engine-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,15 @@ describe("OCREngine", () => {
assert.equal(estimatedOrient.confidence, 1);
}
});

it("clears the image", async () => {
ocr.loadImage(emptyImage(100, 100));
ocr.getBoundingBoxes("word");

ocr.clearImage();

assert.throws(() => {
ocr.getBoundingBoxes("word");
}, "No image loaded");
});
});