Repository navigation
fix: image may be scaled too large for tesseract - #2252
Merged
Merged
Conversation
- tesseract has a 2^31 byte limit on image data - occasionally an image may be scaled too much and larger than that size - fix limits the scaling factor so that we never scale an image larger than what tesseract can handle
qued
reviewed
Dec 13, 2023
Comment on lines
+491
to
+495
| assert [region.text for region in ocr.get_ocr_layout_tesseract(image)] == [ | ||
| "Hello", | ||
| "World", | ||
| "!", | ||
| ] |
Contributor
There was a problem hiding this comment.
What's being tested here, that Tesseract doesn't error out during this operation?
Collaborator
Author
There was a problem hiding this comment.
right; and scaled image still returns expected results
| text_height < env_config.TESSERACT_MIN_TEXT_HEIGHT | ||
| or text_height > env_config.TESSERACT_MAX_TEXT_HEIGHT | ||
| ): | ||
| max_zoom = max(0, np.round(np.sqrt(TESSERACT_MAX_SIZE / np.prod(image.size) / 32), 1)) |
Contributor
There was a problem hiding this comment.
What does 32 represent here? It seems like it should be bytes per pixel but 32 seems too big to be that. Is it 32 bits per pixel and maybe it should be 4 bytes per pixel?
Collaborator
Author
There was a problem hiding this comment.
color depth; ideally we compute that from the image property but conveniently we always pass in a 32 color image in by the PIL open as RGB mode.
…o-far-for-tesseract
qued
approved these changes
Dec 13, 2023
qued
left a comment
Contributor
There was a problem hiding this comment.
Confirmed the test fails on main and passes on this branch.
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses CORE-2965 by limiting zoom factor so that the scaled image can still be processed by tesseract.
test
A unit test is added in this PR to test a unlikely case where we'd scale an image a few thousand times and massively exceed the limit without the fix.
Unstructured reviewers can also use the document in the ticket to test.