updated to match images being stored in minio
This commit is contained in:
@@ -7,11 +7,12 @@ from base64 import b64encode
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from pydantic import BaseModel
|
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
|
from shared.dto import ModelData
|
||||||
|
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ class VisualCommunication(BaseModel):
|
|||||||
"""Visual communication model."""
|
"""Visual communication model."""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
image: Image.Image
|
object_name: str
|
||||||
annotation: ModelData | None = None
|
annotation: ModelData | None = None
|
||||||
prediction: ModelData | None = None
|
prediction: ModelData | None = None
|
||||||
|
|
||||||
@@ -33,12 +34,22 @@ class VisualCommunication(BaseModel):
|
|||||||
return cls.__name__
|
return cls.__name__
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file(cls, path: Path) -> VisualCommunication:
|
def from_file(cls, path: Path, minio_client: Minio) -> VisualCommunication:
|
||||||
"""Instantiate from file."""
|
"""Instantiate from file."""
|
||||||
|
assert isinstance(path, Path)
|
||||||
|
assert isinstance(minio_client, Minio)
|
||||||
|
# determine name
|
||||||
name = path.stem
|
name = path.stem
|
||||||
|
# open and upload image to minio
|
||||||
image = Image.open(path)
|
image = Image.open(path)
|
||||||
image.load()
|
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
|
@classmethod
|
||||||
def decode_image(cls, content: str) -> Image.Image:
|
def decode_image(cls, content: str) -> Image.Image:
|
||||||
@@ -46,35 +57,26 @@ class VisualCommunication(BaseModel):
|
|||||||
_, content_data = content.split(',')
|
_, content_data = content.split(',')
|
||||||
return Image.open(BytesIO(b64decode(content_data)))
|
return Image.open(BytesIO(b64decode(content_data)))
|
||||||
|
|
||||||
@field_serializer('image')
|
def get_image(self, minio_client: Minio) -> Image.Image:
|
||||||
@classmethod
|
"""Load image data from minio."""
|
||||||
def serialize_image(cls, image: Image.Image) -> bytes: # type: ignore
|
assert isinstance(minio_client, Minio)
|
||||||
"""Convert image to bytes for storage in database."""
|
# get buffer from minio
|
||||||
buffer = BytesIO()
|
buffer = get(
|
||||||
image.save(buffer, format='JPEG')
|
client=minio_client,
|
||||||
return buffer.getvalue()
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
# convert data to image
|
||||||
|
im = Image.open(buffer)
|
||||||
|
return im
|
||||||
|
|
||||||
@field_validator('image', mode='before')
|
def webencoded_image(self, minio_client: Minio) -> str:
|
||||||
@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:
|
|
||||||
"""Convert image to be displayed on webpage."""
|
"""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
|
# convert images to bytes string
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
self.image.save(buffer, format='png')
|
image.save(buffer, format='png')
|
||||||
img_enc = b64encode(buffer.getvalue()).decode('utf-8')
|
img_enc = b64encode(buffer.getvalue()).decode('utf-8')
|
||||||
return f"data:image/png;base64, {img_enc}"
|
return f"data:image/png;base64, {img_enc}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user