type-of-cap / src /prediction.py
danupurnomo's picture
Update src/prediction.py
bd0746e verified
import streamlit as st
import pandas as pd
import os
import sys
from io import BytesIO, StringIO
import tensorflow as tf
from PIL import Image
import numpy as np
# load files
model=tf.keras.models.load_model('./src/best_model_sore.keras')
klas = ['baseball_cap', 'beanie_hat', 'bucket_hat', 'fedora_hat', 'flat_cap']
st.title('Jenis Topi')
def run():
picup = st.file_uploader('Upload a picture', type=['jpg', 'jpeg', 'png'])
if picup is not None:
st.image(picup, caption='Uploaded Image', use_column_width=True)
img = Image.open(picup).convert('RGB')
img = img.resize((400,400))
img_array = np.array(img)
img_array = np.expand_dims(img_array, axis=0)
prediction = model.predict(img_array)
pred_index = np.argmax(prediction)
pred_class = klas[pred_index]
confidence = prediction[0][pred_index]*100
st.write(f'Prediction: **{pred_class}** ({confidence:.2f}% confidence)')
st.success(f'Prediction: **{pred_class}** ({confidence:.2f}% confidence)')
if __name__ == '__main__':
run()