예제 #1
0
    def on_post(self, req: falcon.Request, resp: falcon.Response):
        source_value = req.get_param("source_value", required=True)
        source_type = req.get_param("source_type", required=True)
        target_value = req.get_param("target_value", required=True)
        target_type = req.get_param("target_type", required=True)

        auth = parse_auth(req)
        username = None if auth is None else auth.get_username()
        completed_at = datetime.datetime.utcnow()

        target_value_tag = get_tag(target_value)
        source_value_tag = get_tag(source_value)
        taxonomy_value = match_unprefixed_value(target_value_tag, target_type)

        query = LogoAnnotation.update(
            {
                LogoAnnotation.annotation_type: target_type,
                LogoAnnotation.annotation_value: target_value,
                LogoAnnotation.annotation_value_tag: target_value_tag,
                LogoAnnotation.taxonomy_value: taxonomy_value,
                LogoAnnotation.username: username,
                LogoAnnotation.completed_at: completed_at,
            }
        ).where(
            LogoAnnotation.annotation_type == source_type,
            LogoAnnotation.annotation_value_tag == source_value_tag,
        )
        updated = query.execute()
        resp.media = {"updated": updated}
예제 #2
0
    def search(self, req: falcon.Request, resp: falcon.Response):
        count: int = req.get_param_as_int(
            "count", min_value=1, max_value=2000, default=25
        )
        type_: Optional[str] = req.get_param("type")
        barcode: Optional[str] = req.get_param("barcode")
        value: Optional[str] = req.get_param("value")
        min_confidence: Optional[float] = req.get_param_as_float("min_confidence")
        random: bool = req.get_param_as_bool("random", default=False)
        server_domain: Optional[str] = req.get_param("server_domain")
        annotated: bool = req.get_param_as_bool("annotated", default=False)

        where_clauses = [LogoAnnotation.annotation_value.is_null(not annotated)]
        join_image_prediction = False
        join_image_model = False

        if server_domain:
            where_clauses.append(ImageModel.server_domain == server_domain)
            join_image_model = True

        if min_confidence is not None:
            where_clauses.append(ImagePrediction.max_confidence >= min_confidence)
            join_image_prediction = True

        if barcode is not None:
            where_clauses.append(ImageModel.barcode == barcode)
            join_image_model = True

        if type_ is not None:
            where_clauses.append(LogoAnnotation.annotation_type == type_)

        if value is not None:
            value_tag = get_tag(value)
            where_clauses.append(LogoAnnotation.annotation_value_tag == value_tag)

        query = LogoAnnotation.select()
        join_image_prediction = join_image_prediction or join_image_model

        if join_image_prediction:
            query = query.join(ImagePrediction)

            if join_image_model:
                query = query.join(ImageModel)

        if where_clauses:
            query = query.where(*where_clauses)

        query_count = query.count()

        if random:
            query = query.order_by(peewee.fn.Random())

        query = query.limit(count)
        items = [item.to_dict() for item in query.iterator()]

        for item in items:
            image_prediction = item.pop("image_prediction")
            item["image"] = image_prediction["image"]

        resp.media = {"logos": items, "count": query_count}
예제 #3
0
    def on_post(self, req: falcon.Request, resp: falcon.Response):
        server_domain = req.media.get("server_domain", settings.OFF_SERVER_DOMAIN)
        annotations = req.media["annotations"]
        auth = parse_auth(req)
        username = None if auth is None else auth.get_username()
        completed_at = datetime.datetime.utcnow()
        annotated_logos = []

        for annotation in annotations:
            logo_id = annotation["logo_id"]
            type_ = annotation["type"]
            value = annotation["value"] or None
            logo = LogoAnnotation.get_by_id(logo_id)
            if value is not None:
                logo.annotation_value = value
                value_tag = get_tag(value)
                logo.annotation_value_tag = value_tag
                logo.taxonomy_value = match_unprefixed_value(value_tag, type_)

            logo.annotation_type = type_
            logo.username = username
            logo.completed_at = completed_at
            logo.save()
            annotated_logos.append(logo)

        created = generate_insights_from_annotated_logos(annotated_logos, server_domain)
        resp.media = {"created insights": created}
