Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import tensorflow as tf | |
| from tensorflow.keras.applications import MobileNet | |
| from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| # Load the pre-trained MobileNet model | |
| model = MobileNet(weights='imagenet') | |
| # Create a Streamlit web app | |
| st.title("Image Classification with MobileNet") | |
| # Upload an image through Streamlit | |
| uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"]) | |
| if uploaded_image is not None: | |
| # Display the uploaded image | |
| st.image(uploaded_image, caption='Uploaded Image', use_column_width=True) | |
| # Preprocess the image for MobileNet | |
| img = image.load_img(uploaded_image, target_size=(224, 224)) | |
| img_array = image.img_to_array(img) | |
| img_array = preprocess_input(img_array) | |
| img_array = np.expand_dims(img_array, axis=0) | |
| # Classify the image using MobileNet | |
| predictions = model.predict(img_array) | |
| decoded_predictions = decode_predictions(predictions, top=3)[0] | |
| st.subheader("Top Predictions:") | |
| for i, (imagenet_id, label, score) in enumerate(decoded_predictions): | |
| st.write(f"{i + 1}: {label} ({score:.2f})") | |