File size: 4,861 Bytes
cbde55b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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)