Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
|
@@ -7,6 +6,7 @@ from sklearn.linear_model import LinearRegression
|
|
| 7 |
import random
|
| 8 |
import base64
|
| 9 |
import joblib
|
|
|
|
| 10 |
|
| 11 |
# Initialize the face mesh model
|
| 12 |
mp_face_mesh = mp.solutions.face_mesh
|
|
@@ -15,7 +15,6 @@ face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True,
|
|
| 15 |
refine_landmarks=True,
|
| 16 |
min_detection_confidence=0.5)
|
| 17 |
|
| 18 |
-
|
| 19 |
# Functions for feature extraction
|
| 20 |
def extract_features(image, landmarks):
|
| 21 |
red_channel = image[:, :, 2]
|
|
@@ -28,7 +27,6 @@ def extract_features(image, landmarks):
|
|
| 28 |
|
| 29 |
return [red_percent, green_percent, blue_percent]
|
| 30 |
|
| 31 |
-
|
| 32 |
def train_model(output_range):
|
| 33 |
X = [[
|
| 34 |
random.uniform(0.2, 0.5),
|
|
@@ -115,6 +113,25 @@ def build_table(title, rows):
|
|
| 115 |
return html
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Build health card layout
|
| 119 |
def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""):
|
| 120 |
from datetime import datetime
|
|
@@ -122,7 +139,6 @@ def build_health_card(profile_image, test_results, summary, patient_name="", pat
|
|
| 122 |
|
| 123 |
html = f"""
|
| 124 |
<div id="health-card" style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 700px; margin: 20px auto; border-radius: 16px; background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%); border: 2px solid #ddd; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15); padding: 30px; color: #1a1a1a;">
|
| 125 |
-
|
| 126 |
<div style="background-color: rgba(255, 255, 255, 0.9); border-radius: 12px; padding: 20px; margin-bottom: 25px; border: 1px solid #e0e0e0;">
|
| 127 |
<div style="display: flex; align-items: center; margin-bottom: 15px;">
|
| 128 |
<div style="background: linear-gradient(135deg, #64b5f6, #42a5f5); padding: 8px 16px; border-radius: 8px; margin-right: 20px;">
|
|
@@ -157,40 +173,7 @@ def build_health_card(profile_image, test_results, summary, patient_name="", pat
|
|
| 157 |
{summary}
|
| 158 |
</div>
|
| 159 |
</div>
|
| 160 |
-
|
| 161 |
-
<div style="display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;">
|
| 162 |
-
<button onclick="window.print()" style="padding: 12px 24px; background: linear-gradient(135deg, #4caf50, #45a049); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3); transition: all 0.3s;">
|
| 163 |
-
📥 Download Report
|
| 164 |
-
</button>
|
| 165 |
-
<button style="padding: 12px 24px; background: linear-gradient(135deg, #2196f3, #1976d2); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px; box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);">
|
| 166 |
-
📞 Find Labs Near Me
|
| 167 |
-
</button>
|
| 168 |
-
</div>
|
| 169 |
</div>
|
| 170 |
-
|
| 171 |
-
<style>
|
| 172 |
-
@media print {{
|
| 173 |
-
body * {{
|
| 174 |
-
visibility: hidden;
|
| 175 |
-
}}
|
| 176 |
-
#health-card, #health-card * {{
|
| 177 |
-
visibility: visible;
|
| 178 |
-
}}
|
| 179 |
-
#health-card {{
|
| 180 |
-
position: absolute;
|
| 181 |
-
left: 0;
|
| 182 |
-
top: 0;
|
| 183 |
-
width: 100% !important;
|
| 184 |
-
max-width: none !important;
|
| 185 |
-
margin: 0 !important;
|
| 186 |
-
box-shadow: none !important;
|
| 187 |
-
border: none !important;
|
| 188 |
-
}}
|
| 189 |
-
button {{
|
| 190 |
-
display: none !important;
|
| 191 |
-
}}
|
| 192 |
-
}}
|
| 193 |
-
</style>
|
| 194 |
"""
|
| 195 |
return html
|
| 196 |
|
|
@@ -216,7 +199,6 @@ def analyze_face(input_data):
|
|
| 216 |
# Resize image to reduce processing time
|
| 217 |
frame = cv2.resize(frame, (640, 480)) # Adjust resolution for Replit
|
| 218 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 219 |
-
# Provide image dimensions to mediapipe to avoid NORM_RECT warning
|
| 220 |
result = face_mesh.process(frame_rgb)
|
| 221 |
if not result.multi_face_landmarks:
|
| 222 |
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
|
@@ -299,7 +281,10 @@ def analyze_face(input_data):
|
|
| 299 |
current_patient_details['gender'],
|
| 300 |
current_patient_details['id']
|
| 301 |
)
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
|
| 305 |
# Modified route_inputs function
|
|
@@ -318,8 +303,8 @@ def route_inputs(mode, image, video, patient_name, patient_age, patient_gender,
|
|
| 318 |
'id': patient_id
|
| 319 |
}
|
| 320 |
|
| 321 |
-
|
| 322 |
-
return
|
| 323 |
|
| 324 |
|
| 325 |
# Create Gradio interface
|
|
@@ -342,12 +327,12 @@ with gr.Blocks() as demo:
|
|
| 342 |
sources=["upload", "webcam"])
|
| 343 |
submit_btn = gr.Button("🔍 Analyze")
|
| 344 |
with gr.Column():
|
| 345 |
-
|
| 346 |
-
|
| 347 |
|
| 348 |
submit_btn.click(fn=route_inputs,
|
| 349 |
inputs=[mode_selector, image_input, video_input, patient_name, patient_age, patient_gender, patient_id],
|
| 350 |
-
outputs=[
|
| 351 |
|
| 352 |
# Launch Gradio for Replit
|
| 353 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 6 |
import random
|
| 7 |
import base64
|
| 8 |
import joblib
|
| 9 |
+
from fpdf import FPDF
|
| 10 |
|
| 11 |
# Initialize the face mesh model
|
| 12 |
mp_face_mesh = mp.solutions.face_mesh
|
|
|
|
| 15 |
refine_landmarks=True,
|
| 16 |
min_detection_confidence=0.5)
|
| 17 |
|
|
|
|
| 18 |
# Functions for feature extraction
|
| 19 |
def extract_features(image, landmarks):
|
| 20 |
red_channel = image[:, :, 2]
|
|
|
|
| 27 |
|
| 28 |
return [red_percent, green_percent, blue_percent]
|
| 29 |
|
|
|
|
| 30 |
def train_model(output_range):
|
| 31 |
X = [[
|
| 32 |
random.uniform(0.2, 0.5),
|
|
|
|
| 113 |
return html
|
| 114 |
|
| 115 |
|
| 116 |
+
# Generate PDF report using FPDF
|
| 117 |
+
def generate_pdf(report_html):
|
| 118 |
+
pdf = FPDF()
|
| 119 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 120 |
+
pdf.add_page()
|
| 121 |
+
pdf.set_font("Arial", size=12)
|
| 122 |
+
|
| 123 |
+
# Add a title
|
| 124 |
+
pdf.cell(200, 10, txt="Face-Based Health Report", ln=True, align="C")
|
| 125 |
+
|
| 126 |
+
# Write the report HTML content into the PDF
|
| 127 |
+
pdf.multi_cell(0, 10, txt=report_html)
|
| 128 |
+
|
| 129 |
+
# Save the PDF to a file
|
| 130 |
+
pdf_output = "/mnt/data/health_report.pdf"
|
| 131 |
+
pdf.output(pdf_output)
|
| 132 |
+
return pdf_output
|
| 133 |
+
|
| 134 |
+
|
| 135 |
# Build health card layout
|
| 136 |
def build_health_card(profile_image, test_results, summary, patient_name="", patient_age="", patient_gender="", patient_id=""):
|
| 137 |
from datetime import datetime
|
|
|
|
| 139 |
|
| 140 |
html = f"""
|
| 141 |
<div id="health-card" style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; max-width: 700px; margin: 20px auto; border-radius: 16px; background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%); border: 2px solid #ddd; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15); padding: 30px; color: #1a1a1a;">
|
|
|
|
| 142 |
<div style="background-color: rgba(255, 255, 255, 0.9); border-radius: 12px; padding: 20px; margin-bottom: 25px; border: 1px solid #e0e0e0;">
|
| 143 |
<div style="display: flex; align-items: center; margin-bottom: 15px;">
|
| 144 |
<div style="background: linear-gradient(135deg, #64b5f6, #42a5f5); padding: 8px 16px; border-radius: 8px; margin-right: 20px;">
|
|
|
|
| 173 |
{summary}
|
| 174 |
</div>
|
| 175 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
"""
|
| 178 |
return html
|
| 179 |
|
|
|
|
| 199 |
# Resize image to reduce processing time
|
| 200 |
frame = cv2.resize(frame, (640, 480)) # Adjust resolution for Replit
|
| 201 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
| 202 |
result = face_mesh.process(frame_rgb)
|
| 203 |
if not result.multi_face_landmarks:
|
| 204 |
return "<div style='color:red;'>⚠️ Error: Face not detected.</div>", None
|
|
|
|
| 281 |
current_patient_details['gender'],
|
| 282 |
current_patient_details['id']
|
| 283 |
)
|
| 284 |
+
|
| 285 |
+
# Generate PDF
|
| 286 |
+
pdf_file_path = generate_pdf(health_card_html)
|
| 287 |
+
return pdf_file_path
|
| 288 |
|
| 289 |
|
| 290 |
# Modified route_inputs function
|
|
|
|
| 303 |
'id': patient_id
|
| 304 |
}
|
| 305 |
|
| 306 |
+
pdf_file_path = analyze_face(image if mode == "Image" else video)
|
| 307 |
+
return pdf_file_path
|
| 308 |
|
| 309 |
|
| 310 |
# Create Gradio interface
|
|
|
|
| 327 |
sources=["upload", "webcam"])
|
| 328 |
submit_btn = gr.Button("🔍 Analyze")
|
| 329 |
with gr.Column():
|
| 330 |
+
download_btn = gr.Button("Download Report (PDF)")
|
| 331 |
+
download_btn.download(pdf_file_path, "health_report.pdf")
|
| 332 |
|
| 333 |
submit_btn.click(fn=route_inputs,
|
| 334 |
inputs=[mode_selector, image_input, video_input, patient_name, patient_age, patient_gender, patient_id],
|
| 335 |
+
outputs=[download_btn])
|
| 336 |
|
| 337 |
# Launch Gradio for Replit
|
| 338 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|