vinuka/leafsecurehost
0
1from flask import Flask, request, jsonify2from tensorflow.keras.models import load_model3from tensorflow.keras.preprocessing import image4import numpy as np5from PIL import Image6import io7import os8import base649from flask_cors import CORS10 11 12 13app = Flask(__name__)14CORS(app)15 16#dataset clasess17class_name = {18 0: 'Blister Blight',19 1: 'Brown Blight',20 2: 'Gray Blight',21 3: 'Healthy',22 4: 'White Spot'23}24 25#load saved model26model_dir = './CNN_TEA_MODEL.h5'27model = load_model(model_dir)28 29 30 31 32 33@app.route("/predict", methods=["POST"])34def predictTest():35 36 if 'file' not in request.files:37 return jsonify({'error': 'No file part'}), 40038 file = request.files['file']39 if file.filename == '':40 return jsonify({'error': 'No selected file'}), 40041 if file:42 # Convert the file storage to PIL Image and ensure it's in RGB43 img = Image.open(io.BytesIO(file.read())).convert('RGB') # Added .convert('RGB')44 img = img.resize((256, 256))45 img_array = np.array(img)46 img_array = np.expand_dims(img_array, axis=0)47 img_array = img_array / 255.0 # Normalize48 49 predictions = model.predict(img_array)50 #get the class with the highest probability51 predicted_class = np.argmax(predictions, axis=1)52 predicted_class_name = class_name[predicted_class[0]]53 54 result = {"class": predicted_class_name}55 print("Prediction: ", result)56 print(predictions)57 58 return jsonify({'prediction': result})59 60 61 62 63 64 65if __name__ == "__main__":66 app.run(debug=True)67 68 69 