Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import keras
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pickle
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="CV2 Image Detection", layout="centered")
|
| 9 |
+
|
| 10 |
+
st.title("🖼️ A Minor Project on Image Detection using CNN and CV2")
|
| 11 |
+
|
| 12 |
+
# Cache the model and label encoder
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model():
|
| 15 |
+
try:
|
| 16 |
+
model_path = r"C:\Users\hi\Desktop\INNOMATICS\CNN\cv2_model.keras"
|
| 17 |
+
label_encoder_path = r"C:\Users\hi\Desktop\INNOMATICS\CNN\label_encoder.pkl"
|
| 18 |
+
|
| 19 |
+
model = keras.models.load_model(model_path)
|
| 20 |
+
with open(label_encoder_path, 'rb') as file:
|
| 21 |
+
label_encoder = pickle.load(file)
|
| 22 |
+
|
| 23 |
+
return model, label_encoder
|
| 24 |
+
except Exception as e:
|
| 25 |
+
st.error(f"Error loading model: {e}")
|
| 26 |
+
return None, None
|
| 27 |
+
|
| 28 |
+
# File uploader for image input
|
| 29 |
+
user_input_img = st.file_uploader("📤 Upload an Image", type=["jpg", "jpeg", "png"])
|
| 30 |
+
|
| 31 |
+
# Initialize session state for prediction
|
| 32 |
+
if "prediction" not in st.session_state:
|
| 33 |
+
st.session_state["prediction"] = None
|
| 34 |
+
st.session_state["uploaded_img"] = None
|
| 35 |
+
|
| 36 |
+
if user_input_img is not None:
|
| 37 |
+
# Load and preprocess image
|
| 38 |
+
img = Image.open(user_input_img)
|
| 39 |
+
img_array = np.array(img)
|
| 40 |
+
img_resized = cv2.resize(img_array, (64, 64))
|
| 41 |
+
img_resized = np.expand_dims(img_resized, axis=0) # Another way to batch dimension instead of np.newaxis
|
| 42 |
+
|
| 43 |
+
# Store image in session state
|
| 44 |
+
st.session_state["uploaded_img"] = img
|
| 45 |
+
|
| 46 |
+
# Button for Prediction
|
| 47 |
+
if st.button("🔍 Predict"):
|
| 48 |
+
if st.session_state['uploaded_img'] is None:
|
| 49 |
+
st.warning("⚠️ Please upload an image before predicting!")
|
| 50 |
+
|
| 51 |
+
else:
|
| 52 |
+
model, encoder = load_model()
|
| 53 |
+
if model and encoder:
|
| 54 |
+
# Make prediction
|
| 55 |
+
predicted_label = encoder.inverse_transform(np.argmax(model.predict(img_resized), axis = 1))
|
| 56 |
+
|
| 57 |
+
# Store prediction in session state
|
| 58 |
+
st.session_state["prediction"] = predicted_label
|
| 59 |
+
|
| 60 |
+
# Display result in two columns if prediction exists
|
| 61 |
+
if st.session_state["prediction"]:
|
| 62 |
+
col1, col2 = st.columns([1, 1])
|
| 63 |
+
|
| 64 |
+
with col1:
|
| 65 |
+
st.success(f"**Prediction:** {st.session_state['prediction']}")
|
| 66 |
+
|
| 67 |
+
with col2:
|
| 68 |
+
st.image(st.session_state["uploaded_img"], caption="Uploaded Image", use_container_width =True)
|