-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add get_words_in_area function for OCR
- Loading branch information
1 parent
f459633
commit 2ea5e27
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Optional | ||
|
||
import pytest | ||
|
||
from openfoodfacts.ocr import OCRResult | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"ocr_url, bounding_box, expected_text", | ||
[ | ||
( | ||
# It corresponds to this OCR crop: https://robotoff.openfoodfacts.org/api/v1/images/crop?image_url=https://images.openfoodfacts.org/images/products/089/000/000/1202/1.jpg&y_min=0.08416666666666667&x_min=0.30077691453940064&y_max=0.09583333333333334&x_max=0.37735849056603776 | ||
"https://raw.githubusercontent.com/openfoodfacts/test-data/main/openfoodfacts-python/tests/unit/0890000001202_1.json", | ||
[101, 271, 115, 340], | ||
"Materne", | ||
), | ||
( | ||
# same, but the bounding box is distinct from the logo area | ||
"https://raw.githubusercontent.com/openfoodfacts/test-data/main/openfoodfacts-python/tests/unit/0890000001202_1.json", | ||
[120, 271, 134, 340], | ||
None, | ||
), | ||
( | ||
# same, but the bounding box is distinct from the logo area | ||
"https://raw.githubusercontent.com/openfoodfacts/test-data/main/openfoodfacts-python/tests/unit/0890000001202_1.json", | ||
[120, 271, 134, 340], | ||
None, | ||
), | ||
( | ||
# [0.2808293402194977,0.37121888995170593,0.35544055700302124,0.49409016966819763] | ||
# /540/091/030/1160/1.jpg | ||
"https://raw.githubusercontent.com/openfoodfacts/test-data/main/openfoodfacts-python/tests/unit/5400910301160_1.json", | ||
[337, 327, 427, 436], | ||
"NUTRIDIA", | ||
), | ||
], | ||
) | ||
def test_get_words_in_area( | ||
ocr_url: str, bounding_box: list[int, int, int, int], expected_text: Optional[str] | ||
): | ||
ocr_result = OCRResult.from_url(ocr_url) | ||
words = ocr_result.get_words_in_area(bounding_box) | ||
|
||
if expected_text is None: | ||
assert words == [] | ||
else: | ||
assert words is not None | ||
assert len(words) == 1 | ||
assert words[0].text.strip() == expected_text |