Module datatap.droplet.bounding_box
Expand source code
from __future__ import annotations
from typing import Optional
from typing_extensions import TypedDict
from ..geometry import Rectangle, RectangleJson
from ..utils import basic_repr
class _BoundingBoxJsonOptional(TypedDict, total = False):
confidence: float
class BoundingBoxJson(_BoundingBoxJsonOptional, TypedDict):
"""
The serialized JSON representation of a bounding box.
"""
rectangle: RectangleJson
class BoundingBox:
"""
A `BoundingBox` represents the area within an image taken up by a detection,
specified as an axis-aligned rectangle.
"""
rectangle: Rectangle
"""
The area within the image where the corresponding detection appears.
"""
confidence: Optional[float]
"""
The confidence associated with this bounding box.
"""
@staticmethod
def from_json(json: BoundingBoxJson) -> BoundingBox:
"""
Constructs a `BoundingBox` from a `BoundingBoxJson`.
"""
return BoundingBox(
Rectangle.from_json(json["rectangle"]),
confidence = json.get("confidence")
)
def __init__(self, rectangle: Rectangle, *, confidence: Optional[float] = None):
self.rectangle = rectangle
self.confidence = confidence
self.rectangle.assert_valid()
def __repr__(self) -> str:
return basic_repr("BoundingBox", self.rectangle, confidence = self.confidence)
def __eq__(self, other: BoundingBox) -> bool:
if not isinstance(other, BoundingBox): # type: ignore - pyright complains about the isinstance check being redundant
return NotImplemented
return self.rectangle == other.rectangle and self.confidence == other.confidence
def to_json(self) -> BoundingBoxJson:
"""
Serializes this `BoundingBox` to a `BoundingBoxJson`.
"""
json: BoundingBoxJson = {
"rectangle": self.rectangle.to_json()
}
if self.confidence is not None:
json["confidence"] = self.confidence
return json
def meets_confidence_threshold(self, threshold: float) -> bool:
"""
Returns `True` if and only if the confidence of this bounding box is
either unset or it is at least the given `threshold`.
"""
return self.confidence is None or self.confidence >= threshold
Classes
class BoundingBox (rectangle: Rectangle, *, confidence: Optional[float] = None)
-
A
BoundingBox
represents the area within an image taken up by a detection, specified as an axis-aligned rectangle.Expand source code
class BoundingBox: """ A `BoundingBox` represents the area within an image taken up by a detection, specified as an axis-aligned rectangle. """ rectangle: Rectangle """ The area within the image where the corresponding detection appears. """ confidence: Optional[float] """ The confidence associated with this bounding box. """ @staticmethod def from_json(json: BoundingBoxJson) -> BoundingBox: """ Constructs a `BoundingBox` from a `BoundingBoxJson`. """ return BoundingBox( Rectangle.from_json(json["rectangle"]), confidence = json.get("confidence") ) def __init__(self, rectangle: Rectangle, *, confidence: Optional[float] = None): self.rectangle = rectangle self.confidence = confidence self.rectangle.assert_valid() def __repr__(self) -> str: return basic_repr("BoundingBox", self.rectangle, confidence = self.confidence) def __eq__(self, other: BoundingBox) -> bool: if not isinstance(other, BoundingBox): # type: ignore - pyright complains about the isinstance check being redundant return NotImplemented return self.rectangle == other.rectangle and self.confidence == other.confidence def to_json(self) -> BoundingBoxJson: """ Serializes this `BoundingBox` to a `BoundingBoxJson`. """ json: BoundingBoxJson = { "rectangle": self.rectangle.to_json() } if self.confidence is not None: json["confidence"] = self.confidence return json def meets_confidence_threshold(self, threshold: float) -> bool: """ Returns `True` if and only if the confidence of this bounding box is either unset or it is at least the given `threshold`. """ return self.confidence is None or self.confidence >= threshold
Class variables
var confidence : Optional[float]
-
The confidence associated with this bounding box.
var rectangle : Rectangle
-
The area within the image where the corresponding detection appears.
Static methods
def from_json(json: BoundingBoxJson) ‑> BoundingBox
-
Constructs a
BoundingBox
from aBoundingBoxJson
.Expand source code
@staticmethod def from_json(json: BoundingBoxJson) -> BoundingBox: """ Constructs a `BoundingBox` from a `BoundingBoxJson`. """ return BoundingBox( Rectangle.from_json(json["rectangle"]), confidence = json.get("confidence") )
Methods
def meets_confidence_threshold(self, threshold: float) ‑> bool
-
Returns
True
if and only if the confidence of this bounding box is either unset or it is at least the giventhreshold
.Expand source code
def meets_confidence_threshold(self, threshold: float) -> bool: """ Returns `True` if and only if the confidence of this bounding box is either unset or it is at least the given `threshold`. """ return self.confidence is None or self.confidence >= threshold
def to_json(self) ‑> BoundingBoxJson
-
Serializes this
BoundingBox
to aBoundingBoxJson
.Expand source code
def to_json(self) -> BoundingBoxJson: """ Serializes this `BoundingBox` to a `BoundingBoxJson`. """ json: BoundingBoxJson = { "rectangle": self.rectangle.to_json() } if self.confidence is not None: json["confidence"] = self.confidence return json
class BoundingBoxJson (*args, **kwargs)
-
The serialized JSON representation of a bounding box.
Expand source code
class BoundingBoxJson(_BoundingBoxJsonOptional, TypedDict): """ The serialized JSON representation of a bounding box. """ rectangle: RectangleJson
Ancestors
- builtins.dict
Class variables
var confidence : float
var rectangle : Tuple[Tuple[float, float], Tuple[float, float]]