Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# ============================================================
|
| 8 |
+
# 📦 LOAD MODEL
|
| 9 |
+
# ============================================================
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = "animal_model.keras"
|
| 12 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 13 |
+
|
| 14 |
+
# ============================================================
|
| 15 |
+
# 📂 LOAD CLASS LABELS
|
| 16 |
+
# ============================================================
|
| 17 |
+
|
| 18 |
+
with open("class_labels.json", "r") as f:
|
| 19 |
+
class_labels = json.load(f)
|
| 20 |
+
|
| 21 |
+
class_names = list(class_labels.keys())
|
| 22 |
+
|
| 23 |
+
# ============================================================
|
| 24 |
+
# 🖥️ STREAMLIT UI
|
| 25 |
+
# ============================================================
|
| 26 |
+
|
| 27 |
+
st.title("🐾 Animal Classification App")
|
| 28 |
+
st.write("Upload an image and the model will predict the animal.")
|
| 29 |
+
|
| 30 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
|
| 31 |
+
|
| 32 |
+
IMG_SIZE = (160, 160)
|
| 33 |
+
|
| 34 |
+
if uploaded_file is not None:
|
| 35 |
+
image = Image.open(uploaded_file)
|
| 36 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 37 |
+
|
| 38 |
+
# Preprocess
|
| 39 |
+
img = image.resize(IMG_SIZE)
|
| 40 |
+
img_array = np.array(img) / 255.0
|
| 41 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 42 |
+
|
| 43 |
+
# Prediction
|
| 44 |
+
predictions = model.predict(img_array)
|
| 45 |
+
predicted_class = class_names[np.argmax(predictions)]
|
| 46 |
+
|
| 47 |
+
st.subheader("🔍 Prediction:")
|
| 48 |
+
st.success(predicted_class)
|