BeyzaTopbas commited on
Commit
03b6b48
·
verified ·
1 Parent(s): 9d667f0

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +66 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,68 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
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}")