Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import pickle
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Set page configuration
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Image Detection App",
|
| 12 |
+
page_icon="📸",
|
| 13 |
+
layout="centered",
|
| 14 |
+
initial_sidebar_state="expanded"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Load the trained model and label encoder
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def load_resources():
|
| 20 |
+
model = load_model("captains_cv2_model.keras")
|
| 21 |
+
with open("label_encoder.pkl", "rb") as f:
|
| 22 |
+
le = pickle.load(f)
|
| 23 |
+
return model, le
|
| 24 |
+
|
| 25 |
+
model, label_encoder = load_resources()
|
| 26 |
+
|
| 27 |
+
# Function to preprocess the uploaded image
|
| 28 |
+
def preprocess_image(uploaded_file):
|
| 29 |
+
# Save the uploaded file temporarily to disk
|
| 30 |
+
temp_path = "temp_image.jpg"
|
| 31 |
+
with open(temp_path, "wb") as f:
|
| 32 |
+
f.write(uploaded_file.read())
|
| 33 |
+
|
| 34 |
+
# Read the image using cv2.imread
|
| 35 |
+
img = cv2.imread(temp_path)
|
| 36 |
+
# Resize to the model's expected input size (64, 64)
|
| 37 |
+
img = cv2.resize(img, (64, 64)) # Note: cv2.resize takes (width, height), not (height, width, channels)
|
| 38 |
+
# Add new axis for batch dimension
|
| 39 |
+
img = img[np.newaxis, :, :, :]
|
| 40 |
+
|
| 41 |
+
# Clean up the temporary file
|
| 42 |
+
os.remove(temp_path)
|
| 43 |
+
return img
|
| 44 |
+
|
| 45 |
+
# Sidebar
|
| 46 |
+
st.sidebar.title("About")
|
| 47 |
+
st.sidebar.info(
|
| 48 |
+
"This app uses a Convolutional Neural Network (CNN) to classify images into one of 10 categories. "
|
| 49 |
+
"Upload an image, and the model will predict its class!"
|
| 50 |
+
)
|
| 51 |
+
st.sidebar.markdown("### Classes")
|
| 52 |
+
st.sidebar.write(
|
| 53 |
+
"The model can predict: lifeboat, ladybug, pizza, bell pepper, school bus, koala, espresso, red panda, orange, sports car."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Main content
|
| 57 |
+
st.title("📸 Image Classification App")
|
| 58 |
+
st.markdown("Upload an image below, and let the model predict its class!")
|
| 59 |
+
|
| 60 |
+
# File uploader
|
| 61 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 62 |
+
|
| 63 |
+
if uploaded_file is not None:
|
| 64 |
+
# Display the uploaded image
|
| 65 |
+
image = Image.open(uploaded_file)
|
| 66 |
+
uploaded_file.seek(0) # Reset file pointer after reading for display
|
| 67 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 68 |
+
|
| 69 |
+
# Preprocess the image
|
| 70 |
+
processed_image = preprocess_image(uploaded_file)
|
| 71 |
+
|
| 72 |
+
# Make prediction
|
| 73 |
+
with st.spinner("Predicting..."):
|
| 74 |
+
# Predict and decode as per your specified steps
|
| 75 |
+
prediction = model.predict(processed_image)
|
| 76 |
+
predicted_class_idx = np.argmax(prediction, axis=1)[0]
|
| 77 |
+
predicted_class = label_encoder.inverse_transform([predicted_class_idx])[0]
|
| 78 |
+
|
| 79 |
+
# Display the prediction
|
| 80 |
+
st.success("Prediction complete!")
|
| 81 |
+
st.markdown(f"### Predicted Class: **{predicted_class}**")
|
| 82 |
+
st.write(f"Prediction Confidence: {prediction[0][predicted_class_idx]:.4f}")
|
| 83 |
+
|
| 84 |
+
# Optional: Display confidence scores for all classes
|
| 85 |
+
if st.checkbox("Show confidence scores for all classes"):
|
| 86 |
+
class_names = label_encoder.classes_
|
| 87 |
+
confidence_scores = {class_names[i]: float(prediction[0][i]) for i in range(len(class_names))}
|
| 88 |
+
st.bar_chart(confidence_scores)
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
st.info("Please upload an image to get started.")
|
| 92 |
+
|
| 93 |
+
# Footer
|
| 94 |
+
st.markdown("---")
|
| 95 |
+
st.markdown("Created with ❤️ using Streamlit | Hosted on [Hugging Face Spaces](https://huggingface.co/spaces)")
|