updated database interface

This commit is contained in:
Brian Bjarke Jensen
2024-02-20 19:55:53 +01:00
parent f0b3de27ed
commit c166420909
3 changed files with 96 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
from __future__ import annotations
from pydantic import BaseModel, field_validator
from PIL import Image
from io import BytesIO
from pathlib import Path
from src.model_experiential import ExperientialModelOutput
from src.model_interpersonal import (
ContactModelOutput,
AngleModelOutput,
PointOfViewModelOutput,
DistanceModelOutput,
ModalityLightingModelOutput,
ModalityColorModelOutput,
ModalityDepthModelOutput
)
from src.model_textual import (
InformationValueModelOutput,
FramingModelOutput,
SalienceModelOutput
)
class ModelOutputs(BaseModel):
experiential: ExperientialModelOutput
contact: ContactModelOutput
angle: AngleModelOutput
point_of_view: PointOfViewModelOutput
distance: DistanceModelOutput
modality_lighting: ModalityLightingModelOutput
modality_color: ModalityColorModelOutput
modality_depth: ModalityDepthModelOutput
information_value: InformationValueModelOutput
framing: FramingModelOutput
salience: SalienceModelOutput
class VisualCommunication(BaseModel):
name: str
image: bytes
annotation: ModelOutputs | None = None
prediction: ModelOutputs | None = None
@classmethod
def classname(cls) -> str:
"""Return classname."""
return cls.__name__
@classmethod
def from_file(cls, path: Path) -> VisualCommunication:
"""Instantiate from file."""
name = path.stem
image = Image.open(path)
return VisualCommunication(name=name, image=image)
@field_validator("image", mode="before")
@classmethod
def convert_to_bytes(cls, raw: Image.Image | BytesIO | bytes) -> bytes:
if isinstance(raw, Image.Image):
raw = raw.tobytes()
if isinstance(raw, BytesIO):
raw = raw.read()
return raw
def __repr__(self) -> str:
return f"{self.classname()}(name='{self.name}')"
@property
def image(self) -> Image.Image:
return Image.open(BytesIO(self.image))