added from_annotations classmethod and fixed bug when generating webencoded image

This commit is contained in:
Brian Bjarke Jensen
2024-02-24 22:43:26 +01:00
parent 3fb2fb6258
commit d3eac22706
+36 -4
View File
@@ -4,11 +4,12 @@ from PIL import Image
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from base64 import b64encode from base64 import b64encode
import random
import logging import logging
from typing import List from typing import List
from src.model_experiential import ExperientialModelOutput from src.model_experiential import (
VisualSyntaxModelOutput
)
from src.model_interpersonal import ( from src.model_interpersonal import (
ContactModelOutput, ContactModelOutput,
AngleModelOutput, AngleModelOutput,
@@ -25,7 +26,7 @@ from src.model_textual import (
) )
class ModelOutputs(BaseModel): class ModelOutputs(BaseModel):
experiential: ExperientialModelOutput visual_syntax: VisualSyntaxModelOutput
contact: ContactModelOutput contact: ContactModelOutput
angle: AngleModelOutput angle: AngleModelOutput
point_of_view: PointOfViewModelOutput point_of_view: PointOfViewModelOutput
@@ -51,6 +52,37 @@ class ModelOutputs(BaseModel):
in cls.model_fields.items() in cls.model_fields.items()
} }
return cls(**kwargs) return cls(**kwargs)
@classmethod
def from_annotations(
cls,
visual_syntax: str,
contact: str,
angle: str,
point_of_view: str,
distance: str,
modality_lighting: str,
modality_color: str,
modality_depth: str,
information_value: str,
framing: str,
salience: str
) -> ModelOutputs:
"""Instantiate from annotation."""
kwargs = {
"visual_syntax": VisualSyntaxModelOutput.from_choice(visual_syntax),
"contact": ContactModelOutput.from_choice(contact),
"angle": AngleModelOutput.from_choice(angle),
"point_of_view": PointOfViewModelOutput.from_choice(point_of_view),
"distance": DistanceModelOutput.from_choice(distance),
"modality_lighting": ModalityLightingModelOutput.from_choice(modality_lighting),
"modality_color": ModalityColorModelOutput.from_choice(modality_color),
"modality_depth": ModalityDepthModelOutput.from_choice(modality_depth),
"information_value": InformationValueModelOutput.from_choice(information_value),
"framing": FramingModelOutput.from_choice(framing),
"salience": SalienceModelOutput.from_choice(salience)
}
return cls(**kwargs)
class VisualCommunication(BaseModel): class VisualCommunication(BaseModel):
@@ -99,7 +131,7 @@ class VisualCommunication(BaseModel):
buffer = BytesIO() buffer = BytesIO()
self.image.save(buffer, format="png") self.image.save(buffer, format="png")
img_enc = b64encode(buffer.getvalue()).decode("utf-8") img_enc = b64encode(buffer.getvalue()).decode("utf-8")
return img_enc return f"data:image/png;base64, {img_enc}"
def generate_random_prediction(self, force: bool = False) -> None: def generate_random_prediction(self, force: bool = False) -> None:
"""Generate random prediction values.""" """Generate random prediction values."""