Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- src/app6.py +68 -0
- src/rice_model.tflite +3 -0
src/app6.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# 1. Instellingen
|
| 8 |
+
# -----------------------------
|
| 9 |
+
# Gebruik dezelfde image size als bij training!
|
| 10 |
+
IMG_SIZE = (224, 224) # of (128, 128) als je model daarop is getraind
|
| 11 |
+
|
| 12 |
+
CLASS_NAMES = ['Karacadag', 'Basmati', 'Jasmine', 'Arborio', 'Ipsala']
|
| 13 |
+
|
| 14 |
+
st.set_page_config(page_title="Rice Classifier", page_icon="🌾")
|
| 15 |
+
st.title("🌾 Rice Classifier")
|
| 16 |
+
st.write("Upload een rijstkorrel-afbeelding om het type te laten voorspellen.")
|
| 17 |
+
|
| 18 |
+
# -----------------------------
|
| 19 |
+
# 2. Laad TFLite model
|
| 20 |
+
# -----------------------------
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_interpreter():
|
| 23 |
+
interpreter = tf.lite.Interpreter(model_path="rice_model.tflite")
|
| 24 |
+
interpreter.allocate_tensors()
|
| 25 |
+
input_details = interpreter.get_input_details()
|
| 26 |
+
output_details = interpreter.get_output_details()
|
| 27 |
+
return interpreter, input_details, output_details
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
interpreter, input_details, output_details = load_interpreter()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.error(f"Kon TFLite-model niet laden: {e}")
|
| 33 |
+
st.stop()
|
| 34 |
+
|
| 35 |
+
# -----------------------------
|
| 36 |
+
# 3. Upload afbeelding
|
| 37 |
+
# -----------------------------
|
| 38 |
+
uploaded = st.file_uploader("Upload een afbeelding", type=["jpg", "jpeg", "png"])
|
| 39 |
+
|
| 40 |
+
if uploaded is None:
|
| 41 |
+
st.info("👆 Kies hierboven een afbeelding.")
|
| 42 |
+
else:
|
| 43 |
+
# Toon de geüploade afbeelding
|
| 44 |
+
img = Image.open(uploaded)
|
| 45 |
+
st.image(img, caption="Geüploade afbeelding", use_container_width=True)
|
| 46 |
+
|
| 47 |
+
# Zorg dat het echt RGB is
|
| 48 |
+
if img.mode != "RGB":
|
| 49 |
+
img = img.convert("RGB")
|
| 50 |
+
|
| 51 |
+
# Resize + normaliseer
|
| 52 |
+
img = img.resize(IMG_SIZE)
|
| 53 |
+
img_array = np.array(img) / 255.0
|
| 54 |
+
img_array = np.expand_dims(img_array, axis=0).astype(np.float32)
|
| 55 |
+
|
| 56 |
+
# -----------------------------
|
| 57 |
+
# 4. Voorspelling
|
| 58 |
+
# -----------------------------
|
| 59 |
+
interpreter.set_tensor(input_details[0]['index'], img_array)
|
| 60 |
+
interpreter.invoke()
|
| 61 |
+
prediction = interpreter.get_tensor(output_details[0]['index'])
|
| 62 |
+
|
| 63 |
+
idx = int(np.argmax(prediction))
|
| 64 |
+
confidence = float(prediction[0][idx])
|
| 65 |
+
|
| 66 |
+
st.subheader("🔍 Resultaat")
|
| 67 |
+
st.write(f"**Predicted class:** {CLASS_NAMES[idx]}")
|
| 68 |
+
st.write(f"**Confidence:** {confidence:.4f}")
|
src/rice_model.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4255f5ffed912644bd475693dd3b6af51de3d3965a3a8a7ea789e3fee17e6776
|
| 3 |
+
size 16687744
|