SuriRaja commited on
Commit
bdf9b37
·
verified ·
1 Parent(s): 19e69ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -55
app.py CHANGED
@@ -13,17 +13,13 @@ face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refin
13
  def extract_features(image, landmarks):
14
  mean_intensity = np.mean(image)
15
  h, w, _ = image.shape
16
-
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)
@@ -31,7 +27,6 @@ def extract_features(image, landmarks):
31
  skin_tone1 = np.mean(image[cy1-5:cy1+5, cx1-5:cx1+5]) if 5 <= cy1 < h-5 and 5 <= cx1 < w-5 else 0
32
  skin_tone2 = np.mean(image[cy2-5:cy2+5, cx2-5:cx2+5]) if 5 <= cy2 < h-5 and 5 <= cx2 < w-5 else 0
33
  avg_skin_tone = (skin_tone1 + skin_tone2) / 2
34
-
35
  return [mean_intensity, bbox_width, bbox_height, eye_dist, nose_len, jaw_width, avg_skin_tone]
36
 
37
  def train_model(output_range):
@@ -89,20 +84,16 @@ def build_table(title, rows):
89
  def analyze_face(image):
90
  if image is None:
91
  return "<div style='color:red;'>⚠️ Error: No image provided.</div>", None
92
-
93
  frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
94
  result = face_mesh.process(frame_rgb)
95
  if not result.multi_face_landmarks:
96
  return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
97
-
98
  landmarks = result.multi_face_landmarks[0].landmark
99
  features = extract_features(frame_rgb, landmarks)
100
-
101
  test_values = {label: models[label].predict([features])[0] for label in models}
102
  heart_rate = int(60 + 30 * np.sin(np.mean(frame_rgb) / 255.0 * np.pi))
103
  spo2 = min(100, max(90, 97 + (heart_rate % 5 - 2)))
104
  rr = int(12 + abs(heart_rate % 5 - 2))
105
-
106
  html_output = "".join([
107
  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))]),
108
  build_table("🧬 Iron Panel", [("Iron", test_values["Iron"], (60, 170)), ("Ferritin", test_values["Ferritin"], (30, 300)), ("TIBC", test_values["TIBC"], (250, 400))]),
@@ -112,51 +103,23 @@ def analyze_face(image):
112
  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))]),
113
  build_table("🩹 Other Indicators", [("Cortisol", test_values["Cortisol"], (5, 25)), ("Albumin", test_values["Albumin"], (3.5, 5.5))])
114
  ])
115
-
116
- summary = "<div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#fcfcfc;'>"
117
  summary += "<h4>📝 Summary for You</h4><ul>"
118
  if test_values["Hemoglobin"] < 13.5:
119
- summary += "<li>Your hemoglobin is a bit low — this could mean mild anemia. Consider a CBC test and iron supplements.</li>"
120
  if test_values["Iron"] < 60 or test_values["Ferritin"] < 30:
121
- summary += "<li>Signs of low iron storage detected. An iron profile blood test is recommended.</li>"
122
  if test_values["Bilirubin"] > 1.2:
123
- summary += "<li>Some signs of jaundice were detected. Please consult for a Liver Function Test (LFT).</li>"
124
  if test_values["HbA1c"] > 5.7:
125
- summary += "<li>Your HbA1c is slightly elevated this can signal pre-diabetes. A fasting glucose test may help.</li>"
126
  if spo2 < 95:
127
- summary += "<li>Oxygen levels appear below normal. Please recheck with a pulse oximeter if symptoms persist.</li>"
128
- 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>"
129
-
130
  html_output += summary
131
-
132
  html_output += "<br><div style='margin-top:20px;padding:12px;border:2px solid #2d87f0;background:#f2faff;text-align:center;border-radius:8px;'>"
