From d3eac227068885bfa1b6b50c62ace05b9521f160 Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Sat, 24 Feb 2024 22:43:26 +0100 Subject: [PATCH] added from_annotations classmethod and fixed bug when generating webencoded image --- src/database/classes.py | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/database/classes.py b/src/database/classes.py index 568b415..06387a0 100644 --- a/src/database/classes.py +++ b/src/database/classes.py @@ -4,11 +4,12 @@ from PIL import Image from io import BytesIO from pathlib import Path from base64 import b64encode -import random import logging from typing import List -from src.model_experiential import ExperientialModelOutput +from src.model_experiential import ( + VisualSyntaxModelOutput +) from src.model_interpersonal import ( ContactModelOutput, AngleModelOutput, @@ -25,7 +26,7 @@ from src.model_textual import ( ) class ModelOutputs(BaseModel): - experiential: ExperientialModelOutput + visual_syntax: VisualSyntaxModelOutput contact: ContactModelOutput angle: AngleModelOutput point_of_view: PointOfViewModelOutput @@ -51,6 +52,37 @@ class ModelOutputs(BaseModel): in cls.model_fields.items() } 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): @@ -99,7 +131,7 @@ class VisualCommunication(BaseModel): buffer = BytesIO() self.image.save(buffer, format="png") 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: """Generate random prediction values."""