Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# Face Detection-Based AI Automation of Lab Tests
|
| 2 |
-
# Gradio App
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import cv2
|
|
@@ -66,100 +66,64 @@ def generate_flags_extended(params):
|
|
| 66 |
|
| 67 |
def analyze_face(image):
|
| 68 |
if image is None:
|
| 69 |
-
return
|
| 70 |
|
| 71 |
frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 72 |
result = face_mesh.process(frame_rgb)
|
| 73 |
|
| 74 |
-
if result.multi_face_landmarks:
|
| 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 |
-
demo = gr.
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
""
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
# 🧠 Face-Based AI Lab Test Inference
|
| 129 |
-
Upload a clear face image to simulate categorized lab reports with visual grouping.
|
| 130 |
-
""")
|
| 131 |
-
|
| 132 |
-
with gr.Row():
|
| 133 |
-
with gr.Column(scale=1):
|
| 134 |
-
image_input = gr.Image(type="numpy", label="📸 Upload a Face Image")
|
| 135 |
-
submit_btn = gr.Button("🔍 Analyze Now")
|
| 136 |
-
with gr.Column(scale=2):
|
| 137 |
-
accordion_output = gr.Accordion("📂 Diagnostic Summary", open=True)
|
| 138 |
-
with accordion_output:
|
| 139 |
-
result_html = gr.HighlightedText(label="📊 Grouped Report", combine_adjacent=True)
|
| 140 |
-
result_image = gr.Image(label="🧍 Annotated Face Scan")
|
| 141 |
-
|
| 142 |
-
def format_report(sections):
|
| 143 |
-
lines = []
|
| 144 |
-
for title, values in sections.items():
|
| 145 |
-
lines.append((f"{title}",))
|
| 146 |
-
for item in values:
|
| 147 |
-
lines.append((f" - {item}",))
|
| 148 |
-
return lines
|
| 149 |
-
|
| 150 |
-
submit_btn.click(
|
| 151 |
-
fn=analyze_face,
|
| 152 |
-
inputs=image_input,
|
| 153 |
-
outputs=[result_html, result_image],
|
| 154 |
-
preprocess=False,
|
| 155 |
-
postprocess=False,
|
| 156 |
-
_js="(data) => [data]"
|
| 157 |
-
).then(
|
| 158 |
-
fn=format_report,
|
| 159 |
-
inputs=None,
|
| 160 |
-
outputs=result_html
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
gr.Markdown("---\n✅ Optimized for Mobile · Risk Indicators: 🔻 Low, 🔺 High, ✅ Normal")
|
| 164 |
|
| 165 |
demo.launch()
|
|
|
|
| 1 |
# Face Detection-Based AI Automation of Lab Tests
|
| 2 |
+
# Gradio App - Cleaned for Hugging Face Compatibility
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import cv2
|
|
|
|
| 66 |
|
| 67 |
def analyze_face(image):
|
| 68 |
if image is None:
|
| 69 |
+
return "⚠️ Error: No image provided.", None
|
| 70 |
|
| 71 |
frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 72 |
result = face_mesh.process(frame_rgb)
|
| 73 |
|
| 74 |
+
if not result.multi_face_landmarks:
|
| 75 |
+
return "⚠️ Error: Face not detected.", None
|
| 76 |
+
|
| 77 |
+
landmarks = result.multi_face_landmarks[0].landmark
|
| 78 |
+
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
| 79 |
+
spo2, rr = estimate_spo2_rr(heart_rate)
|
| 80 |
+
|
| 81 |
+
hb, wbc, platelets = 12.3, 6.4, 210
|
| 82 |
+
iron, ferritin, tibc = 55, 45, 340
|
| 83 |
+
bilirubin, creatinine = 1.5, 1.3
|
| 84 |
+
tsh, cortisol = 2.5, 18
|
| 85 |
+
fbs, hba1c = 120, 6.2
|
| 86 |
+
|
| 87 |
+
flags = generate_flags_extended([hb, wbc, platelets, iron, ferritin, tibc, bilirubin, creatinine, tsh, cortisol, fbs, hba1c])
|
| 88 |
+
|
| 89 |
+
report = f"""
|
| 90 |
+
### 🩸 Hematology
|
| 91 |
+
- Hemoglobin (Hb): {hb} g/dL - {get_risk_color(hb, (13.5, 17.5))}
|
| 92 |
+
- WBC Count: {wbc} x10^3/uL - {get_risk_color(wbc, (4.0, 11.0))}
|
| 93 |
+
- Platelet Count: {platelets} x10^3/uL - {get_risk_color(platelets, (150, 450))}
|
| 94 |
+
|
| 95 |
+
### 🧬 Iron & Liver Panel
|
| 96 |
+
- Iron: {iron} µg/dL - {get_risk_color(iron, (60, 170))}
|
| 97 |
+
- Ferritin: {ferritin} ng/mL - {get_risk_color(ferritin, (30, 300))}
|
| 98 |
+
- TIBC: {tibc} µg/dL - {get_risk_color(tibc, (250, 400))}
|
| 99 |
+
- Bilirubin: {bilirubin} mg/dL - {get_risk_color(bilirubin, (0.3, 1.2))}
|
| 100 |
+
|
| 101 |
+
### 🧪 Kidney, Thyroid & Stress
|
| 102 |
+
- Creatinine: {creatinine} mg/dL - {get_risk_color(creatinine, (0.6, 1.2))}
|
| 103 |
+
- TSH: {tsh} µIU/mL - {get_risk_color(tsh, (0.4, 4.0))}
|
| 104 |
+
- Cortisol: {cortisol} µg/dL - {get_risk_color(cortisol, (5, 25))}
|
| 105 |
+
|
| 106 |
+
### 🧁 Metabolic Panel
|
| 107 |
+
- Fasting Blood Sugar: {fbs} mg/dL - {get_risk_color(fbs, (70, 110))}
|
| 108 |
+
- HbA1c: {hba1c}% - {get_risk_color(hba1c, (4.0, 5.7))}
|
| 109 |
+
|
| 110 |
+
### ❤️ Vital Signs
|
| 111 |
+
- SpO2: {spo2}% - {get_risk_color(spo2, (95, 100))}
|
| 112 |
+
- Heart Rate: {heart_rate} bpm - {get_risk_color(heart_rate, (60, 100))}
|
| 113 |
+
- Respiratory Rate: {rr} breaths/min - {get_risk_color(rr, (12, 20))}
|
| 114 |
+
- Blood Pressure: Low (simulated)
|
| 115 |
+
|
| 116 |
+
### ⚠️ Risk Flags
|
| 117 |
+
- {"\n- ".join(flags)}
|
| 118 |
+
"""
|
| 119 |
+
return report, frame_rgb
|
| 120 |
+
|
| 121 |
+
demo = gr.Interface(
|
| 122 |
+
fn=analyze_face,
|
| 123 |
+
inputs=gr.Image(type="numpy", label="📸 Upload Face Image"),
|
| 124 |
+
outputs=[gr.Markdown(label="🧪 Diagnostic Report"), gr.Image(label="🧍 Annotated Face")],
|
| 125 |
+
title="Face-Based AI Lab Test Inference",
|
| 126 |
+
description="Upload a clear face image to get lab test estimates and vital signs via facial analysis."
|
| 127 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
demo.launch()
|