File size: 4,734 Bytes
5bd1dc2
 
 
 
 
 
 
644713d
 
 
 
 
5bd1dc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""

Flask Web Application for Multimodal Glaucoma Detection

Provides web interface for Fundus, OCT, and Multimodal predictions

"""

from flask import Flask, render_template, request, jsonify, send_from_directory
import os
import sys

# Ensure the project root is in sys.path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from pathlib import Path
import base64
from io import BytesIO
from models.load_models import ModelLoader
from models.predict import predict_fundus, predict_oct, predict_multimodal
from utils.gradcam import generate_gradcam_image
import traceback

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max file size

# Ensure upload directory exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

# Load models on startup
print("Loading models...")
model_loader = ModelLoader()
print("Models loaded successfully!")

@app.route('/')
def index():
    """Landing page with mode selection"""
    return render_template('index.html')

@app.route('/fundus')
def fundus_page():
    """Fundus analysis page"""
    return render_template('upload.html', mode='fundus')

@app.route('/oct')
def oct_page():
    """OCT analysis page"""
    return render_template('upload.html', mode='oct')

@app.route('/multimodal')
def multimodal_page():
    """Multimodal analysis page"""
    return render_template('upload.html', mode='multimodal')

@app.route('/about')
def about_page():
    """About page"""
    return render_template('about.html')

@app.route('/predict/fundus', methods=['POST'])
def predict_fundus_endpoint():
    """Fundus prediction endpoint"""
    try:
        if 'fundus_image' not in request.files:
            return jsonify({'error': 'No fundus image provided'}), 400
        
        file = request.files['fundus_image']
        if file.filename == '':
            return jsonify({'error': 'No file selected'}), 400
        
        # Save uploaded file
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], 'fundus_temp.jpg')
        file.save(filepath)
        
        # Get prediction
        result = predict_fundus(filepath, model_loader)
        
        return jsonify(result)
    
    except Exception as e:
        print(f"Error in fundus prediction: {e}")
        traceback.print_exc()
        return jsonify({'error': str(e)}), 500

@app.route('/predict/oct', methods=['POST'])
def predict_oct_endpoint():
    """OCT prediction endpoint"""
    try:
        if 'oct_image' not in request.files:
            return jsonify({'error': 'No OCT image provided'}), 400
        
        file = request.files['oct_image']
        if file.filename == '':
            return jsonify({'error': 'No file selected'}), 400
        
        # Save uploaded file
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], 'oct_temp.jpg')
        file.save(filepath)
        
        # Get prediction
        result = predict_oct(filepath, model_loader)
        
        return jsonify(result)
    
    except Exception as e:
        print(f"Error in OCT prediction: {e}")
        traceback.print_exc()
        return jsonify({'error': str(e)}), 500

@app.route('/predict/multimodal', methods=['POST'])
def predict_multimodal_endpoint():
    """Multimodal prediction endpoint"""
    try:
        if 'fundus_image' not in request.files or 'oct_image' not in request.files:
            return jsonify({'error': 'Both fundus and OCT images required'}), 400
        
        fundus_file = request.files['fundus_image']
        oct_file = request.files['oct_image']
        
        if fundus_file.filename == '' or oct_file.filename == '':
            return jsonify({'error': 'No file selected'}), 400
        
        # Save uploaded files
        fundus_path = os.path.join(app.config['UPLOAD_FOLDER'], 'fundus_temp.jpg')
        oct_path = os.path.join(app.config['UPLOAD_FOLDER'], 'oct_temp.jpg')
        fundus_file.save(fundus_path)
        oct_file.save(oct_path)
        
        # Get prediction
        result = predict_multimodal(fundus_path, oct_path, model_loader)
        
        return jsonify(result)
    
    except Exception as e:
        print(f"Error in multimodal prediction: {e}")
        traceback.print_exc()
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    print("\n" + "="*60)
    print("Multimodal Glaucoma Detection System")
    print("="*60)
    print("Server starting on http://localhost:5000")
    print("Press Ctrl+C to stop")
    print("="*60 + "\n")
    app.run(debug=False, host='0.0.0.0', port=7860)