diff --git a/robotoff/insights/annotate.py b/robotoff/insights/annotate.py index 40839ef143..feb3bce4d7 100644 --- a/robotoff/insights/annotate.py +++ b/robotoff/insights/annotate.py @@ -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) diff --git a/tests/integration/insights/test_annotate.py b/tests/integration/insights/test_annotate.py index 01268fa89b..e09c88bd30 100644 --- a/tests/integration/insights/test_annotate.py +++ b/tests/integration/insights/test_annotate.py @@ -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: @@ -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, @@ -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", } @@ -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( @@ -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, @@ -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} @@ -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=(