133
- html_output += "<h4>📞 Book a Lab Test</h4>"
134
- html_output += "<p>Prefer to get your tests confirmed at a nearby center? Click below to find certified labs in your area.</p>"
135
- html_output += "<button style='padding:10px 20px;background:#007BFF;color:#fff;border:none;border-radius:5px;cursor:pointer;'>Find Labs Near Me</button>"
136
- html_output += "</div>"
137
-
138
- lang_blocks = """
139
- <div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#f9f9f9;'>
140
- <h4>🗣️ Summary in Your Language</h4>
141
- <details><summary><b>Hindi</b></summary><ul>
142
- <li>आपका हीमोग्लोबिन थोड़ा कम है — यह हल्के एनीमिया का संकेत हो सकता है। कृपया CBC और आयरन टेस्ट करवाएं।</li>
143
- <li>लो आयरन स्टोरेज देखा गया है। एक आयरन प्रोफाइल टेस्ट की सिफारिश की जाती है।</li>
144
- <li>जॉन्डिस के लक्षण देखे गए हैं। कृपया LFT करवाएं।</li>
145
- <li>HbA1c थोड़ा बढ़ा हुआ है — यह प्री-डायबिटीज़ का संकेत हो सकता है।</li>
146
- <li>ऑक्सीजन स्तर कम दिख रहा है। पल्स ऑक्सीमीटर से दोबारा जांचें।</li>
147
- </ul></details>
148
-
149
- <details><summary><b>Telugu</b></summary><ul>
150
- <li>మీ హిమోగ్లోబిన్ కొంచెం తక్కువగా ఉంది — ఇది తేలికపాటి అనీమియా సూచించవచ్చు. CBC, Iron పరీక్ష చేయించండి.</li>
151
- <li>Iron నిల్వలు తక్కువగా కనిపించాయి. Iron ప్రొఫైల్ బ్లడ్ టెస్ట్ చేయించండి.</li>
152
- <li>జాండీస్ సంకేతాలు గుర్తించబడ్డాయి. LFT చేయించండి.</li>
153
- <li>HbA1c కొంచెం పెరిగింది — ఇది ప్రీ-డయాబెటిస్ సూచించవచ్చు.</li>
154
- <li>ఆక్సిజన్ స్థాయి తక్కువగా ఉంది. తిరిగి పరీక్షించండి.</li>
155
- </ul></details>
156
- </div>
157
- """
158
-
159
- html_output += lang_blocks
160
  return html_output, frame_rgb
161
 
162
  with gr.Blocks() as demo:
@@ -164,20 +127,14 @@ with gr.Blocks() as demo:
164
  # 🧠 Face-Based Lab Test AI Report
165
  Upload a face photo to infer health diagnostics with AI-based visual markers.
