Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# Face Detection-Based AI Automation of Lab Tests
|
2 |
-
#
|
3 |
|
4 |
import gradio as gr
|
5 |
import cv2
|
@@ -28,51 +28,20 @@ def estimate_spo2_rr(heart_rate):
|
|
28 |
def get_risk_color(value, normal_range):
|
29 |
low, high = normal_range
|
30 |
if value < low:
|
31 |
-
return "🔻 LOW"
|
32 |
elif value > high:
|
33 |
-
return "🔺 HIGH"
|
34 |
else:
|
35 |
-
return "✅ Normal"
|
36 |
-
|
37 |
-
def generate_flags_extended(params):
|
38 |
-
hb, wbc, platelets, iron, ferritin, tibc, bilirubin, creatinine, tsh, cortisol, fbs, hba1c = params
|
39 |
-
flags = []
|
40 |
-
if hb < 13.5:
|
41 |
-
flags.append("Hemoglobin Low - Possible Anemia")
|
42 |
-
if wbc < 4.0 or wbc > 11.0:
|
43 |
-
flags.append("Abnormal WBC Count - Possible Infection")
|
44 |
-
if platelets < 150:
|
45 |
-
flags.append("Platelet Drop Risk - Bruising Possible")
|
46 |
-
if iron < 60:
|
47 |
-
flags.append("Iron Deficiency Detected")
|
48 |
-
if ferritin < 30:
|
49 |
-
flags.append("Low Ferritin - Iron Store Low")
|
50 |
-
if tibc > 400:
|
51 |
-
flags.append("High TIBC - Iron Absorption Issue")
|
52 |
-
if bilirubin > 1.2:
|
53 |
-
flags.append("Jaundice Detected - Elevated Bilirubin")
|
54 |
-
if creatinine > 1.2:
|
55 |
-
flags.append("Kidney Function Concern - High Creatinine")
|
56 |
-
if tsh < 0.4 or tsh > 4.0:
|
57 |
-
flags.append("Thyroid Imbalance - Check TSH")
|
58 |
-
if cortisol < 5 or cortisol > 25:
|
59 |
-
flags.append("Stress Hormone Abnormality - Cortisol")
|
60 |
-
if fbs > 110:
|
61 |
-
flags.append("High Fasting Blood Sugar")
|
62 |
-
if hba1c > 5.7:
|
63 |
-
flags.append("Elevated HbA1c - Diabetes Risk")
|
64 |
-
flags.append("Mood / Stress analysis requires separate behavioral model")
|
65 |
-
return flags
|
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 not result.multi_face_landmarks:
|
75 |
-
return "
|
76 |
|
77 |
landmarks = result.multi_face_landmarks[0].landmark
|
78 |
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
@@ -84,46 +53,71 @@ def analyze_face(image):
|
|
84 |
tsh, cortisol = 2.5, 18
|
85 |
fbs, hba1c = 120, 6.2
|
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 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
demo.launch()
|
|
|
1 |
# Face Detection-Based AI Automation of Lab Tests
|
2 |
+
# Redesigned UI using Gradio Blocks + HTML Cards
|
3 |
|
4 |
import gradio as gr
|
5 |
import cv2
|
|
|
28 |
def get_risk_color(value, normal_range):
|
29 |
low, high = normal_range
|
30 |
if value < low:
|
31 |
+
return ("🔻 LOW", "#FFCCCC") # Red background
|
32 |
elif value > high:
|
33 |
+
return ("🔺 HIGH", "#FFE680") # Yellow background
|
34 |
else:
|
35 |
+
return ("✅ Normal", "#CCFFCC") # Green background
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def analyze_face(image):
|
38 |
if image is None:
|
39 |
+
return [], None
|
40 |
|
41 |
frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
42 |
result = face_mesh.process(frame_rgb)
|
|
|
43 |
if not result.multi_face_landmarks:
|
44 |
+
return [["Face not detected", "#FFDDDD"]], None
|
45 |
|
46 |
landmarks = result.multi_face_landmarks[0].landmark
|
47 |
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
|
|
53 |
tsh, cortisol = 2.5, 18
|
54 |
fbs, hba1c = 120, 6.2
|
55 |
|
56 |
+
def section(title, items):
|
57 |
+
html = f'<div style="padding:10px;border:1px solid #ccc;border-radius:8px;margin-bottom:10px;background:#f8f9fa;">'
|
58 |
+
html += f'<h4 style="margin:0 0 10px 0">{title}</h4>'
|
59 |
+
for label, val, rng in items:
|
60 |
+
status, bgcolor = get_risk_color(val, rng)
|
61 |
+
html += f'<div style="padding:6px;margin-bottom:4px;background:{bgcolor};border-radius:4px;">{label}: {val} - {status}</div>'
|
62 |
+
html += '</div>'
|
63 |
+
return html
|
64 |
+
|
65 |
+
cards = [
|
66 |
+
section("🩸 Hematology", [
|
67 |
+
("Hemoglobin", hb, (13.5, 17.5)),
|
68 |
+
("WBC Count", wbc, (4.0, 11.0)),
|
69 |
+
("Platelets", platelets, (150, 450))
|
70 |
+
]),
|
71 |
+
section("🧬 Iron & Liver Panel", [
|
72 |
+
("Iron", iron, (60, 170)),
|
73 |
+
("Ferritin", ferritin, (30, 300)),
|
74 |
+
("TIBC", tibc, (250, 400)),
|
75 |
+
("Bilirubin", bilirubin, (0.3, 1.2))
|
76 |
+
]),
|
77 |
+
section("🧪 Kidney, Thyroid & Stress", [
|
78 |
+
("Creatinine", creatinine, (0.6, 1.2)),
|
79 |
+
("TSH", tsh, (0.4, 4.0)),
|
80 |
+
("Cortisol", cortisol, (5, 25))
|
81 |
+
]),
|
82 |
+
section("🧁 Metabolic Panel", [
|
83 |
+
("Fasting Blood Sugar", fbs, (70, 110)),
|
84 |
+
("HbA1c", hba1c, (4.0, 5.7))
|
85 |
+
]),
|
86 |
+
section("❤️ Vital Signs", [
|
87 |
+
("SpO2", spo2, (95, 100)),
|
88 |
+
("Heart Rate", heart_rate, (60, 100)),
|
89 |
+
("Respiratory Rate", rr, (12, 20))
|
90 |
+
])
|
91 |
+
]
|
92 |
+
return cards, frame_rgb
|
93 |
+
|
94 |
+
# Gradio App Layout (Custom UI with Cards)
|
95 |
+
demo = gr.Blocks()
|
96 |
+
with demo:
|
97 |
+
gr.Markdown("""
|
98 |
+
# 🧠 Face-Based Lab Test AI Report
|
99 |
+
Upload a face photo to infer health diagnostics with AI-based visual markers.
|
100 |
+
""")
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column(scale=1):
|
104 |
+
image_input = gr.Image(type="numpy", label="📸 Upload Face Image")
|
105 |
+
submit_btn = gr.Button("🔍 Analyze")
|
106 |
+
with gr.Column(scale=2):
|
107 |
+
result_html = gr.HTML(label="🧪 Visual Diagnostic Cards")
|
108 |
+
result_image = gr.Image(label="📷 Face Scan Annotated")
|
109 |
+
|
110 |
+
def format_html(cards):
|
111 |
+
return "".join(cards)
|
112 |
+
|
113 |
+
submit_btn.click(
|
114 |
+
fn=analyze_face,
|
115 |
+
inputs=image_input,
|
116 |
+
outputs=[result_html, result_image]
|
117 |
+
).then(
|
118 |
+
fn=format_html,
|
119 |
+
inputs=None,
|
120 |
+
outputs=result_html
|
121 |
+
)
|
122 |
|
123 |
demo.launch()
|