2024-07-30 20:04:29 +00:00
|
|
|
from typing import Any, Optional
|
|
|
|
|
|
|
|
import numpy.typing as npt
|
2024-07-31 12:47:00 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
2024-07-30 20:04:29 +00:00
|
|
|
|
|
|
|
|
2024-07-31 12:47:00 +00:00
|
|
|
class BoundingBox(BaseModel):
|
2024-07-30 20:04:29 +00:00
|
|
|
"""Bounding box helper class."""
|
|
|
|
|
|
|
|
xmin: int
|
|
|
|
ymin: int
|
|
|
|
xmax: int
|
|
|
|
ymax: int
|
|
|
|
|
|
|
|
def to_box(self) -> list[int]:
|
|
|
|
"""Convert to the array notation expected by SAM."""
|
|
|
|
return [self.xmin, self.ymin, self.xmax, self.ymax]
|
|
|
|
|
|
|
|
|
2024-07-31 12:47:00 +00:00
|
|
|
class DetectionResult(BaseModel):
|
2024-07-30 20:04:29 +00:00
|
|
|
"""Detection result from Grounding DINO or Grounded SAM."""
|
|
|
|
|
|
|
|
score: float
|
|
|
|
label: str
|
|
|
|
box: BoundingBox
|
|
|
|
mask: Optional[npt.NDArray[Any]] = None
|
2024-07-31 12:47:00 +00:00
|
|
|
model_config = ConfigDict(
|
|
|
|
# Allow arbitrary types for mask, since it will be a numpy array.
|
|
|
|
arbitrary_types_allowed=True
|
|
|
|
)
|