示例#1
0
def test_product_insights_merge():
    insights_1 = [
        RawInsight(type=InsightType.label, data={}, value_tag="en:organic")
    ]
    product_insights_1 = ProductInsights(
        insights=insights_1,
        barcode="123",
        type=InsightType.label,
        source_image="/123/1.jpg",
    )

    insights_2 = [
        RawInsight(type=InsightType.label, data={}, value_tag="en:pgi")
    ]
    product_insights_2 = ProductInsights(
        insights=insights_2,
        barcode="123",
        type=InsightType.label,
        source_image="/123/1.jpg",
    )

    merged_product_insights = ProductInsights.merge(
        [product_insights_1, product_insights_2])

    assert merged_product_insights.type == InsightType.label
    assert merged_product_insights.barcode == "123"
    assert merged_product_insights.source_image == "/123/1.jpg"
    assert merged_product_insights.insights == insights_1 + insights_2
示例#2
0
def add_category_insight(barcode: str, product: JSONType, server_domain: str) -> bool:
    if get_server_type(server_domain) != ServerType.off:
        return False

    product_insights = []
    product_insight = predict_category_from_product_es(product)

    if product_insight is not None:
        product_insights.append(product_insight)

    product_insight = predict_category_from_product_ml(product, filter_blacklisted=True)

    if product_insight is not None:
        product_insights.append(product_insight)

    if not product_insights:
        return False

    merged_product_insight = ProductInsights.merge(product_insights)
    product_store = get_product_store()
    importer = InsightImporterFactory.create(InsightType.category, product_store)

    imported = importer.import_insights(
        [merged_product_insight],
        server_domain=server_domain,
        automatic=False,
    )

    if imported:
        logger.info("Category insight imported for product {}".format(barcode))

    return bool(imported)
示例#3
0
def get_insights_from_image(
        barcode: str, image_url: str,
        ocr_url: str) -> Dict[InsightType, ProductInsights]:
    try:
        ocr_insights = extract_ocr_insights(ocr_url,
                                            IMAGE_IMPORT_INSIGHT_TYPES)
    except requests.exceptions.RequestException as e:
        logger.info("error during OCR JSON download", exc_info=e)
        return {}
    except OCRParsingException as e:
        logger.error("OCR JSON Parsing error", exc_info=e)
        return {}

    extract_nutriscore = has_nutriscore_insight(
        ocr_insights.get(InsightType.label, None))
    image_ml_insights = extract_image_ml_insights(
        image_url, extract_nutriscore=extract_nutriscore)

    insight_types = set(ocr_insights.keys()).union(image_ml_insights.keys())

    results: Dict[InsightType, ProductInsights] = {}

    for insight_type in insight_types:
        product_insights: List[ProductInsights] = []

        if insight_type in ocr_insights:
            product_insights.append(ocr_insights[insight_type])

        if insight_type in image_ml_insights:
            product_insights.append(image_ml_insights[insight_type])

        results[insight_type] = ProductInsights.merge(product_insights)

    return results
示例#4
0
def test_product_insights_failed_merge():
    with pytest.raises(ValueError):
        ProductInsights.merge([])

    with pytest.raises(ValueError):
        ProductInsights.merge([
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.label,
                source_image="/123/1.jpg",
            ),
            ProductInsights(
                insights=[],
                barcode="234",
                type=InsightType.label,
                source_image="/123/1.jpg",
            ),
        ])

    with pytest.raises(ValueError):
        ProductInsights.merge([
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.label,
                source_image="/123/1.jpg",
            ),
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.category,
                source_image="/123/1.jpg",
            ),
        ])

    with pytest.raises(ValueError):
        ProductInsights.merge([
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.label,
                source_image="/123/1.jpg",
            ),
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.label,
                source_image="/123/2.jpg",
            ),
        ])

    with pytest.raises(ValueError):
        ProductInsights.merge([
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.label,
                source_image="/123/1.jpg",
            ),
            ProductInsights(
                insights=[],
                barcode="123",
                type=InsightType.category,
                source_image="/123/2.jpg",
            ),
        ])