Spaces:
Sleeping
Sleeping
File size: 2,762 Bytes
434c883 82b9fde 434c883 82b9fde 434c883 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 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.") |