Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install required packages
|
2 |
+
#!pip install gradio tensorflow opencv-python scikit-learn
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import tensorflow as tf
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
8 |
+
from tensorflow.keras.applications import ResNet50
|
9 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
10 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
11 |
+
from sklearn.preprocessing import StandardScaler
|
12 |
+
import pandas as pd
|
13 |
+
|
14 |
+
class SurgicalAssistSystem:
|
15 |
+
def __init__(self):
|
16 |
+
# Initialize the image processing model (ResNet50 pretrained)
|
17 |
+
self.image_model = ResNet50(weights='imagenet', include_top=False,
|
18 |
+
input_shape=(224, 224, 3))
|
19 |
+
|
20 |
+
# Initialize scaler for vital signs
|
21 |
+
self.scaler = StandardScaler()
|
22 |
+
|
23 |
+
# Define normal ranges for vital signs
|
24 |
+
self.vital_ranges = {
|
25 |
+
'heart_rate': (60, 100),
|
26 |
+
'blood_pressure_systolic': (90, 140),
|
27 |
+
'blood_pressure_diastolic': (60, 90),
|
28 |
+
'oxygen_saturation': (95, 100),
|
29 |
+
'temperature': (36.5, 37.5)
|
30 |
+
}
|
31 |
+
|
32 |
+
def preprocess_image(self, image):
|
33 |
+
# Resize image to expected dimensions
|
34 |
+
image = cv2.resize(image, (224, 224))
|
35 |
+
image = img_to_array(image)
|
36 |
+
image = np.expand_dims(image, axis=0)
|
37 |
+
image = preprocess_input(image)
|
38 |
+
return image
|
39 |
+
|
40 |
+
def analyze_image(self, image):
|
41 |
+
# Preprocess and analyze the image
|
42 |
+
processed_image = self.preprocess_image(image)
|
43 |
+
features = self.image_model.predict(processed_image)
|
44 |
+
|
45 |
+
# Simplified analysis - detecting potential anomalies
|
46 |
+
feature_mean = np.mean(features)
|
47 |
+
if feature_mean > 0.5:
|
48 |
+
return "Potential anomaly detected in the surgical field"
|
49 |
+
else:
|
50 |
+
return "No immediate concerns in the surgical field"
|
51 |
+
|
52 |
+
def analyze_vitals(self, vitals_dict):
|
53 |
+
alerts = []
|
54 |
+
for vital, value in vitals_dict.items():
|
55 |
+
if vital in self.vital_ranges:
|
56 |
+
min_val, max_val = self.vital_ranges[vital]
|
57 |
+
if value < min_val:
|
58 |
+
alerts.append(f"Warning: {vital} is below normal range")
|
59 |
+
elif value > max_val:
|
60 |
+
alerts.append(f"Warning: {vital} is above normal range")
|
61 |
+
|
62 |
+
if not alerts:
|
63 |
+
return "All vital signs are within normal ranges"
|
64 |
+
return "\n".join(alerts)
|
65 |
+
|
66 |
+
def generate_recommendations(self, image_analysis, vitals_analysis):
|
67 |
+
recommendations = []
|
68 |
+
|
69 |
+
if "anomaly" in image_analysis.lower():
|
70 |
+
recommendations.append("- Recommend detailed inspection of highlighted area")
|
71 |
+
recommendations.append("- Consider additional imaging")
|
72 |
+
|
73 |
+
if "warning" in vitals_analysis.lower():
|
74 |
+
recommendations.append("- Monitor vital signs closely")
|
75 |
+
recommendations.append("- Consider adjusting procedure parameters")
|
76 |
+
|
77 |
+
if not recommendations:
|
78 |
+
recommendations.append("- Proceed with standard protocol")
|
79 |
+
recommendations.append("- Continue routine monitoring")
|
80 |
+
|
81 |
+
return "\n".join(recommendations)
|
82 |
+
|
83 |
+
# Initialize the system
|
84 |
+
system = SurgicalAssistSystem()
|
85 |
+
|
86 |
+
def process_surgical_data(image, heart_rate, blood_pressure_systolic,
|
87 |
+
blood_pressure_diastolic, oxygen_saturation, temperature):
|
88 |
+
# Convert image to numpy array if it's not already
|
89 |
+
if isinstance(image, str):
|
90 |
+
return "Please provide a valid image"
|
91 |
+
|
92 |
+
# Create vitals dictionary
|
93 |
+
vitals = {
|
94 |
+
'heart_rate': heart_rate,
|
95 |
+
'blood_pressure_systolic': blood_pressure_systolic,
|
96 |
+
'blood_pressure_diastolic': blood_pressure_diastolic,
|
97 |
+
'oxygen_saturation': oxygen_saturation,
|
98 |
+
'temperature': temperature
|
99 |
+
}
|
100 |
+
|
101 |
+
# Analyze image and vitals
|
102 |
+
image_analysis = system.analyze_image(image)
|
103 |
+
vitals_analysis = system.analyze_vitals(vitals)
|
104 |
+
recommendations = system.generate_recommendations(image_analysis, vitals_analysis)
|
105 |
+
|
106 |
+
return (f"Image Analysis:\n{image_analysis}\n\n"
|
107 |
+
f"Vitals Analysis:\n{vitals_analysis}\n\n"
|
108 |
+
f"Recommendations:\n{recommendations}")
|
109 |
+
|
110 |
+
# Create Gradio interface
|
111 |
+
iface = gr.Interface(
|
112 |
+
fn=process_surgical_data,
|
113 |
+
inputs=[
|
114 |
+
gr.Image(label="Surgical Field Image"),
|
115 |
+
gr.Number(label="Heart Rate (bpm)"),
|
116 |
+
gr.Number(label="Blood Pressure - Systolic (mmHg)"),
|
117 |
+
gr.Number(label="Blood Pressure - Diastolic (mmHg)"),
|
118 |
+
gr.Number(label="Oxygen Saturation (%)"),
|
119 |
+
gr.Number(label="Temperature (°C)")
|
120 |
+
],
|
121 |
+
outputs=gr.Textbox(label="Analysis Results"),
|
122 |
+
title="Surgical Assistance System",
|
123 |
+
description="Upload an image of the surgical field and enter vital signs for analysis and recommendations."
|
124 |
+
)
|
125 |
+
|
126 |
+
# Launch the interface
|
127 |
+
iface.launch(share=True)
|