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

fix: fix product weight detection bug #1403

Merged
merged 1 commit into from
Aug 28, 2024
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
20 changes: 18 additions & 2 deletions robotoff/prediction/ocr/product_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,23 @@ def is_valid_weight(weight_value: str) -> bool:
return True


def is_extreme_weight(normalized_value: float, unit: str) -> bool:
def is_extreme_weight(
normalized_value: float, unit: str, count: int | None = None
) -> bool:
"""Return True if the weight is extreme, i.e is likely wrongly detected.

If considered extreme, a prediction won't be generated.

:param normalized_value: the normalized weight value
:param unit: the normalized weight unit
:param count: the number of items in the pack, if any
:return: True if the weight is extreme, False otherwise
"""
if count is not None and int(count) > 20:
# More than 20 items in a pack is quite unlikely for
# a consumer product
return True

if unit == "g":
# weights above 10 kg
return normalized_value >= 10000 or normalized_value <= 10
Expand Down Expand Up @@ -200,7 +216,7 @@ def process_multi_packaging(match) -> Optional[dict]:
normalized_value, normalized_unit = normalize_weight(value, unit)

# Check that the weight is not extreme
if is_extreme_weight(normalized_value, normalized_unit):
if is_extreme_weight(normalized_value, normalized_unit, count):
return None

text = f"{count} x {value} {unit}"
Expand Down
27 changes: 14 additions & 13 deletions tests/unit/prediction/ocr/test_product_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,23 @@ def test_is_valid_weight(value: str, is_valid: bool):


@pytest.mark.parametrize(
"value,unit,expected",
"value,unit,count,expected",
[
(10000, "g", True),
(10000, "ml", True),
(9999, "ml", False),
(9999, "g", False),
(100, "g", False),
(100, "ml", False),
(10, "ml", True),
(3, "ml", True),
(10, "g", True),
(2, "g", True),
(10000, "g", None, True),
(10000, "ml", None, True),
(9999, "ml", None, False),
(9999, "g", None, False),
(100, "g", None, False),
(100, "ml", None, False),
(10, "ml", None, True),
(3, "ml", None, True),
(10, "g", None, True),
(2, "g", None, True),
(200, "g", 21, True),
],
)
def test_is_extreme_weight(value: float, unit: str, expected: bool):
assert is_extreme_weight(value, unit) is expected
def test_is_extreme_weight(value: float, unit: str, count: int | None, expected: bool):
assert is_extreme_weight(value, unit, count) is expected


@pytest.mark.parametrize(
Expand Down
Loading