예제 #4
0
def spellcheck(
    pattern: str,
    correction: str,
    country: str = "fr",
    dry: bool = False,
) -> None:
    from robotoff.cli.spellcheck import correct_ingredient
    from robotoff.off import OFFAuthentication
    from robotoff.utils import get_logger
    from robotoff.utils.text import get_tag

    username = typer.prompt("Username ?")
    password = typer.prompt("Password ?", hide_input=True)

    get_logger()
    ingredient = get_tag(pattern)
    comment = "Fixing '{}' typo".format(pattern)
    auth = OFFAuthentication(username=username, password=password)
    correct_ingredient(country,
                       ingredient,
                       pattern,
                       correction,
                       comment,
                       dry_run=dry,
                       auth=auth)
예제 #5
0
def find_packaging(content: Union[OCRResult, str]) -> List[RawInsight]:
    insights = []

    text = get_text(content)

    if not text:
        return []

    processor = KEYWORD_PROCESSOR_STORE.get()

    for (packaging_str, _), span_start, span_end in processor.extract_keywords(
        text, span_info=True
    ):
        packagings = packaging_str.split(";")

        for packaging in packagings:
            match_str = text[span_start:span_end]
            insights.append(
                RawInsight(
                    type=InsightType.packaging,
                    value_tag=get_tag(packaging),
                    value=packaging,
                    data={"text": match_str, "notify": False},
                    automatic_processing=True,
                )
            )

    return insights
예제 #6
0
def extract_brands_google_cloud_vision(ocr_result: OCRResult) -> List[RawInsight]:
    insights = []
    for logo_annotation in ocr_result.logo_annotations:
        if logo_annotation.description in LOGO_ANNOTATION_BRANDS:
            brand = LOGO_ANNOTATION_BRANDS[logo_annotation.description]

            insights.append(
                RawInsight(
                    type=InsightType.brand,
                    value=brand,
                    value_tag=get_tag(brand),
                    automatic_processing=False,
                    predictor="google-cloud-vision",
                    data={"confidence": logo_annotation.score, "notify": False},
                )
            )

    return insights
예제 #7
0
    def spellcheck(
        pattern: str,
        correction: str,
        country: str,
        username: str,
        password: str,
        dry: bool,
    ):
        from robotoff.cli.spellcheck import correct_ingredient
        from robotoff.utils.text import get_tag
        from robotoff.utils import get_logger
        from robotoff.off import OFFAuthentication

        get_logger()
        ingredient = get_tag(pattern)
        comment = "Fixing '{}' typo".format(pattern)
        auth = OFFAuthentication(username=username, password=password)
        correct_ingredient(country,
                           ingredient,
                           pattern,
                           correction,
                           comment,
                           dry_run=dry,
                           auth=auth)
예제 #8
0
    def on_put(self, req: falcon.Request, resp: falcon.Response, logo_id: int):
        logo = LogoAnnotation.get_or_none(id=logo_id)

        if logo is None:
            resp.status = falcon.HTTP_404
            return

        type_ = req.media["type"]
        value = req.media["value"] or None
        updated = False

        if type_ != logo.annotation_type:
            logo.annotation_type = type_
            updated = True

        if value != logo.annotation_value:
            logo.annotation_value = value

            if value is not None:
                value_tag = get_tag(value)
                logo.annotation_value_tag = value_tag
                logo.taxonomy_value = match_unprefixed_value(value_tag, type_)
            else:
                logo.annotation_value_tag = None
                logo.taxonomy_value = None

            updated = True

        if updated:
            auth = parse_auth(req)
            username = None if auth is None else auth.get_username()
            logo.username = username
            logo.completed_at = datetime.datetime.utcnow()
            logo.save()

        resp.status = falcon.HTTP_204
예제 #9
0
def test_get_tag(value: str, output: str):
    assert get_tag(value) == output