Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ["TF_USE_LEGACY_KERAS"] = "1" # tell TF to use tf-keras (legacy)
|
| 3 |
-
|
| 4 |
import streamlit as st
|
| 5 |
import tensorflow as tf
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
# ================== SETTINGS ==================
|
| 10 |
IMG_SIZE = (170, 170) # same as target_size in your ImageDataGenerator
|
|
@@ -16,22 +14,22 @@ CLASS_NAMES = [
|
|
| 16 |
"Leaf Blight",
|
| 17 |
]
|
| 18 |
|
| 19 |
-
|
| 20 |
# =============================================
|
| 21 |
|
| 22 |
|
| 23 |
-
|
| 24 |
@st.cache_resource
|
| 25 |
-
def
|
| 26 |
"""
|
| 27 |
-
Load the
|
| 28 |
"""
|
| 29 |
-
if not os.path.exists(
|
| 30 |
-
st.error(f"Model file '{
|
| 31 |
st.stop()
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def preprocess_image(img: Image.Image) -> np.ndarray:
|
|
@@ -54,16 +52,23 @@ def preprocess_image(img: Image.Image) -> np.ndarray:
|
|
| 54 |
|
| 55 |
def predict_image(img: Image.Image):
|
| 56 |
"""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
"""
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
preds =
|
| 65 |
|
| 66 |
-
# expected: shape (1, 4) with softmax
|
| 67 |
probs = preds[0]
|
| 68 |
idx = int(np.argmax(probs))
|
| 69 |
label = CLASS_NAMES[idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# ================== SETTINGS ==================
|
| 8 |
IMG_SIZE = (170, 170) # same as target_size in your ImageDataGenerator
|
|
|
|
| 14 |
"Leaf Blight",
|
| 15 |
]
|
| 16 |
|
| 17 |
+
TFLITE_MODEL_FILENAME = "grape_disease_model.tflite" # new TFLite model
|
| 18 |
# =============================================
|
| 19 |
|
| 20 |
|
|
|
|
| 21 |
@st.cache_resource
|
| 22 |
+
def load_interpreter():
|
| 23 |
"""
|
| 24 |
+
Load the TFLite model once and cache the interpreter.
|
| 25 |
"""
|
| 26 |
+
if not os.path.exists(TFLITE_MODEL_FILENAME):
|
| 27 |
+
st.error(f"Model file '{TFLITE_MODEL_FILENAME}' not found in this directory.")
|
| 28 |
st.stop()
|
| 29 |
|
| 30 |
+
interpreter = tf.lite.Interpreter(model_path=TFLITE_MODEL_FILENAME)
|
| 31 |
+
interpreter.allocate_tensors()
|
| 32 |
+
return interpreter
|
| 33 |
|
| 34 |
|
| 35 |
def preprocess_image(img: Image.Image) -> np.ndarray:
|
|
|
|
| 52 |
|
| 53 |
def predict_image(img: Image.Image):
|
| 54 |
"""
|
| 55 |
+
Run inference using the TFLite interpreter.
|
| 56 |
+
Returns (predicted_label, confidence, all_probabilities).
|
| 57 |
"""
|
| 58 |
+
interpreter = load_interpreter()
|
| 59 |
+
|
| 60 |
+
input_details = interpreter.get_input_details()
|
| 61 |
+
output_details = interpreter.get_output_details()
|
| 62 |
+
|
| 63 |
+
x = preprocess_image(img).astype(np.float32)
|
| 64 |
+
|
| 65 |
+
# feed input
|
| 66 |
+
interpreter.set_tensor(input_details[0]['index'], x)
|
| 67 |
+
interpreter.invoke()
|
| 68 |
|
| 69 |
+
# get output
|
| 70 |
+
preds = interpreter.get_tensor(output_details[0]['index'])
|
| 71 |
|
|
|
|
| 72 |
probs = preds[0]
|
| 73 |
idx = int(np.argmax(probs))
|
| 74 |
label = CLASS_NAMES[idx]
|