-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_captioner.py
87 lines (75 loc) · 3.64 KB
/
image_captioner.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import base64
import dashscope
import comfy.utils
import numpy as np
from PIL import Image
from io import BytesIO
from http import HTTPStatus
class DashscopeConfig:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"api_key": ("STRING", {"default": ""})
}
}
RETURN_TYPES = ()
FUNCTION = "set_api_key"
CATEGORY = "Config"
def set_api_key(self, api_key):
os.environ['DASHSCOPE_API_KEY'] = api_key
return {}
class ImageCaptioner:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"user_prompt": ("STRING", {"default": "Please describe this image in 50 to 70 words.", "multiline": True}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "generate_image_captions"
CATEGORY = "Image Processing"
def post_process_prompt(self, raw_prompt):
tags = [tag.strip().lower() for tag in raw_prompt.split(',') if tag.strip()]
tags = ['_'.join(tag.split()) for tag in tags]
seen = set()
unique_tags = [tag for tag in tags if not (tag in seen or seen.add(tag))]
final_tags = unique_tags[:70]
return ', '.join(final_tags)
def generate_image_captions(self, image, user_prompt):
dashscope.api_key = os.environ.get("DASHSCOPE_API_KEY")
image = Image.fromarray((image.numpy() * 255).astype(np.uint8)[0])
with BytesIO() as output:
image.save(output, format="PNG")
image_bytes = output.getvalue()
base64_image = base64.b64encode(image_bytes).decode("utf-8")
image_url = f"data:image/png;base64,{base64_image}"
messages = [
{
"role": "system",
"content": "As an AI image tagging expert, please provide precise tags for these images to enhance CLIP model's understanding of the content. Employ succinct keywords or phrases, steering clear of elaborate sentences and extraneous conjunctions. Prioritize the tags by relevance. Your tags should capture key elements such as the main subject, setting, artistic style, composition, image quality, color tone, filter, and camera specifications, and any other tags crucial for the image. When tagging photos of people, include specific details like gender, nationality, attire, actions, pose, expressions, accessories, makeup, composition type, age, etc. For other image categories, apply appropriate and common descriptive tags as well. Recognize and tag any celebrities, well-known landmark or IPs if clearly featured in the image. Your tags should be accurate, non-duplicative, and within a 20-75 word count range. These tags will use for image re-creation, so the closer the resemblance to the original image, the better the tag quality. Tags should be comma-separated. Exceptional tagging will be rewarded with $10 per image."
},
{
"role": "user",
"content": [
{"image": f"{image_url}"},
{"text": f"{user_prompt}"}
]
}
]
response = dashscope.MultiModalConversation.call(
model="qwen-vl-plus",
messages=messages
)
if response.status_code == HTTPStatus.OK:
raw_prompt = response.output.choices[0].message.content
processed_prompt = self.post_process_prompt(raw_prompt)
return (processed_prompt,)
else:
print(f"Error: {response.code} - {response.message}")
return ("Error generating captions.",)