Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -217,28 +217,27 @@ def build_health_card(profile_image, test_results, summary, patient_name="", pat
|
|
217 |
current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
|
218 |
|
219 |
# Modified analyze_face function
|
220 |
-
def analyze_face(
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
frame = input_data
|
231 |
-
if frame is None:
|
232 |
-
return "<div style='color:red;'>⚠️ Error: No image provided.</div>", None
|
233 |
|
234 |
# Resize image to reduce processing time
|
235 |
-
frame = cv2.resize(
|
236 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
237 |
result = face_mesh.process(frame_rgb)
|
|
|
238 |
if not result.multi_face_landmarks:
|
239 |
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
240 |
landmarks = result.multi_face_landmarks[0].landmark # Fixed: Use integer index
|
241 |
features = extract_features(frame_rgb, landmarks)
|
|
|
242 |
test_values = {}
|
243 |
r2_scores = {}
|
244 |
|
@@ -252,28 +251,12 @@ def analyze_face(input_data):
|
|
252 |
test_values[label] = value
|
253 |
r2_scores[label] = 0.0
|
254 |
|
255 |
-
gray = cv2.cvtColor(frame_rgb, cv2.COLOR_RGB2GRAY)
|
256 |
-
green_std = np.std(frame_rgb[:, :, 1]) / 255
|
257 |
-
brightness_std = np.std(gray) / 255
|
258 |
-
tone_index = np.mean(frame_rgb[100:150, 100:150]) / 255 if frame_rgb[
|
259 |
-
100:150, 100:150].size else 0.5
|
260 |
-
hr_features = [brightness_std, green_std, tone_index]
|
261 |
-
heart_rate = float(np.clip(hr_model.predict([hr_features])[0], 60, 100))
|
262 |
-
skin_patch = frame_rgb[100:150, 100:150]
|
263 |
-
skin_tone_index = np.mean(skin_patch) / 255 if skin_patch.size else 0.5
|
264 |
-
brightness_variation = np.std(cv2.cvtColor(frame_rgb,
|
265 |
-
cv2.COLOR_RGB2GRAY)) / 255
|
266 |
-
spo2_features = [heart_rate, brightness_variation, skin_tone_index]
|
267 |
-
spo2 = spo2_model.predict([spo2_features])[0]
|
268 |
-
rr = int(12 + abs(heart_rate % 5 - 2))
|
269 |
-
|
270 |
test_results = {
|
271 |
"Hematology":
|
272 |
build_table("🩸 Hematology",
|
273 |
[("Hemoglobin", test_values["Hemoglobin"], (13.5, 17.5)),
|
274 |
("WBC Count", test_values["WBC Count"], (4.0, 11.0)),
|
275 |
-
("Platelet Count", test_values["Platelet Count"],
|
276 |
-
(150, 450))]),
|
277 |
"Iron Panel":
|
278 |
build_table("🧬 Iron Panel",
|
279 |
[("Iron", test_values["Iron"], (60, 170)),
|
@@ -290,12 +273,9 @@ def analyze_face(input_data):
|
|
290 |
("Potassium", test_values["Potassium"], (3.5, 5.1))]),
|
291 |
"Vitals":
|
292 |
build_table("❤️ Vitals",
|
293 |
-
[("SpO2",
|
294 |
-
("Heart Rate",
|
295 |
-
("
|
296 |
-
("Temperature", test_values["Temperature"], (97, 99)),
|
297 |
-
("BP Systolic", test_values["BP Systolic"], (90, 120)),
|
298 |
-
("BP Diastolic", test_values["BP Diastolic"], (60, 80))])
|
299 |
}
|
300 |
|
301 |
summary = "<ul><li>Your hemoglobin is a bit low — this could mean mild anemia.</li><li>Low iron storage detected — consider an iron profile test.</li><li>Elevated bilirubin — possible jaundice. Recommend LFT.</li><li>High HbA1c — prediabetes indication. Recommend glucose check.</li><li>Low SpO₂ — suggest retesting with a pulse oximeter.</li></ul>"
|
@@ -303,29 +283,26 @@ def analyze_face(input_data):
|
|
303 |
_, buffer = cv2.imencode('.png', frame_rgb)
|
304 |
profile_image_base64 = base64.b64encode(buffer).decode('utf-8')
|
305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
# Use global patient details
|
307 |
-
global current_patient_details
|
308 |
health_card_html = build_health_card(
|
309 |
profile_image_base64,
|
310 |
test_results,
|
311 |
summary,
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
pdf_filepath=
|
317 |
)
|
318 |
|
319 |
-
|
320 |
-
pdf_filename = f"Health_Report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
321 |
-
pdf_result, pdf_filepath = save_results_to_pdf(test_results, pdf_filename)
|
322 |
-
|
323 |
-
if pdf_filepath:
|
324 |
-
# Copy the PDF to a temporary directory for Gradio to serve it
|
325 |
-
temp_pdf_path = "/tmp/" + os.path.basename(pdf_filepath)
|
326 |
-
shutil.copy(pdf_filepath, temp_pdf_path)
|
327 |
-
|
328 |
-
return health_card_html, temp_pdf_path
|
329 |
|
330 |
|
331 |
# Gradio interface setup
|
@@ -353,7 +330,7 @@ with gr.Blocks() as demo:
|
|
353 |
result_pdf = gr.File(label="Download Health Report PDF", interactive=False)
|
354 |
|
355 |
submit_btn.click(fn=analyze_face,
|
356 |
-
inputs=[
|
357 |
outputs=[result_html, result_pdf])
|
358 |
|
359 |
# Launch Gradio for Replit
|
|
|
217 |
current_patient_details = {'name': '', 'age': '', 'gender': '', 'id': ''}
|
218 |
|
219 |
# Modified analyze_face function
|
220 |
+
def analyze_face(inputs):
|
221 |
+
image = inputs[0]
|
222 |
+
patient_name = inputs[1]
|
223 |
+
patient_age = inputs[2]
|
224 |
+
patient_gender = inputs[3]
|
225 |
+
patient_id = inputs[4]
|
226 |
+
mode = inputs[5]
|
227 |
+
|
228 |
+
if mode == "Image" and image is None:
|
229 |
+
return "<div style='color:red;'>⚠️ Error: No image provided.</div>", None
|
|
|
|
|
|
|
230 |
|
231 |
# Resize image to reduce processing time
|
232 |
+
frame = cv2.resize(image, (640, 480)) # Adjust resolution for Replit
|
233 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
234 |
result = face_mesh.process(frame_rgb)
|
235 |
+
|
236 |
if not result.multi_face_landmarks:
|
237 |
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
238 |
landmarks = result.multi_face_landmarks[0].landmark # Fixed: Use integer index
|
239 |
features = extract_features(frame_rgb, landmarks)
|
240 |
+
|
241 |
test_values = {}
|
242 |
r2_scores = {}
|
243 |
|
|
|
251 |
test_values[label] = value
|
252 |
r2_scores[label] = 0.0
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
test_results = {
|
255 |
"Hematology":
|
256 |
build_table("🩸 Hematology",
|
257 |
[("Hemoglobin", test_values["Hemoglobin"], (13.5, 17.5)),
|
258 |
("WBC Count", test_values["WBC Count"], (4.0, 11.0)),
|
259 |
+
("Platelet Count", test_values["Platelet Count"], (150, 450))]),
|
|
|
260 |
"Iron Panel":
|
261 |
build_table("🧬 Iron Panel",
|
262 |
[("Iron", test_values["Iron"], (60, 170)),
|
|
|
273 |
("Potassium", test_values["Potassium"], (3.5, 5.1))]),
|
274 |
"Vitals":
|
275 |
build_table("❤️ Vitals",
|
276 |
+
[("SpO2", 98, (95, 100)),
|
277 |
+
("Heart Rate", 72, (60, 100)),
|
278 |
+
("Temperature", 98.6, (97, 99))])
|
|
|
|
|
|
|
279 |
}
|
280 |
|
281 |
summary = "<ul><li>Your hemoglobin is a bit low — this could mean mild anemia.</li><li>Low iron storage detected — consider an iron profile test.</li><li>Elevated bilirubin — possible jaundice. Recommend LFT.</li><li>High HbA1c — prediabetes indication. Recommend glucose check.</li><li>Low SpO₂ — suggest retesting with a pulse oximeter.</li></ul>"
|
|
|
283 |
_, buffer = cv2.imencode('.png', frame_rgb)
|
284 |
profile_image_base64 = base64.b64encode(buffer).decode('utf-8')
|
285 |
|
286 |
+
# Create a temporary file path for the PDF
|
287 |
+
pdf_filename = f"Health_Report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
288 |
+
pdf_filepath = f"/tmp/{pdf_filename}"
|
289 |
+
|
290 |
+
# Save the PDF
|
291 |
+
pdf_result, _ = save_results_to_pdf(test_results, pdf_filepath)
|
292 |
+
|
293 |
# Use global patient details
|
|
|
294 |
health_card_html = build_health_card(
|
295 |
profile_image_base64,
|
296 |
test_results,
|
297 |
summary,
|
298 |
+
patient_name,
|
299 |
+
patient_age,
|
300 |
+
patient_gender,
|
301 |
+
patient_id,
|
302 |
+
pdf_filepath=pdf_filepath
|
303 |
)
|
304 |
|
305 |
+
return health_card_html, pdf_filepath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
|
307 |
|
308 |
# Gradio interface setup
|
|
|
330 |
result_pdf = gr.File(label="Download Health Report PDF", interactive=False)
|
331 |
|
332 |
submit_btn.click(fn=analyze_face,
|
333 |
+
inputs=[image_input, patient_name, patient_age, patient_gender, patient_id, mode_selector],
|
334 |
outputs=[result_html, result_pdf])
|
335 |
|
336 |
# Launch Gradio for Replit
|