Skip to content

Commit

Permalink
fix(nutrisight): allow to override nutrition image (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 authored Dec 20, 2024
1 parent 221a8ec commit a53485c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
6 changes: 3 additions & 3 deletions robotoff/insights/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,9 @@ def select_nutrition_image(
# but we provide a default value just in case.
lang = product.get("lang", "en")
image_key = f"nutrition_{lang}"
# We don't want to select the nutrition image if one has already been
# selected
if image_key in images:
# We don't want to select the nutrition image if the image is already
# selected as nutrition image for the main language
if image_key in images and images[image_key]["imgid"] == image_id:
return None

rotation = get_image_rotation(insight.source_image)
Expand Down
44 changes: 36 additions & 8 deletions tests/integration/insights/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_process_annotate_no_user_data(
mock_save_ingredients.assert_called()

class TestNutrientExtractionAnnotator:
SOURCE_IMAGE = "/872/032/603/7888/1.jpg"
SOURCE_IMAGE = "/872/032/603/7888/2.jpg"

@pytest.fixture
def mock_select_rotate_image(self, mocker) -> Mock:
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_select_nutrition_image_no_image_meta(
mock_select_rotate_image: Mock,
nutrient_extraction_insight: ProductInsightFactory,
):
product: JSONType = {"images": {"1": {}}, "lang": "fr"}
product: JSONType = {"images": {"2": {}}, "lang": "fr"}
NutrientExtractionAnnotator.select_nutrition_image(
insight=nutrient_extraction_insight,
product=product,
Expand All @@ -213,8 +213,8 @@ def test_select_nutrition_image_already_selected(
):
product: JSONType = {
"images": {
"1": {"sizes": {"full": {"w": 1000, "h": 2000}}},
"nutrition_fr": {},
"2": {"sizes": {"full": {"w": 1000, "h": 2000}}},
"nutrition_fr": {"imgid": "2"},
},
"lang": "fr",
}
Expand All @@ -230,7 +230,7 @@ def test_select_nutrition_image(
nutrient_extraction_insight: ProductInsightFactory,
):
product = {
"images": {"1": {"sizes": {"full": {"w": 1000, "h": 2000}}}},
"images": {"2": {"sizes": {"full": {"w": 1000, "h": 2000}}}},
"lang": "fr",
}
NutrientExtractionAnnotator.select_nutrition_image(
Expand All @@ -239,7 +239,35 @@ def test_select_nutrition_image(
)
mock_select_rotate_image.assert_called_once_with(
product_id=nutrient_extraction_insight.get_product_id(),
image_id="1",
image_id="2",
image_key="nutrition_fr",
rotate=None,
crop_bounding_box=None,
auth=None,
is_vote=False,
insight_id=nutrient_extraction_insight.id,
)

def test_select_nutrition_image_override_nutrition_image(
self,
mock_select_rotate_image: Mock,
nutrient_extraction_insight: ProductInsightFactory,
):
product = {
"images": {
"2": {"sizes": {"full": {"w": 1000, "h": 2000}}},
# image 1 already selected, should be overridden
"nutrition_fr": {"imgid": "1"},
},
"lang": "fr",
}
NutrientExtractionAnnotator.select_nutrition_image(
insight=nutrient_extraction_insight,
product=product,
)
mock_select_rotate_image.assert_called_once_with(
product_id=nutrient_extraction_insight.get_product_id(),
image_id="2",
image_key="nutrition_fr",
rotate=None,
crop_bounding_box=None,
Expand All @@ -254,7 +282,7 @@ def test_select_nutrition_image_with_rotation_and_nutrition_table_detection(
nutrient_extraction_insight: ProductInsightFactory,
):
product = {
"images": {"1": {"sizes": {"full": {"w": 1000, "h": 2000}}}},
"images": {"2": {"sizes": {"full": {"w": 1000, "h": 2000}}}},
"lang": "fr",
}
rotation_data = {"rotation": 90}
Expand Down Expand Up @@ -289,7 +317,7 @@ def test_select_nutrition_image_with_rotation_and_nutrition_table_detection(
)
mock_select_rotate_image.assert_called_once_with(
product_id=nutrient_extraction_insight.get_product_id(),
image_id="1",
image_id="2",
image_key="nutrition_fr",
rotate=rotation_data["rotation"],
crop_bounding_box=(
Expand Down

0 comments on commit a53485c

Please sign in to comment.