File size: 2,296 Bytes
a775bfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image

# -----------------------------
# 1. Instellingen
# -----------------------------
# Gebruik dezelfde image size als bij training!
IMG_SIZE = (224, 224)   # of (128, 128) als je model daarop is getraind

CLASS_NAMES = ['Karacadag', 'Basmati', 'Jasmine', 'Arborio', 'Ipsala']

st.set_page_config(page_title="Rice Classifier", page_icon="🌾")
st.title("🌾 Rice Classifier")
st.write("Upload een rijstkorrel-afbeelding om het type te laten voorspellen.")

# -----------------------------
# 2. Laad TFLite model
# -----------------------------
@st.cache_resource
def load_interpreter():
    interpreter = tf.lite.Interpreter(model_path="rice_model.tflite")
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    return interpreter, input_details, output_details

try:
    interpreter, input_details, output_details = load_interpreter()
except Exception as e:
    st.error(f"Kon TFLite-model niet laden: {e}")
    st.stop()

# -----------------------------
# 3. Upload afbeelding
# -----------------------------
uploaded = st.file_uploader("Upload een afbeelding", type=["jpg", "jpeg", "png"])

if uploaded is None:
    st.info("👆 Kies hierboven een afbeelding.")
else:
    # Toon de geüploade afbeelding
    img = Image.open(uploaded)
    st.image(img, caption="Geüploade afbeelding", use_container_width=True)

    # Zorg dat het echt RGB is
    if img.mode != "RGB":
        img = img.convert("RGB")

    # Resize + normaliseer
    img = img.resize(IMG_SIZE)
    img_array = np.array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0).astype(np.float32)

    # -----------------------------
    # 4. Voorspelling
    # -----------------------------
    interpreter.set_tensor(input_details[0]['index'], img_array)
    interpreter.invoke()
    prediction = interpreter.get_tensor(output_details[0]['index'])

    idx = int(np.argmax(prediction))
    confidence = float(prediction[0][idx])

    st.subheader("🔍 Resultaat")
    st.write(f"**Predicted class:** {CLASS_NAMES[idx]}")
    st.write(f"**Confidence:** {confidence:.4f}")