Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
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,20 +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", "#FFCCCC")
|
32 |
elif value > high:
|
33 |
-
return ("🔺 HIGH", "#FFE680")
|
34 |
else:
|
35 |
-
return ("✅ Normal", "#CCFFCC")
|
36 |
|
37 |
def analyze_face(image):
|
38 |
if image is None:
|
39 |
-
return
|
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
|
45 |
|
46 |
landmarks = result.multi_face_landmarks[0].landmark
|
47 |
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
@@ -62,36 +62,17 @@ def analyze_face(image):
|
|
62 |
html += '</div>'
|
63 |
return html
|
64 |
|
65 |
-
|
66 |
-
section("🩸 Hematology", [
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
])
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
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("""
|
@@ -107,17 +88,10 @@ with demo:
|
|
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()
|
|
|
1 |
# Face Detection-Based AI Automation of Lab Tests
|
2 |
+
# Redesigned UI using Gradio Blocks + HTML Cards (Fixed .then() error)
|
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")
|
32 |
elif value > high:
|
33 |
+
return ("🔺 HIGH", "#FFE680")
|
34 |
else:
|
35 |
+
return ("✅ Normal", "#CCFFCC")
|
36 |
|
37 |
def analyze_face(image):
|
38 |
if image is None:
|
39 |
+
return "<div style='color:red;'>⚠️ Error: No image provided.</div>", 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 "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
45 |
|
46 |
landmarks = result.multi_face_landmarks[0].landmark
|
47 |
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
|
|
62 |
html += '</div>'
|
63 |
return html
|
64 |
|
65 |
+
report = "".join([
|
66 |
+
section("🩸 Hematology", [("Hemoglobin", hb, (13.5, 17.5)), ("WBC Count", wbc, (4.0, 11.0)), ("Platelets", platelets, (150, 450))]),
|
67 |
+
section("🧬 Iron & Liver Panel", [("Iron", iron, (60, 170)), ("Ferritin", ferritin, (30, 300)), ("TIBC", tibc, (250, 400)), ("Bilirubin", bilirubin, (0.3, 1.2))]),
|
68 |
+
section("🧪 Kidney, Thyroid & Stress", [("Creatinine", creatinine, (0.6, 1.2)), ("TSH", tsh, (0.4, 4.0)), ("Cortisol", cortisol, (5, 25))]),
|
69 |
+
section("🧁 Metabolic Panel", [("Fasting Blood Sugar", fbs, (70, 110)), ("HbA1c", hba1c, (4.0, 5.7))]),
|
70 |
+
section("❤️ Vital Signs", [("SpO2", spo2, (95, 100)), ("Heart Rate", heart_rate, (60, 100)), ("Respiratory Rate", rr, (12, 20))])
|
71 |
+
])
|
72 |
+
|
73 |
+
return report, frame_rgb
|
74 |
+
|
75 |
+
# Gradio App Layout (Fixed HTML output handling)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
demo = gr.Blocks()
|
77 |
with demo:
|
78 |
gr.Markdown("""
|
|
|
88 |
result_html = gr.HTML(label="🧪 Visual Diagnostic Cards")
|
89 |
result_image = gr.Image(label="📷 Face Scan Annotated")
|
90 |
|
|
|
|
|
|
|
91 |
submit_btn.click(
|
92 |
fn=analyze_face,
|
93 |
inputs=image_input,
|
94 |
outputs=[result_html, result_image]
|
|
|
|
|
|
|
|
|
95 |
)
|
96 |
|
97 |
demo.launch()
|