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

Update prediction.py

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