Update app.py
Browse files
app.py
CHANGED
@@ -120,7 +120,52 @@ def analyze_video(video_path):
|
|
120 |
plot_path = "/tmp/ppg_plot.png"
|
121 |
plt.savefig(plot_path)
|
122 |
plt.close()
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
def analyze_face(image):
|
126 |
if image is None:
|
|
|
120 |
plot_path = "/tmp/ppg_plot.png"
|
121 |
plt.savefig(plot_path)
|
122 |
plt.close()
|
123 |
+
# Reuse frame_sample for full analysis
|
124 |
+
frame_rgb = cv2.cvtColor(frame_sample, cv2.COLOR_BGR2RGB)
|
125 |
+
result = face_mesh.process(frame_rgb)
|
126 |
+
if not result.multi_face_landmarks:
|
127 |
+
return "<div style='color:red;'>⚠️ Face not detected in video.</div>", frame_rgb
|
128 |
+
landmarks = result.multi_face_landmarks[0].landmark
|
129 |
+
features = extract_features(frame_rgb, landmarks)
|
130 |
+
test_values = {}
|
131 |
+
r2_scores = {}
|
132 |
+
for label in models:
|
133 |
+
if label == "Hemoglobin":
|
134 |
+
prediction = models[label].predict([features])[0]
|
135 |
+
test_values[label] = prediction
|
136 |
+
r2_scores[label] = hemoglobin_r2
|
137 |
+
else:
|
138 |
+
value = models[label].predict([[random.uniform(0.2, 0.5) for _ in range(7)]])[0]
|
139 |
+
test_values[label] = value
|
140 |
+
r2_scores[label] = 0.0
|
141 |
+
html_output = "".join([
|
142 |
+
f'<div style="font-size:14px;color:#888;margin-bottom:10px;">Hemoglobin R² Score: {r2_scores.get("Hemoglobin", "NA"):.2f}</div>',
|
143 |
+
build_table("🩸 Hematology", [("Hemoglobin", test_values["Hemoglobin"], (13.5, 17.5)), ("WBC Count", test_values["WBC Count"], (4.0, 11.0)), ("Platelet Count", test_values["Platelet Count"], (150, 450))]),
|
144 |
+
build_table("🧬 Iron Panel", [("Iron", test_values["Iron"], (60, 170)), ("Ferritin", test_values["Ferritin"], (30, 300)), ("TIBC", test_values["TIBC"], (250, 400))]),
|
145 |
+
build_table("🧬 Liver & Kidney", [("Bilirubin", test_values["Bilirubin"], (0.3, 1.2)), ("Creatinine", test_values["Creatinine"], (0.6, 1.2)), ("Urea", test_values["Urea"], (7, 20))]),
|
146 |
+
build_table("🧪 Electrolytes", [("Sodium", test_values["Sodium"], (135, 145)), ("Potassium", test_values["Potassium"], (3.5, 5.1))]),
|
147 |
+
build_table("🧁 Metabolic & Thyroid", [("FBS", test_values["FBS"], (70, 110)), ("HbA1c", test_values["HbA1c"], (4.0, 5.7)), ("TSH", test_values["TSH"], (0.4, 4.0))]),
|
148 |
+
build_table("❤️ Vitals", [("SpO2", spo2, (95, 100)), ("Heart Rate", heart_rate, (60, 100)), ("Respiratory Rate", rr, (12, 20)), ("Temperature", test_values["Temperature"], (97, 99)), ("BP Systolic", test_values["BP Systolic"], (90, 120)), ("BP Diastolic", test_values["BP Diastolic"], (60, 80))]),
|
149 |
+
build_table("🩹 Other Indicators", [("Cortisol", test_values["Cortisol"], (5, 25)), ("Albumin", test_values["Albumin"], (3.5, 5.5))])
|
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 test_values["Hemoglobin"] < 13.5:
|
154 |
+
summary += "<li>Your hemoglobin is a bit low — this could mean mild anemia.</li>"
|
155 |
+
if test_values["Iron"] < 60 or test_values["Ferritin"] < 30:
|
156 |
+
summary += "<li>Low iron storage detected — consider an iron profile test.</li>"
|
157 |
+
if test_values["Bilirubin"] > 1.2:
|
158 |
+
summary += "<li>Elevated bilirubin — possible jaundice. Recommend LFT.</li>"
|
159 |
+
if test_values["HbA1c"] > 5.7:
|
160 |
+
summary += "<li>High HbA1c — prediabetes indication. Recommend glucose check.</li>"
|
161 |
+
if spo2 < 95:
|
162 |
+
summary += "<li>Low SpO₂ — suggest retesting with a pulse oximeter.</li>"
|
163 |
+
summary += "</ul><p><strong>💡 Tip:</strong> This is an AI-based estimate. Please follow up with a lab.</p></div>"
|
164 |
+
html_output += summary
|
165 |
+
html_output += "<br><div style='margin-top:20px;padding:12px;border:2px solid #2d87f0;background:#f2faff;text-align:center;border-radius:8px;'>"
|
166 |
+
html_output += "<h4>📞 Book a Lab Test</h4><p>Prefer confirmation? Find certified labs near you.</p>"
|
167 |
+
html_output += "<button style='padding:10px 20px;background:#007BFF;color:#fff;border:none;border-radius:5px;cursor:pointer;'>Find Labs Near Me</button></div>"
|
168 |
+
return html_output, frame_rgb
|
169 |
|
170 |
def analyze_face(image):
|
171 |
if image is None:
|