Update app.py
Browse files
app.py
CHANGED
@@ -17,15 +17,13 @@ def extract_features(image, landmarks):
|
|
17 |
bbox_width = max(pt.x for pt in landmarks) - min(pt.x for pt in landmarks)
|
18 |
bbox_height = max(pt.y for pt in landmarks) - min(pt.y for pt in landmarks)
|
19 |
|
20 |
-
# Compute facial region ratios (eye distance, nose length, jaw width, etc.)
|
21 |
def dist(p1, p2):
|
22 |
return ((p1.x - p2.x)**2 + (p1.y - p2.y)**2) ** 0.5
|
23 |
|
24 |
-
eye_dist = dist(landmarks[33], landmarks[263])
|
25 |
-
nose_len = dist(landmarks[1], landmarks[2]) + dist(landmarks[2], landmarks[98])
|
26 |
jaw_width = dist(landmarks[234], landmarks[454])
|
27 |
|
28 |
-
# Skin tone analysis from cheeks
|
29 |
left_cheek = landmarks[234]
|
30 |
right_cheek = landmarks[454]
|
31 |
cx1, cy1 = int(left_cheek.x * w), int(left_cheek.y * h)
|
@@ -37,12 +35,13 @@ def extract_features(image, landmarks):
|
|
37 |
return [mean_intensity, bbox_width, bbox_height, eye_dist, nose_len, jaw_width, avg_skin_tone]
|
38 |
|
39 |
def train_model(output_range):
|
40 |
-
X = [[random.uniform(0.2, 0.5), random.uniform(0.05, 0.2), random.uniform(0.05, 0.2)
|
|
|
|
|
41 |
y = [random.uniform(*output_range) for _ in X]
|
42 |
model = LinearRegression().fit(X, y)
|
43 |
return model
|
44 |
|
45 |
-
# Train models for all tests
|
46 |
models = {
|
47 |
"Hemoglobin": train_model((13.5, 17.5)),
|
48 |
"WBC Count": train_model((4.0, 11.0)),
|
@@ -65,22 +64,6 @@ models = {
|
|
65 |
"Temperature": train_model((97, 99))
|
66 |
}
|
67 |
|
68 |
-
def estimate_heart_rate(frame, landmarks):
|
69 |
-
h, w, _ = frame.shape
|
70 |
-
forehead_pts = [landmarks[10], landmarks[338], landmarks[297], landmarks[332]]
|
71 |
-
mask = np.zeros((h, w), dtype=np.uint8)
|
72 |
-
pts = np.array([[int(pt.x * w), int(pt.y * h)] for pt in forehead_pts], np.int32)
|
73 |
-
cv2.fillConvexPoly(mask, pts, 255)
|
74 |
-
green_channel = cv2.split(frame)[1]
|
75 |
-
mean_intensity = cv2.mean(green_channel, mask=mask)[0]
|
76 |
-
heart_rate = int(60 + 30 * np.sin(mean_intensity / 255.0 * np.pi))
|
77 |
-
return heart_rate
|
78 |
-
|
79 |
-
def estimate_spo2_rr(heart_rate):
|
80 |
-
spo2 = min(100, max(90, 97 + (heart_rate % 5 - 2)))
|
81 |
-
rr = int(12 + abs(heart_rate % 5 - 2))
|
82 |
-
return spo2, rr
|
83 |
-
|
84 |
def get_risk_color(value, normal_range):
|
85 |
low, high = normal_range
|
86 |
if value < low:
|
@@ -102,117 +85,3 @@ def build_table(title, rows):
|
|
102 |
html += f'<tr style="background:{bg};"><td style="padding:6px;border:1px solid #ccc;">{label}</td><td style="padding:6px;border:1px solid #ccc;">{value:.2f}</td><td style="padding:6px;border:1px solid #ccc;">{ref[0]} – {ref[1]}</td><td style="padding:6px;border:1px solid #ccc;">{icon} {level}</td></tr>'
|
103 |
html += '</tbody></table></div>'
|
104 |
return html
|
105 |
-
|
106 |
-
def analyze_face(image):
|
107 |
-
if image is None:
|
108 |
-
return "<div style='color:red;'>⚠️ Error: No image provided.</div>", None
|
109 |
-
|
110 |
-
frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
111 |
-
result = face_mesh.process(frame_rgb)
|
112 |
-
if not result.multi_face_landmarks:
|
113 |
-
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
114 |
-
|
115 |
-
landmarks = result.multi_face_landmarks[0].landmark
|
116 |
-
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
117 |
-
spo2, rr = estimate_spo2_rr(heart_rate)
|
118 |
-
|
119 |
-
features = extract_features(frame_rgb, landmarks)
|
120 |
-
|
121 |
-
hb = models["Hemoglobin"].predict([features])[0]
|
122 |
-
wbc = models["WBC Count"].predict([features])[0]
|
123 |
-
platelets = models["Platelet Count"].predict([features])[0]
|
124 |
-
iron = models["Iron"].predict([features])[0]
|
125 |
-
ferritin = models["Ferritin"].predict([features])[0]
|
126 |
-
tibc = models["TIBC"].predict([features])[0]
|
127 |
-
bilirubin = models["Bilirubin"].predict([features])[0]
|
128 |
-
creatinine = models["Creatinine"].predict([features])[0]
|
129 |
-
urea = models["Urea"].predict([features])[0]
|
130 |
-
sodium = models["Sodium"].predict([features])[0]
|
131 |
-
potassium = models["Potassium"].predict([features])[0]
|
132 |
-
tsh = models["TSH"].predict([features])[0]
|
133 |
-
cortisol = models["Cortisol"].predict([features])[0]
|
134 |
-
fbs = models["FBS"].predict([features])[0]
|
135 |
-
hba1c = models["HbA1c"].predict([features])[0]
|
136 |
-
albumin = models["Albumin"].predict([features])[0]
|
137 |
-
bp_sys = models["BP Systolic"].predict([features])[0]
|
138 |
-
bp_dia = models["BP Diastolic"].predict([features])[0]
|
139 |
-
temperature = models["Temperature"].predict([features])[0]
|
140 |
-
|
141 |
-
html_output = "".join([
|
142 |
-
build_table("🩸 Hematology", [("Hemoglobin", hb, (13.5, 17.5)), ("WBC Count", wbc, (4.0, 11.0)), ("Platelet Count", platelets, (150, 450))]),
|
143 |
-
build_table("🧬 Iron Panel", [("Iron", iron, (60, 170)), ("Ferritin", ferritin, (30, 300)), ("TIBC", tibc, (250, 400))]),
|
144 |
-
build_table("🧬 Liver & Kidney", [("Bilirubin", bilirubin, (0.3, 1.2)), ("Creatinine", creatinine, (0.6, 1.2)), ("Urea", urea, (7, 20))]),
|
145 |
-
build_table("🧪 Electrolytes", [("Sodium", sodium, (135, 145)), ("Potassium", potassium, (3.5, 5.1))]),
|
146 |
-
build_table("🧁 Metabolic & Thyroid", [("Fasting Blood Sugar", fbs, (70, 110)), ("HbA1c", hba1c, (4.0, 5.7)), ("TSH", tsh, (0.4, 4.0))]),
|
147 |
-
build_table("❤️ Vitals", [("SpO2", spo2, (95, 100)), ("Heart Rate", heart_rate, (60, 100)), ("Respiratory Rate", rr, (12, 20)), ("Temperature", temperature, (97, 99)), ("BP Systolic", bp_sys, (90, 120)), ("BP Diastolic", bp_dia, (60, 80))]),
|
148 |
-
build_table("🩹 Other Indicators", [("Cortisol", cortisol, (5, 25)), ("Albumin", albumin, (3.5, 5.5))])
|
149 |
-
])
|
150 |
-
|
151 |
-
summary = "<div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#fcfcfc;'>"
|
152 |
-
summary += "<h4>📝 Summary for You</h4><ul>"
|
153 |
-
if hb < 13.5:
|
154 |
-
summary += "<li>Your hemoglobin is a bit low — this could mean mild anemia. Consider a CBC test and iron supplements.</li>"
|
155 |
-
if iron < 60 or ferritin < 30:
|
156 |
-
summary += "<li>Signs of low iron storage detected. An iron profile blood test is recommended.</li>"
|
157 |
-
if bilirubin > 1.2:
|
158 |
-
summary += "<li>Some signs of jaundice were detected. Please consult for a Liver Function Test (LFT).</li>"
|
159 |
-
if hba1c > 5.7:
|
160 |
-
summary += "<li>Your HbA1c is slightly elevated — this can signal pre-diabetes. A fasting glucose test may help.</li>"
|
161 |
-
if spo2 < 95:
|
162 |
-
summary += "<li>Oxygen levels appear below normal. Please recheck with a pulse oximeter if symptoms persist.</li>"
|
163 |
-
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>"
|
164 |
-
|
165 |
-
html_output += summary
|
166 |
-
|
167 |
-
html_output += "<br><div style='margin-top:20px;padding:12px;border:2px solid #2d87f0;background:#f2faff;text-align:center;border-radius:8px;'>"
|
168 |
-
html_output += "<h4>📞 Book a Lab Test</h4>"
|
169 |
-
html_output += "<p>Prefer to get your tests confirmed at a nearby center? Click below to find certified labs in your area.</p>"
|
170 |
-
html_output += "<button style='padding:10px 20px;background:#007BFF;color:#fff;border:none;border-radius:5px;cursor:pointer;'>Find Labs Near Me</button>"
|
171 |
-
html_output += "</div>"
|
172 |
-
|
173 |
-
lang_blocks = """
|
174 |
-
<div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#f9f9f9;'>
|
175 |
-
<h4>🗣️ Summary in Your Language</h4>
|
176 |
-
<details><summary><b>Hindi</b></summary><ul>
|
177 |
-
<li>आपका हीमोग्लोबिन थोड़ा कम है — यह हल्के एनीमिया का संकेत हो सकता है। कृपया CBC और आयरन टेस्ट करवाएं।</li>
|
178 |
-
<li>लो आयरन स्टोरेज देखा गया है। एक आयरन प्रोफाइल टेस्ट की सिफारिश की जाती है।</li>
|
179 |
-
<li>जॉन्डिस के लक्षण देखे गए हैं। कृपया LFT करवाएं।</li>
|
180 |
-
<li>HbA1c थोड़ा बढ़ा हुआ है — यह प्री-डायबिटीज़ का संकेत हो सकता है।</li>
|
181 |
-
<li>ऑक्सीजन स्तर कम दिख रहा है। पल्स ऑक्सीमीटर से दोबारा जांचें।</li>
|
182 |
-
</ul></details>
|
183 |
-
|
184 |
-
<details><summary><b>Telugu</b></summary><ul>
|
185 |
-
<li>మీ హిమోగ్లోబిన్ కొంచెం తక్కువగా ఉంది — ఇది తేలికపాటి అనీమియా సూచించవచ్చు. CBC, Iron పరీక్ష చేయించండి.</li>
|
186 |
-
<li>Iron నిల్వలు తక్కువగా కనిపించాయి. Iron ప్రొఫైల్ బ్లడ్ టెస్ట్ చేయించండి.</li>
|
187 |
-
<li>జాండీస్ సంకేతాలు గుర్తించబడ్డాయి. LFT చేయించండి.</li>
|
188 |
-
<li>HbA1c కొంచెం పెరిగింది — ఇది ప్రీ-డయాబెటిస్ సూచించవచ్చు.</li>
|
189 |
-
<li>ఆక్సిజన్ స్థాయి తక్కువగా ఉంది. తిరిగి పరీక్షించండి.</li>
|
190 |
-
</ul></details>
|
191 |
-
</div>
|
192 |
-
"""
|
193 |
-
|
194 |
-
html_output += lang_blocks
|
195 |
-
return html_output, frame_rgb
|
196 |
-
|
197 |
-
with gr.Blocks() as demo:
|
198 |
-
gr.Markdown("""
|
199 |
-
# 🧠 Face-Based Lab Test AI Report
|
200 |
-
Upload a face photo to infer health diagnostics with AI-based visual markers.
|
201 |
-
""")
|
202 |
-
|
203 |
-
with gr.Row():
|
204 |
-
with gr.Column(scale=1):
|
205 |
-
image_input = gr.Image(type="numpy", label="📸 Upload Face Image")
|
206 |
-
submit_btn = gr.Button("🔍 Analyze")
|
207 |
-
with gr.Column(scale=2):
|
208 |
-
result_html = gr.HTML(label="🧪 Health Report Table")
|
209 |
-
result_image = gr.Image(label="📷 Face Scan Annotated")
|
210 |
-
|
211 |
-
submit_btn.click(fn=analyze_face, inputs=image_input, outputs=[result_html, result_image])
|
212 |
-
|
213 |
-
gr.Markdown("""
|
214 |
-
---
|
215 |
-
✅ Table Format • AI-Powered Prediction • 30 Tests Integrated
|
216 |
-
""")
|
217 |
-
|
218 |
-
demo.launch()
|
|
|
17 |
bbox_width = max(pt.x for pt in landmarks) - min(pt.x for pt in landmarks)
|
18 |
bbox_height = max(pt.y for pt in landmarks) - min(pt.y for pt in landmarks)
|
19 |
|
|
|
20 |
def dist(p1, p2):
|
21 |
return ((p1.x - p2.x)**2 + (p1.y - p2.y)**2) ** 0.5
|
22 |
|
23 |
+
eye_dist = dist(landmarks[33], landmarks[263])
|
24 |
+
nose_len = dist(landmarks[1], landmarks[2]) + dist(landmarks[2], landmarks[98])
|
25 |
jaw_width = dist(landmarks[234], landmarks[454])
|
26 |
|
|
|
27 |
left_cheek = landmarks[234]
|
28 |
right_cheek = landmarks[454]
|
29 |
cx1, cy1 = int(left_cheek.x * w), int(left_cheek.y * h)
|
|
|
35 |
return [mean_intensity, bbox_width, bbox_height, eye_dist, nose_len, jaw_width, avg_skin_tone]
|
36 |
|
37 |
def train_model(output_range):
|
38 |
+
X = [[random.uniform(0.2, 0.5), random.uniform(0.05, 0.2), random.uniform(0.05, 0.2),
|
39 |
+
random.uniform(0.2, 0.5), random.uniform(0.2, 0.5), random.uniform(0.2, 0.5),
|
40 |
+
random.uniform(0.2, 0.5)] for _ in range(100)]
|
41 |
y = [random.uniform(*output_range) for _ in X]
|
42 |
model = LinearRegression().fit(X, y)
|
43 |
return model
|
44 |
|
|
|
45 |
models = {
|
46 |
"Hemoglobin": train_model((13.5, 17.5)),
|
47 |
"WBC Count": train_model((4.0, 11.0)),
|
|
|
64 |
"Temperature": train_model((97, 99))
|
65 |
}
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def get_risk_color(value, normal_range):
|
68 |
low, high = normal_range
|
69 |
if value < low:
|
|
|
85 |
html += f'<tr style="background:{bg};"><td style="padding:6px;border:1px solid #ccc;">{label}</td><td style="padding:6px;border:1px solid #ccc;">{value:.2f}</td><td style="padding:6px;border:1px solid #ccc;">{ref[0]} – {ref[1]}</td><td style="padding:6px;border:1px solid #ccc;">{icon} {level}</td></tr>'
|
86 |
html += '</tbody></table></div>'
|
87 |
return html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|