Skip to content

Commit

Permalink
Merge pull request #40 from robertknight/add-clear-image
Browse files Browse the repository at this point in the history
Add `clearImage` method to OCREngine/OCRClient
  • Loading branch information
robertknight authored Jun 7, 2022
2 parents 4014c1d + fa65b97 commit 36ca267
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
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");
});
});

0 comments on commit 36ca267

Please sign in to comment.