Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +97 -37
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,100 @@
|
|
| 1 |
-
import
|
| 2 |
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
from PIL import Image, ImageOps
|
| 5 |
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
from tensorflow.keras.models import load_model
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title="Sign Language Classification", page_icon="🤟", layout="centered")
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = "src/signlanguage_cnn.keras"
|
| 12 |
+
LABEL_MAP_PATH = "src/label_map.json"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@st.cache_resource
|
| 16 |
+
def load_artifacts():
|
| 17 |
+
model = load_model(MODEL_PATH)
|
| 18 |
+
|
| 19 |
+
with open(LABEL_MAP_PATH, "r") as f:
|
| 20 |
+
label_map = json.load(f)
|
| 21 |
+
|
| 22 |
+
# JSON keys are strings -> convert to int keys
|
| 23 |
+
label_map = {int(k): v for k, v in label_map.items()}
|
| 24 |
+
return model, label_map
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def preprocess_image(img: Image.Image) -> np.ndarray:
|
| 28 |
+
# Convert to grayscale
|
| 29 |
+
img = img.convert("L")
|
| 30 |
+
|
| 31 |
+
# Optional: normalize orientation and remove EXIF rotation
|
| 32 |
+
img = ImageOps.exif_transpose(img)
|
| 33 |
+
|
| 34 |
+
# Resize to 28x28 (Sign-MNIST style)
|
| 35 |
+
img = img.resize((28, 28))
|
| 36 |
+
|
| 37 |
+
arr = np.array(img).astype("float32") / 255.0
|
| 38 |
+
|
| 39 |
+
# Shape: (1, 28, 28, 1)
|
| 40 |
+
arr = arr.reshape(1, 28, 28, 1)
|
| 41 |
+
return arr
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
st.title("🤟 Sign Language Classification")
|
| 45 |
+
st.write("Upload an image of a hand sign and the model will predict the class.")
|
| 46 |
+
|
| 47 |
+
with st.sidebar:
|
| 48 |
+
st.header("Files required")
|
| 49 |
+
st.write("- `signlanguage_cnn.keras`")
|
| 50 |
+
st.write("- `label_map.json`")
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
model, label_map = load_artifacts()
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error("Model could not be loaded. Make sure the files are in the repo root:")
|
| 56 |
+
st.code("signlanguage_cnn.keras\nlabel_map.json\napp.py")
|
| 57 |
+
st.exception(e)
|
| 58 |
+
st.stop()
|
| 59 |
+
|
| 60 |
+
uploaded = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
| 61 |
+
|
| 62 |
+
col1, col2 = st.columns(2)
|
| 63 |
+
|
| 64 |
+
if uploaded is not None:
|
| 65 |
+
img = Image.open(uploaded)
|
| 66 |
+
|
| 67 |
+
with col1:
|
| 68 |
+
st.subheader("Input")
|
| 69 |
+
st.image(img, use_container_width=True)
|
| 70 |
+
|
| 71 |
+
x = preprocess_image(img)
|
| 72 |
+
probs = model.predict(x, verbose=0)[0]
|
| 73 |
+
pred_idx = int(np.argmax(probs))
|
| 74 |
+
pred_conf = float(np.max(probs))
|
| 75 |
+
|
| 76 |
+
pred_label = label_map.get(pred_idx, f"Class {pred_idx}")
|
| 77 |
+
|
| 78 |
+
# Top-5
|
| 79 |
+
top_k = 5
|
| 80 |
+
top_idxs = np.argsort(probs)[::-1][:top_k]
|
| 81 |
+
top_rows = []
|
| 82 |
+
for i in top_idxs:
|
| 83 |
+
top_rows.append({
|
| 84 |
+
"class_id": int(i),
|
| 85 |
+
"label": label_map.get(int(i), f"Class {int(i)}"),
|
| 86 |
+
"probability": float(probs[i])
|
| 87 |
+
})
|
| 88 |
+
|
| 89 |
+
with col2:
|
| 90 |
+
st.subheader("Prediction")
|
| 91 |
+
st.metric("Predicted class", pred_label)
|
| 92 |
+
st.metric("Confidence", f"{pred_conf:.2%}")
|
| 93 |
+
|
| 94 |
+
st.write("Top predictions:")
|
| 95 |
+
st.dataframe(top_rows, use_container_width=True)
|
| 96 |
+
|
| 97 |
+
st.divider()
|
| 98 |
+
st.caption("Note: The dataset/model may include a placeholder label like 'Unknown' to handle missing/unused indices.")
|
| 99 |
+
else:
|
| 100 |
+
st.info("Please upload an image to start.")
|