|
| 1 | +from flask import Flask, request, jsonify |
| 2 | +import tensorflow as tf |
| 3 | +import numpy as np |
| 4 | +from PIL import Image |
| 5 | +import io |
| 6 | +from flask_cors import CORS |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | +CORS(app) # Enable CORS for all routes |
| 10 | + |
| 11 | +# Load TFLite Model and allocate tensors |
| 12 | +interpreter = tf.lite.Interpreter(model_path="plant_disease_model.tflite") |
| 13 | +interpreter.allocate_tensors() |
| 14 | + |
| 15 | +# Get input and output tensors |
| 16 | +input_details = interpreter.get_input_details() |
| 17 | +output_details = interpreter.get_output_details() |
| 18 | + |
| 19 | +# Define class names |
| 20 | +CLASS_NAMES = [ |
| 21 | + 'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust', 'Apple___healthy', |
| 22 | + 'Blueberry___healthy', 'Cherry_(including_sour)___Powdery_mildew', |
| 23 | + 'Cherry_(including_sour)___healthy', 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot', |
| 24 | + 'Corn_(maize)___Common_rust_', 'Corn_(maize)___Northern_Leaf_Blight', 'Corn_(maize)___healthy', |
| 25 | + 'Grape___Black_rot', 'Grape___Esca_(Black_Measles)', 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)', |
| 26 | + 'Grape___healthy', 'Orange___Haunglongbing_(Citrus_greening)', 'Peach___Bacterial_spot', |
| 27 | + 'Peach___healthy', 'Pepper,_bell___Bacterial_spot', 'Pepper,_bell___healthy', |
| 28 | + 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', |
| 29 | + 'Raspberry___healthy', 'Soybean___healthy', 'Squash___Powdery_mildew', |
| 30 | + 'Strawberry___Leaf_scorch', 'Strawberry___healthy', 'Tomato___Bacterial_spot', |
| 31 | + 'Tomato___Early_blight', 'Tomato___Late_blight', 'Tomato___Leaf_Mold', |
| 32 | + 'Tomato___Septoria_leaf_spot', 'Tomato___Spider_mites Two-spotted_spider_mite', |
| 33 | + 'Tomato___Target_Spot', 'Tomato___Tomato_Yellow_Leaf_Curl_Virus', 'Tomato___Tomato_mosaic_virus', |
| 34 | + 'Tomato___healthy' |
| 35 | +] |
| 36 | + |
| 37 | +def model_prediction(image_bytes): |
| 38 | + try: |
| 39 | + # Load and preprocess the image |
| 40 | + image = Image.open(io.BytesIO(image_bytes)).convert('RGB') |
| 41 | + image = image.resize((224, 224)) |
| 42 | + input_arr = np.array(image) |
| 43 | + input_arr = np.expand_dims(input_arr, axis=0).astype(np.float32) / 255.0 |
| 44 | + |
| 45 | + # Set the tensor for the input |
| 46 | + interpreter.set_tensor(input_details[0]['index'], input_arr) |
| 47 | + |
| 48 | + # Run the model |
| 49 | + interpreter.invoke() |
| 50 | + |
| 51 | + # Get the output tensor and return the index of the max element |
| 52 | + output_data = interpreter.get_tensor(output_details[0]['index']) |
| 53 | + return int(np.argmax(output_data)) |
| 54 | + except Exception as e: |
| 55 | + print(f"Error in model_prediction: {e}") |
| 56 | + return None |
| 57 | + |
| 58 | +@app.route('/predict', methods=['POST']) |
| 59 | +def predict(): |
| 60 | + if 'image' not in request.files: |
| 61 | + return jsonify({"error": "No image part in the request"}), 400 |
| 62 | + |
| 63 | + file = request.files['image'] |
| 64 | + |
| 65 | + if file.filename == '': |
| 66 | + return jsonify({"error": "No image selected for uploading"}), 400 |
| 67 | + |
| 68 | + try: |
| 69 | + img_bytes = file.read() |
| 70 | + result_index = model_prediction(img_bytes) |
| 71 | + |
| 72 | + if result_index is None: |
| 73 | + return jsonify({"error": "Prediction failed"}), 500 |
| 74 | + |
| 75 | + class_name = CLASS_NAMES[result_index] |
| 76 | + return jsonify({"prediction": class_name}), 200 |
| 77 | + |
| 78 | + except Exception as e: |
| 79 | + print(f"Error in /predict: {e}") |
| 80 | + return jsonify({"error": "An error occurred during prediction"}), 500 |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + app.run(debug=True) |
0 commit comments