Spaces:
Sleeping
Sleeping
narugo1992
commited on
Commit
·
0be6001
1
Parent(s):
b26b6ee
dev(narugo): add text export
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
|
|
1 |
from functools import lru_cache
|
2 |
-
from typing import List, Mapping
|
3 |
|
4 |
import gradio as gr
|
5 |
import numpy as np
|
@@ -48,27 +49,55 @@ def image_preprocess(image: Image.Image) -> np.ndarray:
|
|
48 |
return data.reshape((1, 512, 512, 3)) # B x H x W x C
|
49 |
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
tags = get_deepdanbooru_tags()
|
53 |
session = get_deepdanbooru_onnx()
|
54 |
input_name = session.get_inputs()[0].name
|
55 |
output_names = [output.name for output in session.get_outputs()]
|
56 |
|
57 |
result = session.run(output_names, {input_name: image_preprocess(image)})[0]
|
58 |
-
|
59 |
tag: float(score) for tag, score in zip(tags, result[0])
|
60 |
if score >= threshold
|
61 |
}
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
if __name__ == '__main__':
|
65 |
interface = gr.Interface(
|
66 |
image_to_deepdanbooru_tags,
|
67 |
inputs=[
|
68 |
gr.Image(type='pil', label='Original Image'),
|
69 |
-
gr.Slider(0.0, 1.0, 0.5, label='Tagging Confidence Threshold')
|
|
|
|
|
|
|
|
|
|
|
70 |
outputs=[
|
71 |
-
gr.
|
|
|
72 |
],
|
73 |
interpretation="default"
|
74 |
|
|
|
1 |
+
import re
|
2 |
from functools import lru_cache
|
3 |
+
from typing import List, Mapping, Tuple
|
4 |
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
|
|
49 |
return data.reshape((1, 512, 512, 3)) # B x H x W x C
|
50 |
|
51 |
|
52 |
+
RE_SPECIAL = re.compile(r'([\\()])')
|
53 |
+
|
54 |
+
|
55 |
+
def image_to_deepdanbooru_tags(image: Image.Image, threshold: float,
|
56 |
+
use_spaces: bool, use_escape: bool, include_ranks: bool, score_descend: bool) \
|
57 |
+
-> Tuple[Mapping[str, float], str]:
|
58 |
tags = get_deepdanbooru_tags()
|
59 |
session = get_deepdanbooru_onnx()
|
60 |
input_name = session.get_inputs()[0].name
|
61 |
output_names = [output.name for output in session.get_outputs()]
|
62 |
|
63 |
result = session.run(output_names, {input_name: image_preprocess(image)})[0]
|
64 |
+
filtered_tags = {
|
65 |
tag: float(score) for tag, score in zip(tags, result[0])
|
66 |
if score >= threshold
|
67 |
}
|
68 |
|
69 |
+
text_items = []
|
70 |
+
tags_pairs = filtered_tags.items()
|
71 |
+
if score_descend:
|
72 |
+
tags_pairs = sorted(tags_pairs, key=lambda x: (-x[1], x[0]))
|
73 |
+
for tag, score in tags_pairs:
|
74 |
+
tag_outformat = tag
|
75 |
+
if use_spaces:
|
76 |
+
tag_outformat = tag_outformat.replace('_', ' ')
|
77 |
+
if use_escape:
|
78 |
+
tag_outformat = re.sub(RE_SPECIAL, r'\\\1', tag_outformat)
|
79 |
+
if include_ranks:
|
80 |
+
tag_outformat = f"({tag_outformat}:{score:.3f})"
|
81 |
+
text_items.append(tag_outformat)
|
82 |
+
output_text = ', '.join(text_items)
|
83 |
+
|
84 |
+
return output_text, filtered_tags
|
85 |
+
|
86 |
|
87 |
if __name__ == '__main__':
|
88 |
interface = gr.Interface(
|
89 |
image_to_deepdanbooru_tags,
|
90 |
inputs=[
|
91 |
gr.Image(type='pil', label='Original Image'),
|
92 |
+
gr.Slider(0.0, 1.0, 0.5, label='Tagging Confidence Threshold'),
|
93 |
+
gr.Checkbox(value=False, label='Use Space Instead Of _'),
|
94 |
+
gr.Checkbox(value=True, label='Use Text Escape'),
|
95 |
+
gr.Checkbox(value=False, label='Keep Confidences'),
|
96 |
+
gr.Checkbox(value=True, label='Descend By Confidence'),
|
97 |
+
],
|
98 |
outputs=[
|
99 |
+
gr.TextArea(label='Exported Text'),
|
100 |
+
gr.Label(label='Tags'),
|
101 |
],
|
102 |
interpretation="default"
|
103 |
|