From 2e060f07763791b30c53f7c1c9a391d7c5181f71 Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Sat, 15 Jun 2024 16:19:51 +0200 Subject: [PATCH] updated to match images being stored in minio --- .../database/classes/visual_communication.py | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/shared/database/classes/visual_communication.py b/shared/database/classes/visual_communication.py index f20a0ab..9a9898b 100755 --- a/shared/database/classes/visual_communication.py +++ b/shared/database/classes/visual_communication.py @@ -7,11 +7,12 @@ from base64 import b64encode from io import BytesIO from pathlib import Path +from minio import Minio from PIL import Image from pydantic import BaseModel -from pydantic import field_serializer -from pydantic import field_validator +from shared.data_store import get +from shared.data_store import put from shared.dto import ModelData @@ -19,7 +20,7 @@ class VisualCommunication(BaseModel): """Visual communication model.""" name: str - image: Image.Image + object_name: str annotation: ModelData | None = None prediction: ModelData | None = None @@ -33,12 +34,22 @@ class VisualCommunication(BaseModel): return cls.__name__ @classmethod - def from_file(cls, path: Path) -> VisualCommunication: + def from_file(cls, path: Path, minio_client: Minio) -> VisualCommunication: """Instantiate from file.""" + assert isinstance(path, Path) + assert isinstance(minio_client, Minio) + # determine name name = path.stem + # open and upload image to minio image = Image.open(path) image.load() - return VisualCommunication(name=name, image=image) + buffer = BytesIO() + image.save(buffer, 'png') + object_name = put( + client=minio_client, + buffer=buffer, + ) + return VisualCommunication(name=name, object_name=object_name) @classmethod def decode_image(cls, content: str) -> Image.Image: @@ -46,35 +57,26 @@ class VisualCommunication(BaseModel): _, content_data = content.split(',') return Image.open(BytesIO(b64decode(content_data))) - @field_serializer('image') - @classmethod - def serialize_image(cls, image: Image.Image) -> bytes: # type: ignore - """Convert image to bytes for storage in database.""" - buffer = BytesIO() - image.save(buffer, format='JPEG') - return buffer.getvalue() + def get_image(self, minio_client: Minio) -> Image.Image: + """Load image data from minio.""" + assert isinstance(minio_client, Minio) + # get buffer from minio + buffer = get( + client=minio_client, + object_name=self.object_name, + ) + # convert data to image + im = Image.open(buffer) + return im - @field_validator('image', mode='before') - @classmethod - def convert_to_image( - cls, - image: Image.Image | BytesIO | bytes, - ) -> Image.Image: - """Convert bytes input from database into image.""" - if isinstance(image, bytes): - image = BytesIO(image) - if isinstance(image, BytesIO): - image = Image.open(image) - return image - - def __repr__(self) -> str: - return f"{self.classname()}(name='{self.name}')" - - def webencoded_image(self) -> str: + def webencoded_image(self, minio_client: Minio) -> str: """Convert image to be displayed on webpage.""" + assert isinstance(minio_client, Minio) + # get image from minio + image = self.get_image(minio_client) # convert images to bytes string buffer = BytesIO() - self.image.save(buffer, format='png') + image.save(buffer, format='png') img_enc = b64encode(buffer.getvalue()).decode('utf-8') return f"data:image/png;base64, {img_enc}"