Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,14 @@
|
|
1 |
# Face Detection-Based AI Automation of Lab Tests
|
2 |
-
#
|
3 |
|
4 |
-
import
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
import mediapipe as mp
|
8 |
-
import pandas as pd
|
9 |
-
import time
|
10 |
-
import os
|
11 |
|
12 |
# Setup Mediapipe Face Mesh
|
13 |
mp_face_mesh = mp.solutions.face_mesh
|
14 |
-
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=
|
15 |
|
16 |
# Function to calculate mean green intensity (simplified rPPG)
|
17 |
def estimate_heart_rate(frame, landmarks):
|
@@ -25,74 +22,44 @@ def estimate_heart_rate(frame, landmarks):
|
|
25 |
heart_rate = int(60 + 30 * np.sin(mean_intensity / 255.0 * np.pi)) # Simulated
|
26 |
return heart_rate
|
27 |
|
28 |
-
# Estimate SpO2 and Respiratory Rate (
|
29 |
def estimate_spo2_rr(heart_rate):
|
30 |
spo2 = min(100, max(90, 97 + (heart_rate % 5 - 2)))
|
31 |
rr = int(12 + abs(heart_rate % 5 - 2))
|
32 |
return spo2, rr
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
FRAME_WINDOW = st.image([])
|
45 |
-
camera = cv2.VideoCapture(0)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
57 |
-
spo2, rr = estimate_spo2_rr(heart_rate)
|
58 |
-
results = {
|
59 |
-
"Hemoglobin": "12.3 g/dL (Estimated)",
|
60 |
-
"SpO2": f"{spo2}%",
|
61 |
-
"Heart Rate": f"{heart_rate} bpm",
|
62 |
-
"Blood Pressure": "Low",
|
63 |
-
"Respiratory Rate": f"{rr} breaths/min",
|
64 |
-
"Risk Flags": ["Anemia Mild", "Hydration Low"]
|
65 |
-
}
|
66 |
-
FRAME_WINDOW.image(frame_rgb)
|
67 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
68 |
-
break
|
69 |
-
camera.release()
|
70 |
-
|
71 |
-
# Right: Health Report
|
72 |
-
with col2:
|
73 |
-
st.header("🧪 AI-Based Diagnostic Report")
|
74 |
-
if results:
|
75 |
-
with st.expander("Hematology & Blood Tests", expanded=True):
|
76 |
-
st.metric("Hemoglobin", results["Hemoglobin"], "Anemia Mild")
|
77 |
-
|
78 |
-
with st.expander("Vital Signs and Biochemical Tests", expanded=True):
|
79 |
-
st.metric("SpO2", results["SpO2"])
|
80 |
-
st.metric("Heart Rate", results["Heart Rate"])
|
81 |
-
st.metric("Blood Pressure", results["Blood Pressure"], "Low")
|
82 |
-
st.metric("Respiratory Rate", results["Respiratory Rate"], "Hydration Low")
|
83 |
-
|
84 |
-
with st.expander("Risk Flags"):
|
85 |
-
for flag in results["Risk Flags"]:
|
86 |
-
st.error(flag)
|
87 |
-
|
88 |
-
# Export Button
|
89 |
-
if st.button("📥 Export Report as CSV"):
|
90 |
-
df = pd.DataFrame([results])
|
91 |
-
df.to_csv("lab_scan_report.csv", index=False)
|
92 |
-
st.success("Report saved as lab_scan_report.csv")
|
93 |
else:
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
st.markdown("---")
|
98 |
-
st.caption("© 2025 FaceLab AI by Sathkrutha Tech Solutions. Built with Streamlit, OpenCV, MediaPipe, and rPPG techniques.")
|
|
|
1 |
# Face Detection-Based AI Automation of Lab Tests
|
2 |
+
# Gradio App with OpenCV + MediaPipe + rPPG Integration for Hugging Face Spaces
|
3 |
|
4 |
+
import gradio as gr
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
import mediapipe as mp
|
|
|
|
|
|
|
8 |
|
9 |
# Setup Mediapipe Face Mesh
|
10 |
mp_face_mesh = mp.solutions.face_mesh
|
11 |
+
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5)
|
12 |
|
13 |
# Function to calculate mean green intensity (simplified rPPG)
|
14 |
def estimate_heart_rate(frame, landmarks):
|
|
|
22 |
heart_rate = int(60 + 30 * np.sin(mean_intensity / 255.0 * np.pi)) # Simulated
|
23 |
return heart_rate
|
24 |
|
25 |
+
# Estimate SpO2 and Respiratory Rate (simulated based on heart rate)
|
26 |
def estimate_spo2_rr(heart_rate):
|
27 |
spo2 = min(100, max(90, 97 + (heart_rate % 5 - 2)))
|
28 |
rr = int(12 + abs(heart_rate % 5 - 2))
|
29 |
return spo2, rr
|
30 |
|
31 |
+
# Main analysis function
|
32 |
+
def analyze_face(image):
|
33 |
+
if image is None:
|
34 |
+
return "No image provided", None
|
35 |
|
36 |
+
frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
37 |
+
result = face_mesh.process(frame_rgb)
|
38 |
|
39 |
+
if result.multi_face_landmarks:
|
40 |
+
landmarks = result.multi_face_landmarks[0].landmark
|
41 |
+
heart_rate = estimate_heart_rate(frame_rgb, landmarks)
|
42 |
+
spo2, rr = estimate_spo2_rr(heart_rate)
|
|
|
|
|
43 |
|
44 |
+
report = {
|
45 |
+
"Hemoglobin": "12.3 g/dL (Estimated)",
|
46 |
+
"SpO2": f"{spo2}%",
|
47 |
+
"Heart Rate": f"{heart_rate} bpm",
|
48 |
+
"Blood Pressure": "Low",
|
49 |
+
"Respiratory Rate": f"{rr} breaths/min",
|
50 |
+
"Risk Flags": ["Anemia Mild", "Hydration Low"]
|
51 |
+
}
|
52 |
+
return report, frame_rgb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
else:
|
54 |
+
return "Face not detected", None
|
55 |
+
|
56 |
+
# Launch UI
|
57 |
+
demo = gr.Interface(
|
58 |
+
fn=analyze_face,
|
59 |
+
inputs=gr.Image(type="numpy", label="Upload a Face Image"),
|
60 |
+
outputs=[gr.JSON(label="AI Diagnostic Report"), gr.Image(label="Annotated Image")],
|
61 |
+
title="Face-Based AI Lab Test Automation",
|
62 |
+
description="Upload a face image to estimate basic vital signs and lab test indicators using AI-based visual inference."
|
63 |
+
)
|
64 |
|
65 |
+
demo.launch()
|
|
|
|