-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
95 lines (74 loc) · 3.14 KB
/
app.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
import gradio as gr
from os import listdir
from dominant_color import get_dominant_colors
NUM_CNT = 8
NUM_HLS = 4
NUM_RAW = 12
cnt_colors = [gr.ColorPicker(visible=False) for _ in range(NUM_CNT)]
cnt_pcts = [gr.Textbox(visible=False) for _ in range(NUM_CNT)]
hls_colors = [gr.ColorPicker(visible=False) for _ in range(NUM_HLS)]
hls_pcts = [gr.Textbox(visible=False) for _ in range(NUM_HLS)]
raw_colors = [gr.ColorPicker(visible=False) for _ in range(NUM_RAW)]
raw_pcts = [gr.Textbox(visible=False) for _ in range(NUM_RAW)]
my_examples = [
[f"./imgs/{fname}"] for fname in listdir("./imgs") if fname.endswith("jpg")
]
def dom_col(img_in):
palette_cnt, palette_hls, palette_pcts, hex_pcts, img_out = get_dominant_colors(img_in, k=NUM_HLS)
def get_pct_md(pct):
return f"### {round(pct*100, 2)}%"
def get_hex_pct_md(cp):
return get_pct_md(palette_pcts.get(cp, 0))
cnt_colors = [gr.ColorPicker(h) for h in palette_cnt]
cnt_pcts = [gr.Textbox(get_hex_pct_md(h)) for h in palette_cnt]
hls_colors = [gr.ColorPicker(h) for h in palette_hls]
hls_pcts = [gr.Textbox(get_hex_pct_md(h)) for h in palette_hls]
raw_colors = [gr.ColorPicker(hp[0]) for hp in hex_pcts[:NUM_RAW]]
raw_pcts = [gr.Textbox(get_pct_md(hp[1])) for hp in hex_pcts[:NUM_RAW]]
return cnt_colors + cnt_pcts + hls_colors + hls_pcts + raw_colors + raw_pcts + [img_out]
def get_color(cp):
return cp
def get_color_md(cp):
return f"### {cp}"
def get_md(x):
return x
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("# Dominant color calculator")
gr.Interface(
dom_col,
inputs=gr.Image(type="pil"),
outputs=[*cnt_colors, *cnt_pcts,
*hls_colors, *hls_pcts,
*raw_colors, *raw_pcts,
gr.Image(type="pil", label="img_out")],
cache_examples=True,
examples=my_examples,
allow_flagging="never",
fill_width=True
)
with gr.Column(scale=0, variant="default"):
with gr.Column(scale=0, variant="panel"):
gr.Markdown("# By Count")
for i,o in enumerate(cnt_colors):
with gr.Row():
gr.ColorPicker(get_color, inputs=[o], show_label=False, scale=0, container=False)
gr.Markdown(get_color_md, inputs=[o], show_label=False)
gr.Markdown(get_md, inputs=[cnt_pcts[i]], show_label=False)
with gr.Column(scale=0, variant="panel"):
gr.Markdown("# By HLS")
for i,o in enumerate(hls_colors):
with gr.Row():
gr.ColorPicker(get_color, inputs=[o], show_label=False, scale=0, container=False)
gr.Markdown(get_color_md, inputs=[o], show_label=False)
gr.Markdown(get_md, inputs=[hls_pcts[i]], show_label=False)
with gr.Column(scale=0, variant="panel"):
gr.Markdown("# By Raw Count")
for i,o in enumerate(raw_colors):
with gr.Row():
gr.ColorPicker(get_color, inputs=[o], show_label=False, scale=0, container=False)
gr.Markdown(get_color_md, inputs=[o], show_label=False)
gr.Markdown(get_md, inputs=[raw_pcts[i]], show_label=False)
if __name__ == "__main__":
demo.launch()