from flask import Flask, request, jsonify, render_template from tensorflow.keras.models import model_from_json from PIL import Image import numpy as np app = Flask(__name__) # Load model architecture from JSON file with open("model.json", "r") as json_file: loaded_model_json = json_file.read() Model = model_from_json(loaded_model_json) Model.load_weights("model.h5") print("Loaded model from disk") # predict def preprocess_image(image): img = Image.open(image) img = img.resize((224, 224)) img_array = np.expand_dims(img, axis=0) return img_array @app.route('/') def index(): return render_template('index.html') @app.route('/predict', methods=['POST']) def predict(): if 'image' not in request.files: return jsonify({'error': 'No file part'}) file = request.files['image'] if file.filename == '': return jsonify({'error': 'No selected file'}) if file: img = preprocess_image(file) predictions = Model.predict(img) predicted_class_index = int(np.argmax(predictions, axis=1)[0]) # Convert to int class_labels = ['pituitary', 'notumor', 'meningioma', 'glioma'] predicted_class_label = class_labels[predicted_class_index] return jsonify({'class': predicted_class_label}) if __name__ == '__main__': app.run(debug=True)