Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Face Detection-Based AI Automation of Lab Tests
|
2 |
+
# Streamlit App with OpenCV + rPPG + MediaPipe Integration (Deployable on Hugging Face Spaces)
|
3 |
+
|
4 |
+
import streamlit as st
|
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=False, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.5)
|
15 |
+
|
16 |
+
# Function to calculate mean green intensity (simplified rPPG)
|
17 |
+
def estimate_heart_rate(frame, landmarks):
|
18 |
+
h, w, _ = frame.shape
|
19 |
+
forehead_pts = [landmarks[10], landmarks[338], landmarks[297], landmarks[332]]
|
20 |
+
mask = np.zeros((h, w), dtype=np.uint8)
|
21 |
+
pts = np.array([[int(pt.x * w), int(pt.y * h)] for pt in forehead_pts], np.int32)
|
22 |
+
cv2.fillConvexPoly(mask, pts, 255)
|
23 |
+
green_channel = cv2.split(frame)[1]
|
24 |
+
mean_intensity = cv2.mean(green_channel, mask=mask)[0]
|
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 (dummy based on heart 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 |
+
# Streamlit UI setup
|
35 |
+
st.set_page_config(page_title="Face-Based Lab Test Automation", layout="wide")
|
36 |
+
st.title("🧠 Face Detection-Based AI Automation of Lab Tests")
|
37 |
+
|
38 |
+
col1, col2 = st.columns([1, 2])
|
39 |
+
|
40 |
+
# Left: Webcam and Face Scan
|
41 |
+
with col1:
|
42 |
+
st.header("📷 Scan Face")
|
43 |
+
run = st.checkbox("Start Camera")
|
44 |
+
FRAME_WINDOW = st.image([])
|
45 |
+
camera = cv2.VideoCapture(0)
|
46 |
+
|
47 |
+
results = {}
|
48 |
+
while run:
|
49 |
+
ret, frame = camera.read()
|
50 |
+
if not ret:
|
51 |
+
break
|
52 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
53 |
+
result = face_mesh.process(frame_rgb)
|
54 |
+
if result.multi_face_landmarks:
|
55 |
+
landmarks = result.multi_face_landmarks[0].landmark
|
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 |
+
st.info("No face scan detected yet.")
|
95 |
+
|
96 |
+
# Footer
|
97 |
+
st.markdown("---")
|
98 |
+
st.caption("© 2025 FaceLab AI by Sathkrutha Tech Solutions. Built with Streamlit, OpenCV, MediaPipe, and rPPG techniques.")
|