from flask import Flask, request, jsonify from tensorflow.keras.models import load_model from PIL import Image import numpy as np import os app = Flask(__name__) # Load the model model = load_model('mobilenet_glaucoma_model.h5', compile=False) def preprocess_image(img): img = img.resize((224, 224)) img = np.array(img) / 255.0 img = np.expand_dims(img, axis=0) return img @app.route('/') def home(): return "Glaucoma Detection Flask API is running!" @app.route('/predict', methods=['POST']) def predict(): if 'file' not in request.files: return jsonify({'error': 'No file uploaded'}), 400 file = request.files['file'] if file.filename == '': return jsonify({'error': 'No file selected'}), 400 try: img = Image.open(file.stream).convert('RGB') img = preprocess_image(img) prediction = model.predict(img)[0] result = 'Glaucoma' if prediction[0] < 0.5 else 'Normal' return jsonify({ 'prediction': result, 'confidence': float(prediction[0]) }) except Exception as e: return jsonify({'error': str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)