-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processing.py
More file actions
38 lines (29 loc) · 1.21 KB
/
image_processing.py
File metadata and controls
38 lines (29 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import Any, Dict
from db import save_tags_to_db
from logging_config import logger
from model import generate_caption_for_image
from tags import generate_tags_from_caption
def process_images_from_db(images: list, processor: Any, model: Any) -> Dict[str, Dict[str, Any]]:
try:
logger.info(f"Found {len(images)} images in the database.")
except Exception:
logger.exception("Failed to fetch images from the database.")
return {}
captions_and_tags = {}
for image in images:
image_id, image_data, created_at = image
try:
caption = generate_caption_for_image(image_data, image_id, processor, model)
except Exception:
logger.exception(f"Failed to generate caption for image: {image_id}")
continue
if not caption:
logger.warning(f"No caption generated for image: {image_id}")
continue
tags = generate_tags_from_caption(caption)
captions_and_tags[image_id] = {"caption": caption, "tags": tags}
try:
save_tags_to_db(image_id, tags)
except Exception:
logger.exception(f"Failed to save tags for image: {image_id}")
return captions_and_tags