Ejemplo n.º 1
0
def run_object_detection(barcode: str, image_url: str, server_domain: str):
    source_image = get_source_from_image_url(image_url)
    image_instance = ImageModel.get_or_none(source_image=source_image)

    if image_instance is None:
        logger.warning("Missing image in DB for image {}".format(image_url))
        return

    timestamp = datetime.datetime.utcnow()
    results = predict_objects(barcode, image_url, server_domain)

    logos = []
    for model_name, result in results.items():
        data = result.to_json(threshold=0.1)
        max_confidence = max([item["score"] for item in data], default=None)
        image_prediction = ImagePrediction.create(
            image=image_instance,
            type="object_detection",
            model_name=model_name,
            model_version=settings.OBJECT_DETECTION_MODEL_VERSION[model_name],
            data={"objects": data},
            timestamp=timestamp,
            max_confidence=max_confidence,
        )
        for i, item in enumerate(data):
            if item["score"] >= 0.5:
                logo = LogoAnnotation.create(
                    image_prediction=image_prediction,
                    index=i,
                    score=item["score"],
                    bounding_box=item["bounding_box"],
                )
                logos.append(logo)

    if logos:
        add_logos_to_ann(image_instance, logos)
        save_nearest_neighbors(logos)
        thresholds = LOGO_CONFIDENCE_THRESHOLDS.get()
        import_logo_insights(logos,
                             thresholds=thresholds,
                             server_domain=server_domain)
Ejemplo n.º 2
0
    def add_logo_to_ann(sleep_time: float):
        from itertools import groupby
        import time

        import requests
        import tqdm

        from robotoff.logos import add_logos_to_ann, get_stored_logo_ids
        from robotoff.models import db, ImageModel, ImagePrediction, LogoAnnotation
        from robotoff.utils import get_logger

        logger = get_logger()
        seen = get_stored_logo_ids()

        with db:
            logos_iter = tqdm.tqdm(LogoAnnotation.select().join(
                ImagePrediction).join(ImageModel).where(
                    LogoAnnotation.nearest_neighbors.is_null()).order_by(
                        ImageModel.id).iterator())
            for _, logo_batch in groupby(
                    logos_iter, lambda x: x.image_prediction.image.id):
                logos = list(logo_batch)

                if all(l.id in seen for l in logos):
                    continue

                image = logos[0].image_prediction.image
                logger.info(f"Adding logos of image {image.id}")
                try:
                    added = add_logos_to_ann(image, logos)
                except requests.exceptions.ReadTimeout:
                    logger.warn("Request timed-out during logo addition")
                    continue

                logger.info(f"Added: {added}")

                if sleep_time:
                    time.sleep(sleep_time)
Ejemplo n.º 3
0
def run_object_detection(
    barcode: str, image: Image.Image, source_image: str, server_domain: str
):
    """Detect logos using the universal logo detector model and generate
    logo-related insights.

    :param barcode: Product barcode
    :param image: Pillow Image to run the object detection on
    :param image_url: URL of the image to use
    :param server_domain: The server domain associated with the image
    """
    logger.info(
        f"Running object detection for product {barcode} ({server_domain}), "
        f"image {source_image}"
    )
    image_instance = ImageModel.get_or_none(source_image=source_image)

    if image_instance is None:
        logger.warning("Missing image in DB for image %s", source_image)
        return

    timestamp = datetime.datetime.utcnow()
    model_name = "universal-logo-detector"
    results = ObjectDetectionModelRegistry.get(model_name).detect_from_image(
        image, output_image=False
    )
    data = results.to_json(threshold=0.1)
    max_confidence = max([item["score"] for item in data], default=None)
    image_prediction = ImagePrediction.create(
        image=image_instance,
        type="object_detection",
        model_name=model_name,
        model_version=settings.OBJECT_DETECTION_MODEL_VERSION[model_name],
        data={"objects": data},
        timestamp=timestamp,
        max_confidence=max_confidence,
    )

    logos = []
    for i, item in enumerate(data):
        if item["score"] >= 0.5:
            logo = LogoAnnotation.create(
                image_prediction=image_prediction,
                index=i,
                score=item["score"],
                bounding_box=item["bounding_box"],
            )
            logos.append(logo)

    logger.info(f"{len(logos)} logos found for image {source_image}")
    if logos:
        add_logos_to_ann(image_instance, logos)

        try:
            save_nearest_neighbors(logos)
        except requests.exceptions.HTTPError as e:
            resp = e.response
            logger.warning(
                f"Could not save nearest neighbors in ANN: {resp.status_code}: %s",
                resp.text,
            )

        thresholds = LOGO_CONFIDENCE_THRESHOLDS.get()
        import_logo_insights(logos, thresholds=thresholds, server_domain=server_domain)