Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions openfoodfacts/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class OCRResult:
"logo_annotations",
"safe_search_annotation",
"label_annotations",
"face_annotations",
)

def __init__(self, data: JSONType):
Expand All @@ -117,6 +118,7 @@ def __init__(self, data: JSONType):
self.logo_annotations: List[LogoAnnotation] = []
self.label_annotations: List[LabelAnnotation] = []
self.safe_search_annotation: Optional[SafeSearchAnnotation] = None
self.face_annotations: List[FaceAnnotation] = []

for text_annotation_data in data.get("textAnnotations", []):
text_annotation = OCRTextAnnotation(text_annotation_data)
Expand Down Expand Up @@ -145,6 +147,10 @@ def __init__(self, data: JSONType):
data["safeSearchAnnotation"]
)

for face_annotation_data in data.get("faceAnnotations", []):
face_annotation = FaceAnnotation(face_annotation_data)
self.face_annotations.append(face_annotation)

def get_full_text(self) -> str:
return (
self.full_text_annotation.text
Expand Down Expand Up @@ -186,6 +192,9 @@ def get_logo_annotations(self) -> List["LogoAnnotation"]:
def get_label_annotations(self) -> List["LabelAnnotation"]:
return self.label_annotations

def get_face_annotations(self) -> List["FaceAnnotation"]:
return self.face_annotations

def get_safe_search_annotation(self):
return self.safe_search_annotation

Expand Down Expand Up @@ -1130,6 +1139,31 @@ def __init__(self, data: JSONType):
self.description = data["description"]


class FaceAnnotation:
__slots__ = (
"detection_confidence",
"joy_likelihood",
"sorrow_likelihood",
"anger_likelihood",
"surprise_likelihood",
"under_exposed_likelihood",
"blurred_likelihood",
"headwear_likelihood",
)

def __init__(self, data: JSONType):
self.detection_confidence = data.get("detectionConfidence", 0.0)
self.joy_likelihood = Likelihood[data.get("joyLikelihood", "UNKNOWN")]
self.sorrow_likelihood = Likelihood[data.get("sorrowLikelihood", "UNKNOWN")]
self.anger_likelihood = Likelihood[data.get("angerLikelihood", "UNKNOWN")]
self.surprise_likelihood = Likelihood[data.get("surpriseLikelihood", "UNKNOWN")]
self.under_exposed_likelihood = Likelihood[
data.get("underExposedLikelihood", "UNKNOWN")
]
self.blurred_likelihood = Likelihood[data.get("blurredLikelihood", "UNKNOWN")]
self.headwear_likelihood = Likelihood[data.get("headwearLikelihood", "UNKNOWN")]


class SafeSearchAnnotation:
__slots__ = ("adult", "spoof", "medical", "violence", "racy")

Expand All @@ -1151,6 +1185,16 @@ def __init__(self, data: JSONType):
]


class Likelihood(enum.IntEnum):
UNKNOWN = 1
VERY_UNLIKELY = 2
UNLIKELY = 3
POSSIBLE = 4
LIKELY = 5
VERY_LIKELY = 6


# This class should be deleted and replaced by `Likelihood` in the future.
class SafeSearchAnnotationLikelihood(enum.IntEnum):
UNKNOWN = 1
VERY_UNLIKELY = 2
Expand Down
Loading