updated database classes
This commit is contained in:
Regular → Executable
+1
@@ -1,3 +1,4 @@
|
|||||||
|
"""Database classes module content."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from .dataset import Dataset
|
from .dataset import Dataset
|
||||||
|
|||||||
Regular → Executable
+15
@@ -1,14 +1,18 @@
|
|||||||
|
"""Definition of database Dataset class."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
import random
|
import random
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import UTC
|
from datetime import UTC
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
|
from pymongo.collection import Collection
|
||||||
|
|
||||||
|
|
||||||
class Dataset(BaseModel):
|
class Dataset(BaseModel):
|
||||||
|
"""Database Dataset model."""
|
||||||
create_time: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
create_time: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
||||||
train_names: list[str]
|
train_names: list[str]
|
||||||
test_names: list[str]
|
test_names: list[str]
|
||||||
@@ -53,4 +57,15 @@ class Dataset(BaseModel):
|
|||||||
test_names=test_name_list,
|
test_names=test_name_list,
|
||||||
validation_names=validation_name_list,
|
validation_names=validation_name_list,
|
||||||
)
|
)
|
||||||
|
logging.debug('finished')
|
||||||
return dataset
|
return dataset
|
||||||
|
|
||||||
|
def save(
|
||||||
|
self,
|
||||||
|
collection: Collection,
|
||||||
|
) -> None:
|
||||||
|
"""Save dataset to database."""
|
||||||
|
res = collection.insert_one(
|
||||||
|
document=self.model_dump(),
|
||||||
|
)
|
||||||
|
logging.debug('inserted document: %s', res)
|
||||||
|
|||||||
Regular → Executable
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
|
"""Definition of database exception."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
class NoDocumentFoundException(Exception):
|
class NoDocumentFoundException(Exception):
|
||||||
pass
|
"""Database exception for when no documents are found."""
|
||||||
|
|||||||
Regular → Executable
+9
-2
@@ -1,3 +1,4 @@
|
|||||||
|
"""Definition of VisualCommunication model."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@@ -15,12 +16,15 @@ from core.dto import ModelData
|
|||||||
|
|
||||||
|
|
||||||
class VisualCommunication(BaseModel):
|
class VisualCommunication(BaseModel):
|
||||||
|
"""Visual communication model."""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
image: Image.Image
|
image: Image.Image
|
||||||
annotation: ModelData | None = None
|
annotation: ModelData | None = None
|
||||||
prediction: ModelData | None = None
|
prediction: ModelData | None = None
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
"""BaseModel configuration."""
|
||||||
arbitrary_types_allowed = True
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -38,12 +42,14 @@ class VisualCommunication(BaseModel):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def decode_image(cls, content: str) -> Image.Image:
|
def decode_image(cls, content: str) -> Image.Image:
|
||||||
"""Decode image."""
|
"""Extract image from webencoded content."""
|
||||||
_, 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')
|
@field_serializer('image')
|
||||||
def serialize_image(image: Image.Image) -> bytes: # type: ignore
|
@classmethod
|
||||||
|
def serialize_image(cls, image: Image.Image) -> bytes: # type: ignore
|
||||||
|
"""Convert image to bytes for storage in database."""
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
image.save(buffer, format='JPEG')
|
image.save(buffer, format='JPEG')
|
||||||
return buffer.getvalue()
|
return buffer.getvalue()
|
||||||
@@ -54,6 +60,7 @@ class VisualCommunication(BaseModel):
|
|||||||
cls,
|
cls,
|
||||||
image: Image.Image | BytesIO | bytes,
|
image: Image.Image | BytesIO | bytes,
|
||||||
) -> Image.Image:
|
) -> Image.Image:
|
||||||
|
"""Convert bytes input from database into image."""
|
||||||
if isinstance(image, bytes):
|
if isinstance(image, bytes):
|
||||||
image = BytesIO(image)
|
image = BytesIO(image)
|
||||||
if isinstance(image, BytesIO):
|
if isinstance(image, BytesIO):
|
||||||
|
|||||||
Reference in New Issue
Block a user