chinnikrishna1/mymodel
0
1from flask import Flask, render_template, request2import tensorflow as tf3from tensorflow.keras.models import load_model4import numpy as np5from PIL import Image6import io7 8# Initialize Flask app9app = Flask(__name__)10 11# Load the model (make sure the path is correct)12model = load_model('model/my_model.h5')13 14# Define the categories (update with your actual categories)15categories = ['Class 1', 'Class 2', 'Class 3', 'Class 4']16 17# Function to preprocess and predict image18def prepare_image(image):19 image = image.resize((224, 224)) # Resize image to match model input20 image = np.array(image) # Convert to numpy array21 image = np.expand_dims(image, axis=0) # Add batch dimension22 image = image / 255.0 # Normalize image23 return image24 25@app.route('/')26def home():27 return render_template('index.html')28 29@app.route('/predict', methods=['POST'])30def predict():31 if 'file' not in request.files:32 return "No file part"33 file = request.files['file']34 if file.filename == '':35 return "No selected file"36 37 # Open the image and prepare for prediction38 img = Image.open(file.stream)39 img = prepare_image(img)40 41 # Make the prediction42 prediction = model.predict(img)43 predicted_class = np.argmax(prediction, axis=1)[0]44 predicted_percentage = prediction[0][predicted_class] * 10045 46 return render_template('index.html', 47 prediction_text=f'Predicted Class: {categories[predicted_class]} with {predicted_percentage:.2f}% certainty')48 49if __name__ == '__main__':50 app.run(debug=True)51 