-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdanbooru_tagger.py
159 lines (127 loc) · 5.43 KB
/
danbooru_tagger.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import io
import base64
import onnxruntime as ort
import csv
from PIL import Image
import numpy as np
import requests
import shutil
DEFAULT_MODEL = "wd-v1-4-moat-tagger-v2"
LIST_MODEL = ("wd-v1-4-moat-tagger-v2",
"wd-v1-4-convnext-tagger-v2", "wd-v1-4-convnext-tagger",
"wd-v1-4-convnextv2-tagger-v2", "wd-v1-4-vit-tagger-v2")
def download_file(url, dst):
try:
with requests.get(url, stream=True) as response:
if response.status_code == 200:
with open(dst, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
return True
else:
print(
f"Failed to download file from {url}. Status code: {response.status_code}")
return False
except Exception as e:
print(f"An error occurred: {e}")
return False
def create_folder_if_not_exists(foldersrc):
if not os.path.exists(foldersrc):
os.makedirs(foldersrc)
def convert_src_to_imagedata(img_path, quality=100):
img = Image.open(img_path)
buf = io.BytesIO()
img.save(buf, format='png', quality=100)
return base64.b64encode(buf.getvalue()).decode("utf-8")
class DanbooruTagger():
def __init__(self, models_dir):
self.models_dir = models_dir
self.options = {
"model_name": DEFAULT_MODEL,
"threshold": 0.35,
"character_threshold": 0.85,
"replace_underscore": True,
"trailing_comma": False,
"exclude_tags": ""
}
def get_installed_models(self):
create_folder_if_not_exists(self.models_dir)
return list(filter(lambda x: x.endswith(".onnx"), os.listdir(self.models_dir)))
def tag(self, image):
model_name = self.options['model_name']
threshold = self.options['threshold']
character_threshold = self.options['character_threshold']
replace_underscore = self.options['replace_underscore']
trailing_comma = self.options['trailing_comma']
exclude_tags = self.options['exclude_tags']
if model_name.endswith(".onnx"):
model_name = model_name[0:-5]
installed = self.get_installed_models()
if not any(model_name + ".onnx" in s for s in installed):
print("model not installed")
return
name = os.path.join(self.models_dir, model_name + ".onnx")
model = ort.InferenceSession(
name, providers=ort.get_available_providers())
input = model.get_inputs()[0]
height = input.shape[1]
# Reduce to max size and pad with white
ratio = float(height) / max(image.size)
new_size = tuple([int(x * ratio) for x in image.size])
image = image.resize(new_size, Image.LANCZOS)
square = Image.new("RGB", (height, height), (255, 255, 255))
square.paste(
image, ((height - new_size[0]) // 2, (height - new_size[1]) // 2))
image = np.array(square).astype(np.float32)
image = image[:, :, ::-1] # RGB -> BGR
image = np.expand_dims(image, 0)
# Read all tags from csv and locate start of each category
tags = []
general_index = None
character_index = None
with open(os.path.join(self.models_dir, model_name + ".csv")) as f:
reader = csv.reader(f)
next(reader)
for row in reader:
if general_index is None and row[2] == "0":
general_index = reader.line_num - 2
elif character_index is None and row[2] == "4":
character_index = reader.line_num - 2
if replace_underscore:
tags.append(row[1].replace("_", " "))
else:
tags.append(row[1])
label_name = model.get_outputs()[0].name
probs = model.run([label_name], {input.name: image})[0]
result = list(zip(tags, probs[0]))
# rating = max(result[:general_index], key=lambda x: x[1])
general = [item for item in result[general_index:character_index]
if item[1] > threshold]
character = [item for item in result[character_index:]
if item[1] > character_threshold]
all = character + general
remove = [s.strip() for s in exclude_tags.lower().split(",")]
all = [tag for tag in all if tag[0] not in remove]
res = ("" if trailing_comma else ", ").join((item[0].replace(
"(", "\\(").replace(")", "\\)") + (", " if trailing_comma else "") for item in all))
return res
def download_model(self, model):
installed = self.get_installed_models()
if any(model + ".onnx" in s for s in installed):
print("model already installed")
return True
url = f"https://huggingface.co/SmilingWolf/{model}/resolve/main/"
is_success = download_file(
f"{url}model.onnx",
os.path.join(self.models_dir, f"{model}.onnx"))
is_success = is_success and download_file(
f"{url}selected_tags.csv",
os.path.join(self.models_dir, f"{model}.csv"))
return is_success
if __name__ == '__main__':
dt = DanbooruTagger(r'D:\Dev\Workspace\Python\NAI-Auto-Generator\models')
is_success = dt.download_model("wd-v1-4-moat-tagger-v2")
print(is_success)
result = dt.tag(Image.open("no_image.png"))
print(result)