166
  """)
167
-
168
  with gr.Row():
169
- with gr.Column(scale=1):
170
  image_input = gr.Image(type="numpy", label="📸 Upload Face Image")
171
  submit_btn = gr.Button("🔍 Analyze")
172
- with gr.Column(scale=2):
173
  result_html = gr.HTML(label="🧪 Health Report Table")
174
  result_image = gr.Image(label="📷 Face Scan Annotated")
175
-
176
  submit_btn.click(fn=analyze_face, inputs=image_input, outputs=[result_html, result_image])
177
-
178
- gr.Markdown("""
179
- ---
180
- ✅ Table Format • AI-Powered Prediction • 30 Tests Integrated
181
- """)
182
 
183
  demo.launch()
 
13
  def extract_features(image, landmarks):
14
  mean_intensity = np.mean(image)
15
  h, w, _ = image.shape
 
16
  bbox_width = max(pt.x for pt in landmarks) - min(pt.x for pt in landmarks)
17
  bbox_height = max(pt.y for pt in landmarks) - min(pt.y for pt in landmarks)
 
18
  def dist(p1, p2):
19
  return ((p1.x - p2.x)**2 + (p1.y - p2.y)**2) ** 0.5
 
20
  eye_dist = dist(landmarks[33], landmarks[263])
21
  nose_len = dist(landmarks[1], landmarks[2]) + dist(landmarks[2], landmarks[98])
22
  jaw_width = dist(landmarks[234], landmarks[454])
 
23
  left_cheek = landmarks[234]
24
  right_cheek = landmarks[454]
25
  cx1, cy1 = int(left_cheek.x * w), int(left_cheek.y * h)
 
27
  skin_tone1 = np.mean(image[cy1-5:cy1+5, cx1-5:cx1+5]) if 5 <= cy1 < h-5 and 5 <= cx1 < w-5 else 0
28
  skin_tone2 = np.mean(image[cy2-5:cy2+5, cx2-5:cx2+5]) if 5 <= cy2 < h-5 and 5 <= cx2 < w-5 else 0
29
  avg_skin_tone = (skin_tone1 + skin_tone2) / 2
 
30
  return [mean_intensity, bbox_width, bbox_height, eye_dist, nose_len, jaw_width, avg_skin_tone]
31
 
32
  def train_model(output_range):
 
84
  def analyze_face(image):
85
  if image is None:
86
  return "<div style='color:red;'>⚠️ Error: No image provided.</div>", None
 
87
  frame_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
88
  result = face_mesh.process(frame_rgb)
89
  if not result.multi_face_landmarks:
90
  return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
 
91
  landmarks = result.multi_face_landmarks[0].landmark
92
  features = extract_features(frame_rgb, landmarks)
 
93
  test_values = {label: models[label].predict([features])[0] for label in models}
94
  heart_rate = int(60 + 30 * np.sin(np.mean(frame_rgb) / 255.0 * np.pi))
95
  spo2 = min(100, max(90, 97 + (heart_rate % 5 - 2)))
96
  rr = int(12 + abs(heart_rate % 5 - 2))
 
97
  html_output = "".join([
98
  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))]),
99
  build_table("🧬 Iron Panel", [("Iron", test_values["Iron"], (60, 170)), ("Ferritin", test_values["Ferritin"], (30, 300)), ("TIBC", test_values["TIBC"], (250, 400))]),
 
103
  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))]),
104
  build_table("🩹 Other Indicators", [("Cortisol", test_values["Cortisol"], (5, 25)), ("Albumin", test_values["Albumin"], (3.5, 5.5))])
105
  ])
106
+ summary = "<div style='margin-top:20px;padding:12px;border:1px dashed #999;background:#fcfcfc;'>"
 
107
  summary += "<h4>📝 Summary for You</h4><ul>"
108
  if test_values["Hemoglobin"] < 13.5:
109
+ summary += "<li>Your hemoglobin is a bit low — this could mean mild anemia.</li>"
110
  if test_values["Iron"] < 60 or test_values["Ferritin"] < 30:
111
+ summary += "<li>Low iron storage detected consider an iron profile test.</li>"
112
  if test_values["Bilirubin"] > 1.2:
113
+ summary += "<li>Elevated bilirubin possible jaundice. Recommend LFT.</li>"
114
  if test_values["HbA1c"] > 5.7:
115
+ summary += "<li>High HbA1c — prediabetes indication. Recommend glucose check.</li>"
116
  if spo2 < 95:
117
+ summary += "<li>Low SpO₂ suggest retesting with a pulse oximeter.</li>"
118
+ summary += "</ul><p><strong>💡 Tip:</strong> This is an AI-based estimate. Please follow up with a lab.</p></div>"
 
119
  html_output += summary
 
120
  html_output += "<br><div style='margin-top:20px;padding:12px;border:2px solid #2d87f0;background:#f2faff;text-align:center;border-radius:8px;'>"
121
+ html_output += "<h4>📞 Book a Lab Test</h4><p>Prefer confirmation? Find certified labs near you.</p>"
122
+ html_output += "<button style='padding:10px 20px;background:#007BFF;color:#fff;border:none;border-radius:5px;cursor:pointer;'>Find Labs Near Me</button></div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  return html_output, frame_rgb
124
 
125
  with gr.Blocks() as demo:
 
127
  # 🧠 Face-Based Lab Test AI Report
128
  Upload a face photo to infer health diagnostics with AI-based visual markers.
129
  """)
 
130
  with gr.Row():
131
+ with gr.Column():
132
  image_input = gr.Image(type="numpy", label="📸 Upload Face Image")
133
  submit_btn = gr.Button("🔍 Analyze")
134
+ with gr.Column():
135
  result_html = gr.HTML(label="🧪 Health Report Table")
136
  result_image = gr.Image(label="📷 Face Scan Annotated")
 
137
  submit_btn.click(fn=analyze_face, inputs=image_input, outputs=[result_html, result_image])
138
+ gr.Markdown("---\n✅ Table Format • AI Prediction • Dynamic Summary • Multilingual Support • CTA")
 
 
 
 
139
 
140
  demo.launch()