frizalul/learn-flower-classification
0
1import streamlit as st2import tensorflow as tf3import numpy as np4from PIL import Image5 6 7st.set_option('deprecation.showfileUploaderEncoding', False)8 9@st.cache(allow_output_mutation=True)10def load_model():11 model = tf.keras.models.load_model('./flower_model_trained.hdf5')12 return model13 14 15def predict_class(image, model):16 17 image = tf.cast(image, tf.float32)18 image = tf.image.resize(image, [180, 180])19 20 image = np.expand_dims(image, axis = 0)21 22 prediction = model.predict(image)23 24 return prediction25 26 27model = load_model()28st.title('Flower Classifier')29 30file = st.file_uploader("Upload an image of a flower", type=["jpg", "png"])31 32 33if file is None:34 st.text('Waiting for upload....')35 36else:37 slot = st.empty()38 slot.text('Running inference....')39 40 test_image = Image.open(file)41 42 st.image(test_image, caption="Input Image", width = 400)43 44 pred = predict_class(np.asarray(test_image), model)45 46 class_names = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']47 48 result = class_names[np.argmax(pred)]49 50 output = 'The image is a ' + result51 52 slot.text('Done')53 54 st.success(output)55 56 