-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (64 loc) · 1.93 KB
/
app.py
File metadata and controls
76 lines (64 loc) · 1.93 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import streamlit as st
from tensorflow.keras.models import load_model
from predictor import predict_cat_dog
import tempfile
from PIL import Image
# ---------------------------
# Page configuration
# ---------------------------
st.set_page_config(
page_title="Cat vs Dog Image Classifier",
page_icon="🐱",
layout="centered"
)
# ---------------------------
# Title & description
# ---------------------------
st.title("🐱🐶 Cat vs Dog Image Classification")
st.markdown(
"""
This project uses a **Convolutional Neural Network (CNN)** trained on image data
to classify whether an uploaded image contains a **Cat** or a **Dog**.
"""
)
# ---------------------------
# Load model once (important)
# ---------------------------
@st.cache_resource
def load_cnn_model():
return load_model("cat_vs_dog")
model = load_cnn_model()
# ---------------------------
# Image uploader
# ---------------------------
uploaded_file = st.file_uploader(
"Upload an image (JPG / PNG)",
type=["jpg", "jpeg", "png"]
)
# ---------------------------
# Prediction logic
# ---------------------------
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Save image temporarily for prediction
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
image.save(tmp.name)
img_path = tmp.name
label, confidence = predict_cat_dog(img_path, model)
# ---------------------------
# Display result
# ---------------------------
st.markdown("---")
st.subheader("🧠 Model Prediction")
st.markdown(f"### **{label}**")
st.progress(confidence)
st.markdown(f"**Confidence:** {confidence:.2f}")
# ---------------------------
# Footer
# ---------------------------
st.markdown("---")
st.markdown(
"<center><small>Built with TensorFlow & Streamlit</small></center>",
unsafe_allow_html=True
)