EnYa32 commited on
Commit
434c883
·
verified ·
1 Parent(s): 704940b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +97 -37
src/streamlit_app.py CHANGED
@@ -1,40 +1,100 @@
1
- import altair as alt
2
  import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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.")