Update app.py
Browse files
app.py
CHANGED
@@ -59,18 +59,23 @@ def visualize(image, det_res, fer_res):
|
|
59 |
|
60 |
def summarize_emotions(fer_res):
|
61 |
"""
|
62 |
-
Maakt een
|
63 |
-
Bij meerdere gezichten wordt de meest voorkomende emotie uitgelicht.
|
64 |
"""
|
65 |
if not fer_res:
|
66 |
-
return "
|
67 |
|
68 |
names_en = [FacialExpressionRecog.getDesc(x) for x in fer_res]
|
69 |
names_nl = [to_dutch(n) for n in names_en]
|
70 |
counts = Counter(names_nl).most_common()
|
71 |
top, top_n = counts[0]
|
72 |
details = ", ".join([f"{name} ({n})" for name, n in counts])
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
def detect_expression(input_image):
|
76 |
image = cv.cvtColor(input_image, cv.COLOR_RGB2BGR)
|
@@ -104,7 +109,7 @@ else:
|
|
104 |
example_paths = []
|
105 |
|
106 |
example_list = [[p] for p in example_paths]
|
107 |
-
CACHE_EXAMPLES = bool(example_list)
|
108 |
# =============================================================
|
109 |
|
110 |
# Gradio Interface (NL)
|
@@ -124,16 +129,17 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
124 |
submit_btn = gr.Button("Verstuur", variant="primary")
|
125 |
clear_btn = gr.Button("Wissen")
|
126 |
with gr.Column(scale=1):
|
127 |
-
# Eerst de afbeelding, DAARONDER de emotie-uitslag
|
128 |
output_image = gr.Image(type="numpy", label="Resultaat gezichtsuitdrukking")
|
129 |
-
emotion_md = gr.Markdown(
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
# Output(s) leegmaken bij nieuwe upload
|
132 |
def _clear_output_on_new_image():
|
133 |
return None, "## **Nog geen resultaat**"
|
134 |
input_image.change(fn=_clear_output_on_new_image, outputs=[output_image, emotion_md])
|
135 |
|
136 |
-
# Knop-actie(s)
|
137 |
submit_btn.click(fn=detect_expression, inputs=input_image, outputs=[output_image, emotion_md])
|
138 |
clear_btn.click(fn=lambda: (None, None, "## **Nog geen resultaat**"),
|
139 |
outputs=[input_image, output_image, emotion_md])
|
@@ -141,10 +147,10 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
141 |
gr.Markdown("Klik op een voorbeeld om te testen.", elem_classes=["example"])
|
142 |
|
143 |
gr.Examples(
|
144 |
-
examples=example_list,
|
145 |
inputs=input_image,
|
146 |
-
outputs=[output_image, emotion_md],
|
147 |
-
fn=detect_expression,
|
148 |
examples_per_page=20,
|
149 |
cache_examples=CACHE_EXAMPLES
|
150 |
)
|
|
|
59 |
|
60 |
def summarize_emotions(fer_res):
|
61 |
"""
|
62 |
+
Maakt een HTML-tekst met een grote, vetgedrukte samenvatting (NL) in groen.
|
|
|
63 |
"""
|
64 |
if not fer_res:
|
65 |
+
return '<div style="color: green;">## <b>Geen gezicht gedetecteerd</b></div>'
|
66 |
|
67 |
names_en = [FacialExpressionRecog.getDesc(x) for x in fer_res]
|
68 |
names_nl = [to_dutch(n) for n in names_en]
|
69 |
counts = Counter(names_nl).most_common()
|
70 |
top, top_n = counts[0]
|
71 |
details = ", ".join([f"{name} ({n})" for name, n in counts])
|
72 |
+
|
73 |
+
return f'''
|
74 |
+
<div style="color: green;">
|
75 |
+
<h1><b>{top}</b></h1>
|
76 |
+
<p><i>Gedetecteerde emoties: {details}</i></p>
|
77 |
+
</div>
|
78 |
+
'''
|
79 |
|
80 |
def detect_expression(input_image):
|
81 |
image = cv.cvtColor(input_image, cv.COLOR_RGB2BGR)
|
|
|
109 |
example_paths = []
|
110 |
|
111 |
example_list = [[p] for p in example_paths]
|
112 |
+
CACHE_EXAMPLES = bool(example_list)
|
113 |
# =============================================================
|
114 |
|
115 |
# Gradio Interface (NL)
|
|
|
129 |
submit_btn = gr.Button("Verstuur", variant="primary")
|
130 |
clear_btn = gr.Button("Wissen")
|
131 |
with gr.Column(scale=1):
|
|
|
132 |
output_image = gr.Image(type="numpy", label="Resultaat gezichtsuitdrukking")
|
133 |
+
emotion_md = gr.Markdown(
|
134 |
+
value="## **Nog geen resultaat**",
|
135 |
+
elem_id="emotie-uitslag",
|
136 |
+
unsafe_allow_html=True
|
137 |
+
)
|
138 |
|
|
|
139 |
def _clear_output_on_new_image():
|
140 |
return None, "## **Nog geen resultaat**"
|
141 |
input_image.change(fn=_clear_output_on_new_image, outputs=[output_image, emotion_md])
|
142 |
|
|
|
143 |
submit_btn.click(fn=detect_expression, inputs=input_image, outputs=[output_image, emotion_md])
|
144 |
clear_btn.click(fn=lambda: (None, None, "## **Nog geen resultaat**"),
|
145 |
outputs=[input_image, output_image, emotion_md])
|
|
|
147 |
gr.Markdown("Klik op een voorbeeld om te testen.", elem_classes=["example"])
|
148 |
|
149 |
gr.Examples(
|
150 |
+
examples=example_list,
|
151 |
inputs=input_image,
|
152 |
+
outputs=[output_image, emotion_md],
|
153 |
+
fn=detect_expression,
|
154 |
examples_per_page=20,
|
155 |
cache_examples=CACHE_EXAMPLES
|
156 |
)
|