Spaces:
Runtime error
Runtime error
File size: 1,343 Bytes
827bcf6 |
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 |
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)
|