Skip to content

Commit 63eecdf

Browse files
authored
fix: invalid zoom lead to cv2 error (#257)
- fix a bug where zoom factor <= 0 raises an exception at runtime - now zoom factor <=0 will result in no scaling of the image Related report from user: https://unstructuredw-kbe4326.slack.com/archives/C044N0YV08G/p1697456659162509 Test: This PR adds a unit test with cases where zoom factor is 0 or -1. Without this fix the same error the user reported shows up: ``` error: (-215:Assertion failed) inv_scale_x > 0 in function 'resize' ```
1 parent dbb203a commit 63eecdf

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.6
2+
3+
* fix a bug where invalid zoom factor lead to exceptions; now invalid zoom factors results in no scaling of the image
4+
15
## 0.7.5
26

37
* Improved packaging

test_unstructured_inference/models/test_tables.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,17 @@ def test_auto_zoom(mocker):
605605
assert spy.call_count == 1
606606

607607

608+
@pytest.mark.parametrize("zoom", [1, 0.1, 5, -1, 0])
609+
def test_zoom_image(example_image, zoom):
610+
width, height = example_image.size
611+
new_image = tables.zoom_image(example_image, zoom)
612+
new_w, new_h = new_image.size
613+
if zoom <= 0:
614+
zoom = 1
615+
assert new_w == np.round(width * zoom, 0)
616+
assert new_h == np.round(height * zoom, 0)
617+
618+
608619
def test_padded_results_has_right_dimensions(table_transformer, example_image):
609620
str_class_name2idx = tables.get_class_map("structure")
610621
# a simpler mapping so we keep all structure in the returned objs below for test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.7.5" # pragma: no cover
1+
__version__ = "0.7.6" # pragma: no cover

unstructured_inference/models/tables.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,9 @@ def cells_to_html(cells):
719719
def zoom_image(image: Image, zoom: float) -> Image:
720720
"""scale an image based on the zoom factor using cv2; the scaled image is post processed by
721721
dilation then erosion to improve edge sharpness for OCR tasks"""
722+
if zoom <= 0:
723+
# no zoom but still does dilation and erosion
724+
zoom = 1
722725
new_image = cv2.resize(
723726
cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR),
724727
None,

0 commit comments

Comments
 (0)