BlanchR2's picture
Update app.py
eb718ce
import streamlit as st
import numpy as np
from PIL import Image
from fastai.vision.all import *
import fastbook
fastbook.setup_book()
# Read list of non venomous
with open('./arizona_non-venomous_snakes.txt', 'r') as file:
non_ven = file.readlines()
non_ven = [i.strip() for i in non_ven]
# Read list of venomous
with open('./arizona_venomous_snakes.txt', 'r') as file:
ven = file.readlines()
ven = [i.strip() for i in ven]
# Create UI
st.markdown('<h2><center>Arizona Snake Classifier</center></h2>', unsafe_allow_html=True) # or try st.header()
uploaded_file = st.file_uploader("Choose an image file of an Arizona snake")
if uploaded_file is not None:
img_path = Path(uploaded_file.name)
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded image')
img_array = np.asarray(image) # convert to array for learner
model = load_learner('arizona_snake_classifier.pkl')
# pred,pred_idx,probs = model.predict(img_array)
# st.write(f'This is a: {pred} snake.')
# st.write(f'Probability: {probs[pred_idx]:.4f}')
# get prediction
pred,pred_idx,probs = model.predict(img_array)
# st.write(f'Prediction: {pred}, Probability: {probs[pred_idx]:.04f}')
add = ''
if pred[0] == 'A' or 'a':
add = 'n'
# output
st.write(f'The model thinks this is a{add} {pred} with probability: {probs[pred_idx]:.4f}')
if pred in ven:
st.write(f'A{add} {pred} is a VENOMOUS snake')
elif pred in non_ven:
st.write(f'\nA{add} {pred} is a non-venomous snake')
else:
st.write('Unknown if this is venomous')
st.write('Have a great day!')