File size: 1,149 Bytes
2a06144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
@st.cache_resource
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%}")