Facebook
From Amanat, 1 Month ago, written in HTML5.
Embed
Download Paste or View Raw
Hits: 151
  1. from flask import Flask, request, jsonify, render_template
  2. import tensorflow as tf
  3. from tensorflow.keras.models import load_model
  4. import numpy as np
  5. from PIL import Image
  6. import io
  7.  
  8. app = Flask(name)
  9.  
  10. # Load your pre-trained model
  11. model = load_model('D:Versity8TH SEMESTERbrain_tumorBrain-Tumor-Segmentation-in-TensorFlow-2.0-mainfiles/model.h5')
  12.  
  13. def preprocess_image(image):
  14.     # Preprocess the image as required by your model (resize, normalize, etc.)
  15.     image = image.resize((256, 256))  # Example size
  16.     image = np.array(image) / 255.0   # Normalize
  17.     image = np.expand_dims(image, axis=0)  # Add batch dimension
  18.     return image
  19.  
  20. @app.route('/')
  21. def home():
  22.     return render_template('index.html')  # Serve your HTML page
  23.  
  24. @app.route('/predict', methods=['POST'])
  25. def predict():
  26.     if 'file' not in request.files:
  27.         return "No file part"
  28.     file = request.files['file']
  29.     if file.filename == '':
  30.         return "No selected file"
  31.     if file:
  32.         img = Image.open(io.BytesIO(file.read()))
  33.         img = preprocess_image(img)
  34.         prediction = model.predict(img)
  35.         result = np.argmax(prediction, axis=-1)
  36.         result = result[0].tolist()  # Convert the prediction to a list format
  37.         return jsonify(result)
  38.  
  39. if name == 'main':
  40.     app.run(debug=True)