import streamlit as st from PIL import Image import numpy as np import tensorflow as tf from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input, decode_predictions model = MobileNetV2(weights='imagenet') def preprocess_image(image): img = image.resize((224,224)) img_array = np.array(img) img_array = np.expand_dims(img_array, axis=0) img_array = preprocess_input(img_array) return img_array def predict(image): img_array = preprocess_image(image) preds = model.predict(img_array) decoded_preds = decode_predictions(preds, top=1)[0] return decoded_preds def main(): st.set_page_config(page_title='Image Classification', page_icon=":camera_flash:") st.title('Image Classification with MobileNetV2') st.sidebar.title("Options") st.sidebar.write('Upload an image for classification') uploaded_file = st.sidebar.file_uploader("", 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('Classify'): with st.spinner('Classifying...'): prediction = predict(image) st.success('Classification done!') st.write('**Prediction:**') imagenet_id, label, score = prediction[0] st.write(f"- **{label}** (Confidence: {score:2%})") if __name__ == '__main__': main()