import streamlit as st
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
model = load_model('cnn_model.h5')
def process_image(img):
img = img.resize((32, 32))
img = np.array(img)
img = img / 255.0
img = np.expand_dims(img, axis=0)
return img
st.markdown("""
""", unsafe_allow_html=True)
st.markdown('
CIFAR-10 Image Classification 🔎
', unsafe_allow_html=True)
st.markdown('Upload an image to classify it into one of the CIFAR-10 categories.
', unsafe_allow_html=True)
file = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
if file is not None:
img = Image.open(file)
st.markdown('', unsafe_allow_html=True)
st.image(img, caption='Uploaded Image', use_container_width=True, output_format='PNG')
st.markdown('
', unsafe_allow_html=True)
with st.spinner('Processing...'):
image = process_image(img)
predictions = model.predict(image)
predicted_class = np.argmax(predictions)
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
confidence = np.max(predictions) * 100 # Confidence score
st.markdown(f'Prediction: {class_names[predicted_class]}
', unsafe_allow_html=True)
st.markdown(f'Confidence: {confidence:.2f}%
', unsafe_allow_html=True)
st.markdown('Class Probabilities:
', unsafe_allow_html=True)
prob_df = pd.DataFrame(predictions[0], index=class_names, columns=["Probability"])
st.bar_chart(prob_df)
st.markdown('---')
st.markdown("Powered by Streamlit and TensorFlow
", unsafe_allow_html=True)