Spaces:
Sleeping
Sleeping
File size: 833 Bytes
05a7a5a 59135b8 05a7a5a 59135b8 | 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 | from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
# ✅ Define your class names in the exact training order
class_names = [
"Pepper__bell___Bacterial_spot",
"Pepper__bell___healthy",
"Potato___healthy",
"Potato___Early_blight"
]
# ✅ Load your trained model
# Make sure this file is in the same folder as model.py
model = load_model("final_plantvillage_model.keras")
def predict(image: Image.Image):
"""
Predict the class of a given PIL image.
"""
image = image.resize((224, 224))
img_array = np.array(image) / 255.0
img_array = np.expand_dims(img_array, axis=0)
predictions = model.predict(img_array)
predicted_class = class_names[np.argmax(predictions)]
confidence = float(np.max(predictions))
return {predicted_class: confidence}
|