Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| from PIL import Image | |
| # Load VGG16 model | |
| def load_model(): | |
| model = VGG16(weights='imagenet') | |
| return model | |
| model = load_model() | |
| st.title(" Image Classification with VGG16") | |
| st.write("Upload an image and let VGG16 predict what's in it!") | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file: | |
| img = Image.open(uploaded_file).convert("RGB") | |
| st.image(img, caption="Uploaded Image", use_column_width=True) | |
| # Preprocess the image | |
| img_resized = img.resize((224, 224)) | |
| img_array = image.img_to_array(img_resized) | |
| img_batch = np.expand_dims(img_array, axis=0) | |
| img_preprocessed = preprocess_input(img_batch) | |
| # Predict | |
| preds = model.predict(img_preprocessed) | |
| top_preds = decode_predictions(preds, top=3)[0] | |
| st.subheader("Top Predictions:") | |
| for i, (imagenet_id, label, prob) in enumerate(top_preds): | |
| st.write(f"**{label}** – {prob:.2%}") | |