Spaces:
Sleeping
Sleeping
DSCmatter commited on
Commit ·
8c52ee1
1
Parent(s): da15db4
slight change
Browse files- app.py +11 -22
- requirements.txt +6 -3
app.py
CHANGED
|
@@ -1,20 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
from
|
| 4 |
-
from tensorflow.keras.preprocessing import image
|
| 5 |
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
-
# --- Load Your Model and Class Names ---
|
| 10 |
-
# Use st.cache_resource to load the model only once
|
| 11 |
@st.cache_resource
|
| 12 |
def load_my_model():
|
| 13 |
-
|
| 14 |
-
model = load_model('resnet50_dryfruits.h5')
|
| 15 |
-
return model
|
| 16 |
|
| 17 |
-
# --- This is the updated dictionary based on your list ---
|
| 18 |
class_names = {
|
| 19 |
0: 'AlmondGrade_A',
|
| 20 |
1: 'CashewGrade_B',
|
|
@@ -26,31 +21,25 @@ class_names = {
|
|
| 26 |
7: 'WalnutGrade_A',
|
| 27 |
8: 'CashewGrade_C'
|
| 28 |
}
|
| 29 |
-
# --------------------------------------------------------
|
| 30 |
|
| 31 |
model = load_my_model()
|
| 32 |
|
| 33 |
-
# --- App Interface ---
|
| 34 |
st.title("Dry Fruit Quality Grader")
|
| 35 |
st.write("Upload an image of a dry fruit, and the model will predict its grade.")
|
| 36 |
|
| 37 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 38 |
|
| 39 |
if uploaded_file is not None:
|
| 40 |
-
|
| 41 |
-
img = Image.open(uploaded_file).convert('RGB') # Ensure 3 channels
|
| 42 |
img = img.resize((224, 224))
|
| 43 |
img_array = image.img_to_array(img)
|
| 44 |
img_batch = np.expand_dims(img_array, axis=0)
|
| 45 |
img_preprocessed = preprocess_input(img_batch)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
confidence = np.max(prediction[0])
|
| 52 |
-
|
| 53 |
-
# 3. Display results
|
| 54 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 55 |
-
st.markdown(f"## Prediction: **{
|
| 56 |
st.markdown(f"### Confidence: **{confidence * 100:.2f}%**")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
# import tensorflow as tf # not needed just to infer
|
| 3 |
+
from tf_keras.models import load_model
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
|
|
|
|
|
|
|
| 9 |
@st.cache_resource
|
| 10 |
def load_my_model():
|
| 11 |
+
return load_model("resnet50_dryfruits.h5", compile=False)
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
class_names = {
|
| 14 |
0: 'AlmondGrade_A',
|
| 15 |
1: 'CashewGrade_B',
|
|
|
|
| 21 |
7: 'WalnutGrade_A',
|
| 22 |
8: 'CashewGrade_C'
|
| 23 |
}
|
|
|
|
| 24 |
|
| 25 |
model = load_my_model()
|
| 26 |
|
|
|
|
| 27 |
st.title("Dry Fruit Quality Grader")
|
| 28 |
st.write("Upload an image of a dry fruit, and the model will predict its grade.")
|
| 29 |
|
| 30 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 31 |
|
| 32 |
if uploaded_file is not None:
|
| 33 |
+
img = Image.open(uploaded_file).convert("RGB")
|
|
|
|
| 34 |
img = img.resize((224, 224))
|
| 35 |
img_array = image.img_to_array(img)
|
| 36 |
img_batch = np.expand_dims(img_array, axis=0)
|
| 37 |
img_preprocessed = preprocess_input(img_batch)
|
| 38 |
+
|
| 39 |
+
preds = model.predict(img_preprocessed)
|
| 40 |
+
idx = int(np.argmax(preds[0]))
|
| 41 |
+
confidence = float(np.max(preds[0]))
|
| 42 |
+
|
|
|
|
|
|
|
|
|
|
| 43 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 44 |
+
st.markdown(f"## Prediction: **{class_names[idx]}**")
|
| 45 |
st.markdown(f"### Confidence: **{confidence * 100:.2f}%**")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.39.0
|
| 2 |
+
tensorflow-cpu==2.17.0
|
| 3 |
+
tf-keras==2.17.0
|
| 4 |
+
h5py>=3.10
|
| 5 |
+
Pillow
|
| 6 |
+
numpy
|