Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load VGG16 model
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
model = VGG16(weights='imagenet')
|
| 11 |
+
return model
|
| 12 |
+
|
| 13 |
+
model = load_model()
|
| 14 |
+
|
| 15 |
+
st.title(" Image Classification with VGG16")
|
| 16 |
+
st.write("Upload an image and let VGG16 predict what's in it!")
|
| 17 |
+
|
| 18 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 19 |
+
|
| 20 |
+
if uploaded_file:
|
| 21 |
+
img = Image.open(uploaded_file).convert("RGB")
|
| 22 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 23 |
+
|
| 24 |
+
# Preprocess the image
|
| 25 |
+
img_resized = img.resize((224, 224))
|
| 26 |
+
img_array = image.img_to_array(img_resized)
|
| 27 |
+
img_batch = np.expand_dims(img_array, axis=0)
|
| 28 |
+
img_preprocessed = preprocess_input(img_batch)
|
| 29 |
+
|
| 30 |
+
# Predict
|
| 31 |
+
preds = model.predict(img_preprocessed)
|
| 32 |
+
top_preds = decode_predictions(preds, top=3)[0]
|
| 33 |
+
|
| 34 |
+
st.subheader("Top Predictions:")
|
| 35 |
+
for i, (imagenet_id, label, prob) in enumerate(top_preds):
|
| 36 |
+
st.write(f"**{label}** – {prob:.2%}")
|