Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff26198721 | ||
|
|
3b0fbaa851 | ||
|
|
68417a7afc | ||
|
|
a3b95f88ba |
+18
-12
@@ -1,5 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from pydantic import BaseModel, field_validator
|
from pydantic import BaseModel, field_validator, field_serializer
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -36,10 +36,13 @@ class ModelOutputs(BaseModel):
|
|||||||
|
|
||||||
class VisualCommunication(BaseModel):
|
class VisualCommunication(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
image: bytes
|
image: Image.Image | BytesIO | bytes
|
||||||
annotation: ModelOutputs | None = None
|
annotation: ModelOutputs | None = None
|
||||||
prediction: ModelOutputs | None = None
|
prediction: ModelOutputs | None = None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def classname(cls) -> str:
|
def classname(cls) -> str:
|
||||||
"""Return classname."""
|
"""Return classname."""
|
||||||
@@ -50,20 +53,23 @@ class VisualCommunication(BaseModel):
|
|||||||
"""Instantiate from file."""
|
"""Instantiate from file."""
|
||||||
name = path.stem
|
name = path.stem
|
||||||
image = Image.open(path)
|
image = Image.open(path)
|
||||||
|
image.load()
|
||||||
return VisualCommunication(name=name, image=image)
|
return VisualCommunication(name=name, image=image)
|
||||||
|
|
||||||
|
@field_serializer("image")
|
||||||
|
def serialize_image(image: Image.Image) -> bytes:
|
||||||
|
buffer = BytesIO()
|
||||||
|
image.save(buffer, format="JPEG")
|
||||||
|
return buffer.getvalue()
|
||||||
|
|
||||||
@field_validator("image", mode="before")
|
@field_validator("image", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def convert_to_bytes(cls, raw: Image.Image | BytesIO | bytes) -> bytes:
|
def convert_to_image(cls, image: Image.Image | BytesIO | bytes) -> Image.Image:
|
||||||
if isinstance(raw, Image.Image):
|
if isinstance(image, bytes):
|
||||||
raw = raw.tobytes()
|
image = BytesIO(image)
|
||||||
if isinstance(raw, BytesIO):
|
if isinstance(image, BytesIO):
|
||||||
raw = raw.read()
|
image = Image.open(image)
|
||||||
return raw
|
return image
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"{self.classname()}(name='{self.name}')"
|
return f"{self.classname()}(name='{self.name}')"
|
||||||
|
|
||||||
@property
|
|
||||||
def image(self) -> Image.Image:
|
|
||||||
return Image.open(BytesIO(self.image))
|
|
||||||
|
|||||||
@@ -17,6 +17,5 @@ def connect():
|
|||||||
client = MongoClient(os.getenv("MONGO_HOST"))
|
client = MongoClient(os.getenv("MONGO_HOST"))
|
||||||
db = client[os.getenv("MONGO_DB")]
|
db = client[os.getenv("MONGO_DB")]
|
||||||
collection = db[os.getenv("MONGO_COLLECTION")]
|
collection = db[os.getenv("MONGO_COLLECTION")]
|
||||||
|
collection.create_index("name", unique=True)
|
||||||
return collection, db, client
|
return collection, db, client
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from pymongo import MongoClient
|
||||||
|
from typing import List
|
||||||
|
import os
|
||||||
|
|
||||||
|
from src.database import VisualCommunication, connect
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# prepare env vars
|
||||||
|
env_path = Path(__file__).parent.parent / "local.env"
|
||||||
|
assert env_path.exists()
|
||||||
|
load_dotenv(env_path)
|
||||||
|
os.environ["MONGO_HOST"] = "localhost"
|
||||||
|
# connect to database
|
||||||
|
collection, db, client = connect()
|
||||||
|
print(client.server_info())
|
||||||
|
# download images
|
||||||
|
data = None
|
||||||
|
for data in collection.find().limit(3):
|
||||||
|
if data is None:
|
||||||
|
print("no document found")
|
||||||
|
break
|
||||||
|
vis_com: VisualCommunication = VisualCommunication.model_validate(data)
|
||||||
|
print(repr(vis_com))
|
||||||
|
if data is not None:
|
||||||
|
print(vis_com.image)
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from pymongo import MongoClient
|
from pymongo.errors import DuplicateKeyError
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from src.database import VisualCommunication, connect
|
from src.database import VisualCommunication, connect
|
||||||
@@ -15,12 +15,19 @@ if __name__ == "__main__":
|
|||||||
vis_com_list = [VisualCommunication.from_file(path) for path in img_path_list]
|
vis_com_list = [VisualCommunication.from_file(path) for path in img_path_list]
|
||||||
for vis_com in vis_com_list:
|
for vis_com in vis_com_list:
|
||||||
print(repr(vis_com))
|
print(repr(vis_com))
|
||||||
# upload images to database
|
# prepare env vars
|
||||||
env_path = test_dir.parent / "mongodb.env"
|
env_path = test_dir.parent / "local.env"
|
||||||
assert env_path.exists()
|
assert env_path.exists()
|
||||||
load_dotenv(env_path)
|
load_dotenv(env_path)
|
||||||
|
os.environ["MONGO_HOST"] = "localhost"
|
||||||
|
# connect to database
|
||||||
collection, db, client = connect()
|
collection, db, client = connect()
|
||||||
print(client.server_info())
|
print(client.server_info())
|
||||||
|
# upload images
|
||||||
for vis_com in vis_com_list:
|
for vis_com in vis_com_list:
|
||||||
result = collection.insert_one(vis_com.model_dump_json())
|
try:
|
||||||
|
result = collection.insert_one(vis_com.model_dump())
|
||||||
|
except DuplicateKeyError as exc:
|
||||||
|
print("ignoring:\n", exc)
|
||||||
|
else:
|
||||||
print(f"inserted document: {result}")
|
print(f"inserted document: {result}")
|
||||||
Reference in New Issue
Block a user