Commit ·
4935acb
1
Parent(s): a737654
Update UI to show demo images
Browse files
ui.py
CHANGED
|
@@ -1,39 +1,113 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from api import check_liveness
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def process_image(image):
|
| 6 |
if image is None:
|
| 7 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
result = check_liveness(image)
|
| 10 |
if "error" in result:
|
| 11 |
-
return
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
def create_interface():
|
| 16 |
-
with gr.Blocks(title="MiniAiLive Face Liveness Detection") as demo:
|
| 17 |
gr.Markdown(
|
| 18 |
"""
|
| 19 |
-
# MiniAiLive Face Liveness Detection
|
| 20 |
**3D Passive Face Liveness Detection (Face Anti-Spoofing)**
|
| 21 |
|
| 22 |
-
Upload a face image
|
| 23 |
"""
|
| 24 |
)
|
| 25 |
|
| 26 |
with gr.Row():
|
| 27 |
-
with gr.Column():
|
| 28 |
-
image_input = gr.Image(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
submit_btn.click(
|
| 34 |
fn=process_image,
|
| 35 |
inputs=image_input,
|
| 36 |
-
outputs=
|
| 37 |
)
|
| 38 |
|
| 39 |
return demo
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from api import check_liveness
|
| 3 |
|
| 4 |
+
EXAMPLE_IMAGES = [
|
| 5 |
+
["assets/1.jpg"],
|
| 6 |
+
["assets/2.jpg"],
|
| 7 |
+
["assets/3.jpg"],
|
| 8 |
+
["assets/4.jpg"],
|
| 9 |
+
["assets/5.jpg"],
|
| 10 |
+
["assets/6.jpg"],
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def _format_result_html(result: dict) -> str:
|
| 15 |
+
is_live = result.get("real", result.get("live", result.get("is_live", False)))
|
| 16 |
+
score = result.get("score", result.get("confidence", result.get("liveness_score", 0)))
|
| 17 |
+
message = result.get("message", result.get("msg", result.get("detail", "")))
|
| 18 |
+
|
| 19 |
+
if isinstance(is_live, str):
|
| 20 |
+
is_live = is_live.lower() in ("true", "1", "yes", "real", "live")
|
| 21 |
+
if isinstance(score, str):
|
| 22 |
+
try:
|
| 23 |
+
score = float(score)
|
| 24 |
+
except (ValueError, TypeError):
|
| 25 |
+
score = 0
|
| 26 |
+
if score > 1:
|
| 27 |
+
score = score / 100
|
| 28 |
+
|
| 29 |
+
pct = max(0, min(100, round(score * 100)))
|
| 30 |
+
badge_color = "#22c55e" if is_live else "#ef4444"
|
| 31 |
+
badge_text = "REAL / LIVE" if is_live else "SPOOF / FAKE"
|
| 32 |
+
icon = "✅" if is_live else "❌"
|
| 33 |
+
|
| 34 |
+
return f"""
|
| 35 |
+
<div style="padding: 20px; border-radius: 12px; background: #f8fafc; border: 1px solid #e2e8f0;">
|
| 36 |
+
<div style="display: flex; align-items: center; gap: 12px; margin-bottom: 16px;">
|
| 37 |
+
<span style="font-size: 28px;">{icon}</span>
|
| 38 |
+
<span style="font-size: 24px; font-weight: 700;">Liveness Result</span>
|
| 39 |
+
</div>
|
| 40 |
+
<div style="display: inline-block; padding: 8px 20px; border-radius: 20px;
|
| 41 |
+
background: {badge_color}; color: white; font-weight: 700;
|
| 42 |
+
font-size: 18px; margin-bottom: 16px;">
|
| 43 |
+
{badge_text}
|
| 44 |
+
</div>
|
| 45 |
+
<div style="margin-top: 14px;">
|
| 46 |
+
<div style="display: flex; justify-content: space-between; margin-bottom: 4px;">
|
| 47 |
+
<span style="font-weight: 600;">Confidence</span>
|
| 48 |
+
<span>{pct}%</span>
|
| 49 |
+
</div>
|
| 50 |
+
<div style="background: #e2e8f0; border-radius: 8px; height: 12px; overflow: hidden;">
|
| 51 |
+
<div style="width: {pct}%; height: 100%; background: {badge_color};
|
| 52 |
+
border-radius: 8px; transition: width 0.5s;"></div>
|
| 53 |
+
</div>
|
| 54 |
+
</div>
|
| 55 |
+
{f"<p style='margin-top: 12px; color: #64748b;'>{message}</p>" if message else ""}
|
| 56 |
+
</div>
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
|
| 60 |
def process_image(image):
|
| 61 |
if image is None:
|
| 62 |
+
return (
|
| 63 |
+
'<div style="padding:20px;color:#64748b;text-align:center;">'
|
| 64 |
+
"Upload or select an example image first</div>",
|
| 65 |
+
{"error": "Please upload an image first"},
|
| 66 |
+
)
|
| 67 |
|
| 68 |
result = check_liveness(image)
|
| 69 |
if "error" in result:
|
| 70 |
+
return (
|
| 71 |
+
f'<div style="padding:20px;color:#ef4444;text-align:center;">{result["error"]}</div>',
|
| 72 |
+
result,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
return _format_result_html(result), result
|
| 76 |
|
| 77 |
|
| 78 |
def create_interface():
|
| 79 |
+
with gr.Blocks(title="MiniAiLive Face Liveness Detection", theme=gr.themes.Soft()) as demo:
|
| 80 |
gr.Markdown(
|
| 81 |
"""
|
| 82 |
+
# 🥇 MiniAiLive Face Liveness Detection
|
| 83 |
**3D Passive Face Liveness Detection (Face Anti-Spoofing)**
|
| 84 |
|
| 85 |
+
Upload a face image or click an example below to check liveness.
|
| 86 |
"""
|
| 87 |
)
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
with gr.Column(scale=1):
|
| 91 |
+
image_input = gr.Image(
|
| 92 |
+
label="Upload Image",
|
| 93 |
+
type="numpy",
|
| 94 |
+
)
|
| 95 |
+
submit_btn = gr.Button("🔍 Check Liveness", variant="primary", size="lg")
|
| 96 |
+
|
| 97 |
+
gr.Examples(
|
| 98 |
+
examples=EXAMPLE_IMAGES,
|
| 99 |
+
inputs=image_input,
|
| 100 |
+
label="📸 Example Images",
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Column(scale=1):
|
| 104 |
+
result_html = gr.HTML(label="Result")
|
| 105 |
+
raw_json = gr.JSON(label="Raw Response", visible=True)
|
| 106 |
|
| 107 |
submit_btn.click(
|
| 108 |
fn=process_image,
|
| 109 |
inputs=image_input,
|
| 110 |
+
outputs=[result_html, raw_json],
|
| 111 |
)
|
| 112 |
|
| 113 |
return demo
|