SignLanguageClassification / src /streamlit_app.py
EnYa32's picture
Update src/streamlit_app.py
434c883 verified
Raw History Blame Contribute Delete
2.76 kB
import json
import numpy as np
import streamlit as st
from PIL import Image, ImageOps
import tensorflow as tf
from tensorflow.keras.models import load_model
st.set_page_config(page_title="Sign Language Classification", page_icon="🤟", layout="centered")
MODEL_PATH = "src/signlanguage_cnn.keras"
LABEL_MAP_PATH = "src/label_map.json"
@st.cache_resource
def load_artifacts():
model = load_model(MODEL_PATH)
with open(LABEL_MAP_PATH, "r") as f:
label_map = json.load(f)
# JSON keys are strings -> convert to int keys
label_map = {int(k): v for k, v in label_map.items()}
return model, label_map
def preprocess_image(img: Image.Image) -> np.ndarray:
# Convert to grayscale
img = img.convert("L")
# Optional: normalize orientation and remove EXIF rotation
img = ImageOps.exif_transpose(img)
# Resize to 28x28 (Sign-MNIST style)
img = img.resize((28, 28))
arr = np.array(img).astype("float32") / 255.0
# Shape: (1, 28, 28, 1)
arr = arr.reshape(1, 28, 28, 1)
return arr
st.title("🤟 Sign Language Classification")
st.write("Upload an image of a hand sign and the model will predict the class.")
with st.sidebar:
st.header("Files required")
st.write("- `signlanguage_cnn.keras`")
st.write("- `label_map.json`")
try:
model, label_map = load_artifacts()
except Exception as e:
st.error("Model could not be loaded. Make sure the files are in the repo root:")
st.code("signlanguage_cnn.keras\nlabel_map.json\napp.py")
st.exception(e)
st.stop()
uploaded = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
col1, col2 = st.columns(2)
if uploaded is not None:
img = Image.open(uploaded)
with col1:
st.subheader("Input")
st.image(img, use_container_width=True)
x = preprocess_image(img)
probs = model.predict(x, verbose=0)[0]
pred_idx = int(np.argmax(probs))
pred_conf = float(np.max(probs))
pred_label = label_map.get(pred_idx, f"Class {pred_idx}")
# Top-5
top_k = 5
top_idxs = np.argsort(probs)[::-1][:top_k]
top_rows = []
for i in top_idxs:
top_rows.append({
"class_id": int(i),
"label": label_map.get(int(i), f"Class {int(i)}"),
"probability": float(probs[i])
})
with col2:
st.subheader("Prediction")
st.metric("Predicted class", pred_label)
st.metric("Confidence", f"{pred_conf:.2%}")
st.write("Top predictions:")
st.dataframe(top_rows, use_container_width=True)
st.divider()
st.caption("Note: The dataset/model may include a placeholder label like 'Unknown' to handle missing/unused indices.")
else:
st.info("Please upload an image to start.")