miracle01's picture
Update app.py
9f412c6 verified
raw
history blame
1.75 kB
# All imports
import streamlit as st
import tensorflow as tf
from PIL import Image
import io
import numpy as np
def load_image():
uploaded_file = st.file_uploader(label='Pick an image to test')
if uploaded_file is not None:
image_data = uploaded_file.getvalue()
st.image(image_data)
img = Image.open(io.BytesIO(image_data))
img = img.resize((224,224))
return img
else:
return None
def load_model():
model_name = 'Model/model.h5'
model = tf.keras.models.load_model(model_name)
return model
def load_labels():
with open('Oxford-102_Flower_dataset_labels.txt', 'r') as file:
data = file.read().splitlines()
return data
def predict(model, labels, img):
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
prediction = model.predict(img_array)
predicted_class = np.argmax(prediction[0], axis=-1)
flower = labels[predicted_class]
closeness = np.round(prediction[0][predicted_class] * 100, 2)
return flower, closeness
def main():
st.title('Oxford 102 Flower Classification Demo')
model = load_model()
labels = load_labels()
image = load_image()
result = st.button('Run on image')
if result and image is not None:
st.markdown('**_Calculating results..._**')
flower, closeness = predict(model, labels, image)
st.markdown(f'<h3 style="color:blue;">Flower Type: <span style="color:black;">{flower}</span></h3>', unsafe_allow_html=True)
st.markdown(f'<h3 style="color:green;">Closeness: <span style="color:black;">{closeness}%</span></h3>', unsafe_allow_html=True)
if __name__ == '__main__':
main()