| | import streamlit as st |
| | import tensorflow as tf |
| | from tensorflow.keras.preprocessing.image import load_img, img_to_array |
| | import numpy as np |
| | from PIL import Image |
| |
|
| | st.set_page_config( |
| | page_title="RealWaste Image Classification", |
| | layout="centered" |
| | ) |
| |
|
| | @st.cache_resource |
| | def load_model(): |
| | return tf.keras.models.load_model('best_model_cnn.h5') |
| |
|
| | def preprocess_image(img): |
| | img = img.resize((128, 128)) |
| | img = img_to_array(img) |
| | img = np.expand_dims(img, axis=0) |
| | img = img / 255.0 |
| | return img |
| |
|
| | LABEL_CLASS = { |
| | 0: "Cardboard", |
| | 1: "Food Organics", |
| | 2: "Metal", |
| | 3: "Vegetation", |
| | } |
| |
|
| | def main(): |
| | st.title("RealWaste Image Classification") |
| | st.write("Upload an image of waste material, and the model will classify it!") |
| |
|
| | uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
| | |
| | if uploaded_file is not None: |
| | image = Image.open(uploaded_file) |
| | st.image(image, caption='Uploaded Image', use_column_width=True) |
| | |
| | if st.button('Predict'): |
| | model = load_model() |
| | processed_image = preprocess_image(image) |
| | |
| | with st.spinner('Predicting...'): |
| | prediction = model.predict(processed_image) |
| | pred_class = LABEL_CLASS[np.argmax(prediction)] |
| | confidence = float(prediction.max()) * 100 |
| | |
| | st.success(f'Prediction: {pred_class.upper()}') |
| | st.info(f'Confidence: {confidence:.2f}%') |
| | |
| | st.write("Class Probabilities:") |
| | for i, prob in enumerate(prediction[0]): |
| | st.write(f"{LABEL_CLASS[i]}: {float(prob) * 100:.2f}%") |
| | st.progress(float(prob)) |
| |
|
| | if __name__ == "__main__": |
| | main() |