Spaces:
Sleeping
Sleeping
# Install required packages | |
#!pip install gradio tensorflow opencv-python scikit-learn | |
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
import cv2 | |
from tensorflow.keras.applications import ResNet50 | |
from tensorflow.keras.applications.resnet50 import preprocess_input | |
from tensorflow.keras.preprocessing.image import img_to_array | |
from sklearn.preprocessing import StandardScaler | |
import pandas as pd | |
class SurgicalAssistSystem: | |
def __init__(self): | |
# Initialize the image processing model (ResNet50 pretrained) | |
self.image_model = ResNet50(weights='imagenet', include_top=False, | |
input_shape=(224, 224, 3)) | |
# Initialize scaler for vital signs | |
self.scaler = StandardScaler() | |
# Define normal ranges for vital signs | |
self.vital_ranges = { | |
'heart_rate': (60, 100), | |
'blood_pressure_systolic': (90, 140), | |
'blood_pressure_diastolic': (60, 90), | |
'oxygen_saturation': (95, 100), | |
'temperature': (36.5, 37.5) | |
} | |
def preprocess_image(self, image): | |
# Resize image to expected dimensions | |
image = cv2.resize(image, (224, 224)) | |
image = img_to_array(image) | |
image = np.expand_dims(image, axis=0) | |
image = preprocess_input(image) | |
return image | |
def analyze_image(self, image): | |
# Preprocess and analyze the image | |
processed_image = self.preprocess_image(image) | |
features = self.image_model.predict(processed_image) | |
# Simplified analysis - detecting potential anomalies | |
feature_mean = np.mean(features) | |
if feature_mean > 0.5: | |
return "Potential anomaly detected in the surgical field" | |
else: | |
return "No immediate concerns in the surgical field" | |
def analyze_vitals(self, vitals_dict): | |
alerts = [] | |
for vital, value in vitals_dict.items(): | |
if vital in self.vital_ranges: | |
min_val, max_val = self.vital_ranges[vital] | |
if value < min_val: | |
alerts.append(f"Warning: {vital} is below normal range") | |
elif value > max_val: | |
alerts.append(f"Warning: {vital} is above normal range") | |
if not alerts: | |
return "All vital signs are within normal ranges" | |
return "\n".join(alerts) | |
def generate_recommendations(self, image_analysis, vitals_analysis): | |
recommendations = [] | |
if "anomaly" in image_analysis.lower(): | |
recommendations.append("- Recommend detailed inspection of highlighted area") | |
recommendations.append("- Consider additional imaging") | |
if "warning" in vitals_analysis.lower(): | |
recommendations.append("- Monitor vital signs closely") | |
recommendations.append("- Consider adjusting procedure parameters") | |
if not recommendations: | |
recommendations.append("- Proceed with standard protocol") | |
recommendations.append("- Continue routine monitoring") | |
return "\n".join(recommendations) | |
# Initialize the system | |
system = SurgicalAssistSystem() | |
def process_surgical_data(image, heart_rate, blood_pressure_systolic, | |
blood_pressure_diastolic, oxygen_saturation, temperature): | |
# Convert image to numpy array if it's not already | |
if isinstance(image, str): | |
return "Please provide a valid image" | |
# Create vitals dictionary | |
vitals = { | |
'heart_rate': heart_rate, | |
'blood_pressure_systolic': blood_pressure_systolic, | |
'blood_pressure_diastolic': blood_pressure_diastolic, | |
'oxygen_saturation': oxygen_saturation, | |
'temperature': temperature | |
} | |
# Analyze image and vitals | |
image_analysis = system.analyze_image(image) | |
vitals_analysis = system.analyze_vitals(vitals) | |
recommendations = system.generate_recommendations(image_analysis, vitals_analysis) | |
return (f"Image Analysis:\n{image_analysis}\n\n" | |
f"Vitals Analysis:\n{vitals_analysis}\n\n" | |
f"Recommendations:\n{recommendations}") | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=process_surgical_data, | |
inputs=[ | |
gr.Image(label="Surgical Field Image"), | |
gr.Number(label="Heart Rate (bpm)"), | |
gr.Number(label="Blood Pressure - Systolic (mmHg)"), | |
gr.Number(label="Blood Pressure - Diastolic (mmHg)"), | |
gr.Number(label="Oxygen Saturation (%)"), | |
gr.Number(label="Temperature (°C)") | |
], | |
outputs=gr.Textbox(label="Analysis Results"), | |
title="Surgical Assistance System", | |
description="Upload an image of the surgical field and enter vital signs for analysis and recommendations." | |
) | |
# Launch the interface | |
iface.launch(share=True) |