Skip to content

Commit

Permalink
feat: add product_insight.bounding_box field
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Oct 25, 2023
1 parent b7519e2 commit 404d155
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions migrations/002_add_bounding_box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Peewee migrations -- 002_add_bounding_box.py."""

from contextlib import suppress

import peewee as pw
from peewee_migrate import Migrator

with suppress(ImportError):
import playhouse.postgres_ext as pw_pext


def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
"""Write your migrations here."""

migrator.add_fields(
"product_insight", bounding_box=pw_pext.BinaryJSONField(index=True, null=True)
)


def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
"""Write your rollback migrations here."""

migrator.remove_fields("product_insight", "bounding_box")
6 changes: 6 additions & 0 deletions robotoff/insights/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ def generate_insights(
candidate.source_image,
)

# Copy bounding box information to reserved field,
# `data->bounding_box` for insights is deprecated and will be
# removed in a future release
if "bounding_box" in candidate.data:
candidate.bounding_box = candidate.data["bounding_box"]

if candidate.data.get("is_annotation"):
username = candidate.data.get("username")
if username:
Expand Down
9 changes: 9 additions & 0 deletions robotoff/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ class ProductInsight(BaseModel):
# Confidence score of the insight, may be null
confidence = peewee.FloatField(null=True, index=True)

# bounding box corresponding to the area of the image related
# to the insight that was detected.
# For example:
# - for logo insights, it's the bounding box of the predicted
# logo
# - for OCR-based insights, it's the text that triggered the
# creation of the insight
bounding_box = BinaryJSONField(null=True, default=list)

def get_product_id(self) -> ProductIdentifier:
return ProductIdentifier(self.barcode, ServerType[self.server_type])

Expand Down
1 change: 1 addition & 0 deletions tests/integration/models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Meta:
confidence: Optional[float] = None
predictor: Optional[str] = None
predictor_version: Optional[str] = None
bounding_box: Optional[list[float]] = None


class PredictionFactory(PeeweeModelFactory):
Expand Down

0 comments on commit 404d155

Please sign in to comment.