CoolFace
Apppublic

Codiux/Classfy

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py85 linesDownload Raw Back to root
1import streamlit as st2from PIL import Image3import matplotlib.pyplot as plt4import tensorflow_hub as hub5import tensorflow as tf6import numpy as np7from tensorflow import keras8from tensorflow.keras.models import load_model9from tensorflow.keras import preprocessing10import time11fig = plt.figure()12 13with open("custom.css") as f:14    st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)15 16st.title('Bag Classifier')17 18st.markdown("Welcome to this simple web application that classifies bags. The bags are classified into six different classes namely: Backpack, Briefcase, Duffle, Handbag and Purse.")19 20 21def main():22    file_uploaded = st.file_uploader("Choose File", type=["png","jpg","jpeg"])23    class_btn = st.button("Classify")24    if file_uploaded is not None:    25        image = Image.open(file_uploaded)26        st.image(image, caption='Uploaded Image', use_column_width=True)27        28    if class_btn:29        if file_uploaded is None:30            st.write("Invalid command, please upload an image")31        else:32            with st.spinner('Model working....'):33                plt.imshow(image)34                plt.axis("off")35                predictions = predict(image)36                time.sleep(1)37                st.success('Classified')38                st.write(predictions)39                st.pyplot(fig)40 41 42def predict(image):43    classifier_model = "base_dir.h5"44    IMAGE_SHAPE = (224, 224,3)45    model = load_model(classifier_model, compile=False, custom_objects={'KerasLayer': hub.KerasLayer})46    test_image = image.resize((224,224))47    test_image = preprocessing.image.img_to_array(test_image)48    test_image = test_image / 255.049    test_image = np.expand_dims(test_image, axis=0)50    class_names = [51          'Backpack',52          'Briefcase',53          'Duffle', 54          'Handbag', 55          'Purse']56    predictions = model.predict(test_image)57    scores = tf.nn.softmax(predictions[0])58    scores = scores.numpy()59    results = {60          'Backpack': 0,61          'Briefcase': 0,62          'Duffle': 0, 63          'Handbag': 0, 64          'Purse': 065}66 67    68    result = f"{class_names[np.argmax(scores)]} with a { (100 * np.max(scores)).round(2) } % confidence." 69    return result70 71 72 73 74 75 76 77 78 79    80 81if __name__ == "__main__":82    main()83 84 85