Spaces:
Build error
Build error
File size: 1,585 Bytes
c0d244f 7b547aa e879fda c0d244f 7b547aa 5b9f78b 7b547aa c0d244f 7b547aa c0d244f |
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 |
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
def run():
st.title("Rice Classifier")
st.write("Upload a picture of rice to predict its type..")
# File upload
uploaded_file = st.file_uploader("Select the rice image...", type=["jpg", "jpeg", "png"])
# Mendefinisikan dimensi gambar
img_height, img_width = 220, 220
# Function to preprocess the uploaded image
def preprocess_image(image):
img = image.resize((img_height, img_width))
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
return img_array
# Mapping numerical predictions to class labels
class_labels = {0: "Arborio", 1: "Basmati", 2: "Ipsala", 3: "Jasmine", 4: "Karacadag"}
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Gambar yang diunggah", use_column_width=True)
# Load the model (once the image is uploaded)
model = load_model("cnn_model.keras")
# Preprocess and predict
img_array = preprocess_image(image)
prediction = model.predict(img_array)
predicted_class = np.argmax(prediction, axis=1)[0]
# Display prediction
st.write(f'Prediksi: {class_labels[predicted_class]}')
else:
st.text("Please upload an image file")
if __name__ == "__main__":
main()
|