miracle01/Flower-Classification
0
1# All imports2import streamlit as st3import tensorflow as tf4from PIL import Image5import io6import numpy as np7 8def load_image():9 uploaded_file = st.file_uploader(label='Pick an image to test')10 if uploaded_file is not None:11 image_data = uploaded_file.getvalue()12 st.image(image_data)13 img = Image.open(io.BytesIO(image_data))14 img = img.resize((224,224))15 return img16 else:17 return None18 19def load_model():20 model_name = 'Model/model.h5'21 model = tf.keras.models.load_model(model_name) 22 return model23 24def load_labels():25 with open('Oxford-102_Flower_dataset_labels.txt', 'r') as file:26 data = file.read().splitlines()27 return data28 29def predict(model, labels, img):30 img_array = tf.keras.preprocessing.image.img_to_array(img)31 img_array = tf.expand_dims(img_array, 0) # Create a batch32 33 prediction = model.predict(img_array)34 predicted_class = np.argmax(prediction[0], axis=-1)35 36 flower = labels[predicted_class]37 closeness = np.round(prediction[0][predicted_class] * 100, 2)38 39 return flower, closeness40 41def main():42 st.title('Flower Classification Using Deep Learning ')43 st.markdown('### NAME:')44 st.write('TOLULOPE')45 st.markdown('### CLASS:')46 st.write('HND2')47 st.markdown('### LEVEL:')48 st.write('400L')49 st.markdown('---')50 st.write("This is a demo of an image classification model trained on the Oxford Flower Dataset. The Oxford Flower Dataset, consisting of 102 flower categories. The images have large scale, pose and light variations. In addition, there are categories that have large variations within the category and several very similar categories. The dataset is visualized using isomap with shape and colour features. Link to the dataset is available @ https://www.kaggle.com/datasets/yousefmohamed20/oxford-102-flower-dataset.. . To test the model, upload an image of a flower and click the 'Run on image' button.")51 st.markdown('---')52 model = load_model()53 labels = load_labels()54 image = load_image()55 result = st.button('Run on image')56 if result and image is not None:57 st.markdown('**_Calculating results..._**')58 flower, closeness = predict(model, labels, image)59 st.markdown(f'<h4 style="color:blue;">Flower Type: <span style="color:black;">{flower}</span></h4>', unsafe_allow_html=True)60 st.markdown(f'<h4 style="color:green;">Closeness: <span style="color:black;">{closeness}%</span></h4>', unsafe_allow_html=True)61 62if __name__ == '__main__':63 main()64 