Spaces:
Runtime error
Runtime error
File size: 1,872 Bytes
d93eadc fc9aeb0 d93eadc a75636d d93eadc 899beb9 fc9aeb0 d93eadc 1516bfe d93eadc |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import os
import streamlit as st
import numpy as np
import PIL.Image
#from PIL import Image
from fastai.vision.all import *
import pathlib
import matplotlib.pyplot as pt
plt = platform.system()
if plt == 'Windows': pathlib.PosixPath = pathlib.WindowsPath
model = load_learner('ksl_model.pkl')
def predict(image_path):
# load the image and convert into
# numpy array
#image= Image.open(image)
# image = Image.open(image)
# PIL images into NumPy arrays
pred_label= model.predict(image_path)
return pred_label
def show_likelihood(pred_label):
class_probs = pred_label[2].numpy()
classes = ["Temple", "You", "Me", "You", "Friend", "Love", "Enough", "Church","Mosque"]
class_labels = [classes[i] for i in range(len(class_probs))]
fig = pt.figure(figsize=(10, 10))
pt.barh(class_labels, class_probs)
pt.ylabel("Class")
pt.xlabel("Probability")
pt.title("Class Probabilities")
pt.xlim(0, 1)
pt.ylim(-1, len(class_probs))
st.pyplot(fig)
def main():
st.set_page_config(page_title="Image Classification App", page_icon=":camera:", layout="wide")
st.write("# KSL Image Classification App")
st.write("This app allows you to upload a KSL image and have it classified by a pre-trained machine learning model.")
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
image = PIL.Image.open(uploaded_file)
image_path = os.path.join("tempDir",uploaded_file.name)
with open(image_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.image(image, caption="Uploaded Image", use_column_width=True)
pred_label = predict(image_path)
st.write("The image was classified as:", pred_label[0])
show_likelihood(pred_label)
if __name__ == '__main__':
main()
|