Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
# Face Detection-Based AI Automation of Lab Tests
|
| 2 |
-
#
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
import mediapipe as mp
|
|
|
|
|
|
|
| 8 |
|
| 9 |
mp_face_mesh = mp.solutions.face_mesh
|
| 10 |
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5)
|
|
@@ -34,112 +36,36 @@ def get_risk_color(value, normal_range):
|
|
| 34 |
else:
|
| 35 |
return ("Normal", "✅", "#CCFFCC")
|
| 36 |
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
build_table("🩸 Hematology", [("Hemoglobin", hb, (13.5, 17.5)), ("WBC Count", wbc, (4.0, 11.0)), ("Platelets", platelets, (150, 450))]),
|
| 71 |
-
build_table("🧬 Iron & Liver Panel", [("Iron", iron, (60, 170)), ("Ferritin", ferritin, (30, 300)), ("TIBC", tibc, (250, 400)), ("Bilirubin", bilirubin, (0.3, 1.2))]),
|
| 72 |
-
build_table("🧪 Kidney, Thyroid & Stress", [("Creatinine", creatinine, (0.6, 1.2)), ("TSH", tsh, (0.4, 4.0)), ("Cortisol", cortisol, (5, 25))]),
|
| 73 |
-
build_table("🧁 Metabolic Panel", [("Fasting Blood Sugar", fbs, (70, 110)), ("HbA1c", hba1c, (4.0, 5.7))]),
|
| 74 |
-
build_table("❤️ Vital Signs", [("SpO2", spo2, (95, 100)), ("Heart Rate", heart_rate, (60, 100)), ("Respiratory Rate", rr, (12, 20))])
|
| 75 |
-
])
|
| 76 |
-
|
| 77 |
-
summary = "<div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#fcfcfc;'>"
|
| 78 |
-
summary += "<h4>📝 Summary for You</h4><ul>"
|
| 79 |
-
if hb < 13.5:
|
| 80 |
-
summary += "<li>Your hemoglobin is a bit low — this could mean mild anemia. Consider a CBC test and iron supplements.</li>"
|
| 81 |
-
if iron < 60 or ferritin < 30:
|
| 82 |
-
summary += "<li>Signs of low iron storage detected. An iron profile blood test is recommended.</li>"
|
| 83 |
-
if bilirubin > 1.2:
|
| 84 |
-
summary += "<li>Some signs of jaundice were detected. Please consult for a Liver Function Test (LFT).</li>"
|
| 85 |
-
if hba1c > 5.7:
|
| 86 |
-
summary += "<li>Your HbA1c is slightly elevated — this can signal pre-diabetes. A fasting glucose test may help.</li>"
|
| 87 |
-
if spo2 < 95:
|
| 88 |
-
summary += "<li>Oxygen levels appear below normal. Please recheck with a pulse oximeter if symptoms persist.</li>"
|
| 89 |
-
summary += "</ul><p><strong>💡 Tip:</strong> This is an AI-based screening and should be followed up with a lab visit for confirmation.</p></div>"
|
| 90 |
-
|
| 91 |
-
html_output += summary
|
| 92 |
-
|
| 93 |
-
html_output += "<br><div style='margin-top:20px;padding:12px;border:2px solid #2d87f0;background:#f2faff;text-align:center;border-radius:8px;'>"
|
| 94 |
-
html_output += "<h4>📞 Book a Lab Test</h4>"
|
| 95 |
-
html_output += "<p>Prefer to get your tests confirmed at a nearby center? Click below to find certified labs in your area.</p>"
|
| 96 |
-
html_output += "<button style='padding:10px 20px;background:#007BFF;color:#fff;border:none;border-radius:5px;cursor:pointer;'>Find Labs Near Me</button>"
|
| 97 |
-
html_output += "</div>"
|
| 98 |
-
|
| 99 |
-
lang_blocks = """
|
| 100 |
-
<div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#f9f9f9;'>
|
| 101 |
-
<h4>🗣️ Summary in Your Language</h4>
|
| 102 |
-
<details><summary><b>Hindi</b></summary><ul>
|
| 103 |
-
<li>आपका हीमोग्लोबिन थोड़ा कम है — यह हल्के एनीमिया का संकेत हो सकता है। कृपया CBC और आयरन टेस्ट करवाएं।</li>
|
| 104 |
-
<li>लो आयरन स्टोरेज देखा गया है। एक आयरन प्रोफाइल टेस्ट की सिफारिश की जाती है।</li>
|
| 105 |
-
<li>जॉन्डिस के लक्षण देखे गए हैं। कृपया LFT करवाएं।</li>
|
| 106 |
-
<li>HbA1c थोड़ा बढ़ा हुआ है — यह प्री-डायबिटीज़ का संकेत हो सकता है।</li>
|
| 107 |
-
<li>ऑक्सीजन स्तर कम दिख रहा है। पल्स ऑक्सीमीटर से दोबारा जांचें।</li>
|
| 108 |
-
</ul></details>
|
| 109 |
-
|
| 110 |
-
<details><summary><b>Telugu</b></summary><ul>
|
| 111 |
-
<li>మీ హిమోగ్లోబిన్ కొంచెం తక్కువగా ఉంది — ఇది తేలికపాటి అనీమియా సూచించవచ్చు. CBC, Iron పరీక్ష చేయించండి.</li>
|
| 112 |
-
<li>Iron నిల్వలు తక్కువగా కనిపించాయి. Iron ప్రొఫైల్ బ్లడ్ టెస్ట్ చేయించండి.</li>
|
| 113 |
-
<li>జాండీస్ సంకేతాలు గుర్తించబడ్డాయి. LFT చేయించండి.</li>
|
| 114 |
-
<li>HbA1c కొంచెం పెరిగింది — ఇది ప్రీ-డయాబెటిస్ సూచించవచ్చు.</li>
|
| 115 |
-
<li>ఆక్సిజన్ స్థాయి తక్కువగా ఉంది. తిరిగి పరీక్షించండి.</li>
|
| 116 |
-
</ul></details>
|
| 117 |
-
</div>
|
| 118 |
-
"""
|
| 119 |
-
|
| 120 |
-
html_output += lang_blocks
|
| 121 |
-
return html_output, frame_rgb
|
| 122 |
-
|
| 123 |
-
# Gradio App Layout
|
| 124 |
-
with gr.Blocks() as demo:
|
| 125 |
-
gr.Markdown("""
|
| 126 |
-
# 🧠 Face-Based Lab Test AI Report
|
| 127 |
-
Upload a face photo to infer health diagnostics with AI-based visual markers.
|
| 128 |
-
""")
|
| 129 |
-
|
| 130 |
-
with gr.Row():
|
| 131 |
-
with gr.Column(scale=1):
|
| 132 |
-
image_input = gr.Image(type="numpy", label="📸 Upload Face Image")
|
| 133 |
-
submit_btn = gr.Button("🔍 Analyze")
|
| 134 |
-
with gr.Column(scale=2):
|
| 135 |
-
result_html = gr.HTML(label="🧪 Health Report Table")
|
| 136 |
-
result_image = gr.Image(label="📷 Face Scan Annotated")
|
| 137 |
-
|
| 138 |
-
submit_btn.click(fn=analyze_face, inputs=image_input, outputs=[result_html, result_image])
|
| 139 |
-
|
| 140 |
-
gr.Markdown("""
|
| 141 |
-
---
|
| 142 |
-
✅ Table Format • Color-coded Status • Normal Range View • Multilingual Summary • Booking Prompt
|
| 143 |
-
""")
|
| 144 |
-
|
| 145 |
-
demo.launch()
|
|
|
|
| 1 |
# Face Detection-Based AI Automation of Lab Tests
|
| 2 |
+
# UI: Clean table, multilingual summary, PDF-ready
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
import mediapipe as mp
|
| 8 |
+
from fpdf import FPDF
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
mp_face_mesh = mp.solutions.face_mesh
|
| 12 |
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5)
|
|
|
|
| 36 |
else:
|
| 37 |
return ("Normal", "✅", "#CCFFCC")
|
| 38 |
|
| 39 |
+
def generate_pdf_report(image, results_dict, summary_text):
|
| 40 |
+
pdf = FPDF()
|
| 41 |
+
pdf.add_page()
|
| 42 |
+
pdf.set_font("Arial", "B", 16)
|
| 43 |
+
pdf.cell(0, 10, "SL Diagnostics - Face Scan AI Lab Report", ln=True, align='C')
|
| 44 |
+
|
| 45 |
+
if image is not None:
|
| 46 |
+
img_path = "patient_face.jpg"
|
| 47 |
+
cv2.imwrite(img_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
|
| 48 |
+
pdf.image(img_path, x=80, y=25, w=50)
|
| 49 |
+
os.remove(img_path)
|
| 50 |
+
pdf.ln(60)
|
| 51 |
+
|
| 52 |
+
pdf.set_font("Arial", "B", 12)
|
| 53 |
+
pdf.cell(0, 10, "Results Summary", ln=True)
|
| 54 |
+
pdf.set_font("Arial", "", 10)
|
| 55 |
+
|
| 56 |
+
for key, val in results_dict.items():
|
| 57 |
+
if isinstance(val, (int, float)):
|
| 58 |
+
pdf.cell(0, 8, f"{key}: {val}", ln=True)
|
| 59 |
+
|
| 60 |
+
pdf.ln(5)
|
| 61 |
+
pdf.set_font("Arial", "B", 12)
|
| 62 |
+
pdf.cell(0, 10, "AI Summary (English)", ln=True)
|
| 63 |
+
pdf.set_font("Arial", "", 10)
|
| 64 |
+
for line in summary_text.split("<li>"):
|
| 65 |
+
if "</li>" in line:
|
| 66 |
+
clean = line.split("</li>")[0].strip()
|
| 67 |
+
pdf.multi_cell(0, 8, f"- {clean}")
|
| 68 |
+
|
| 69 |
+
output_path = "/mnt/data/SL_Diagnostics_Face_Scan_Report.pdf"
|
| 70 |
+
pdf.output(output_path)
|
| 71 |
+
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|