darly9991 commited on
Commit
c0d244f
·
verified ·
1 Parent(s): fd1fd5d

Update prediction.py

Browse files
Files changed (1) hide show
  1. prediction.py +44 -43
prediction.py CHANGED
@@ -1,43 +1,44 @@
1
- import streamlit as st
2
- from PIL import Image
3
- import numpy as np
4
- from tensorflow.keras.models import load_model
5
- from tensorflow.keras.preprocessing.image import img_to_array
6
-
7
- # Function to preprocess the uploaded image
8
- def preprocess_image(image):
9
- img = image.resize((img_height, img_width))
10
- img_array = img_to_array(img)
11
- img_array = np.expand_dims(img_array, axis=0)
12
- img_array /= 255.0
13
- return img_array
14
-
15
- # Mapping numerical predictions to class labels
16
- class_labels = {0: "Arborio", 1: "Basmati", 2: "Ipsala", 3: "Jasmine", 4: "Karacadag"}
17
-
18
- # Main function to run the Streamlit app
19
- def main():
20
- st.title("Rice Classifier")
21
- st.write("Upload a picture of rice to predict its type..")
22
-
23
- # File upload
24
- uploaded_file = st.file_uploader("Select the rice image...", type=["jpg", "jpeg", "png"])
25
-
26
- if uploaded_file is not None:
27
- # Display the uploaded image
28
- image = Image.open(uploaded_file)
29
- st.image(image, caption="Gambar yang diunggah", use_column_width=True)
30
-
31
- # Load the model (once the image is uploaded)
32
- model = load_model("my_model.keras")
33
-
34
- # Preprocess and predict
35
- img_array = preprocess_image(image)
36
- prediction = model.predict(img_array)
37
- predicted_class = np.argmax(prediction, axis=1)[0]
38
-
39
- # Display prediction
40
- st.write(f'Prediksi: {class_labels[predicted_class]}')
41
-
42
- if __name__ == "__main__":
43
- main()
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.preprocessing.image import img_to_array
6
+
7
+ def run():
8
+ # Function to preprocess the uploaded image
9
+ def preprocess_image(image):
10
+ img = image.resize((img_height, img_width))
11
+ img_array = img_to_array(img)
12
+ img_array = np.expand_dims(img_array, axis=0)
13
+ img_array /= 255.0
14
+ return img_array
15
+
16
+ # Mapping numerical predictions to class labels
17
+ class_labels = {0: "Arborio", 1: "Basmati", 2: "Ipsala", 3: "Jasmine", 4: "Karacadag"}
18
+
19
+ # Main function to run the Streamlit app
20
+ def main():
21
+ st.title("Rice Classifier")
22
+ st.write("Upload a picture of rice to predict its type..")
23
+
24
+ # File upload
25
+ uploaded_file = st.file_uploader("Select the rice image...", type=["jpg", "jpeg", "png"])
26
+
27
+ if uploaded_file is not None:
28
+ # Display the uploaded image
29
+ image = Image.open(uploaded_file)
30
+ st.image(image, caption="Gambar yang diunggah", use_column_width=True)
31
+
32
+ # Load the model (once the image is uploaded)
33
+ model = load_model("my_model.keras")
34
+
35
+ # Preprocess and predict
36
+ img_array = preprocess_image(image)
37
+ prediction = model.predict(img_array)
38
+ predicted_class = np.argmax(prediction, axis=1)[0]
39
+
40
+ # Display prediction
41
+ st.write(f'Prediksi: {class_labels[predicted_class]}')
42
+
43
+ if __name__ == "__main__":
44
+ main()