Spaces:
Sleeping
Sleeping
Niharmahesh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -72,59 +72,72 @@ def process_and_predict(image):
|
|
72 |
|
73 |
# Function to plot hand landmarks
|
74 |
def plot_hand_landmarks(landmarks, title):
|
75 |
-
fig, ax = plt.subplots()
|
76 |
-
ax.scatter(landmarks[:, 0], landmarks[:, 1], c='blue')
|
77 |
for connection in mp.solutions.hands.HAND_CONNECTIONS:
|
78 |
start_idx = connection[0]
|
79 |
end_idx = connection[1]
|
80 |
ax.plot([landmarks[start_idx, 0], landmarks[end_idx, 0]],
|
81 |
-
[landmarks[start_idx, 1], landmarks[end_idx, 1]], 'r-')
|
82 |
ax.invert_yaxis()
|
83 |
-
ax.set_title(title)
|
84 |
ax.axis('off')
|
85 |
return fig
|
86 |
|
87 |
# Streamlit app
|
|
|
88 |
st.title("ASL Recognition App")
|
89 |
|
90 |
-
# Create
|
91 |
-
|
92 |
|
93 |
-
with
|
94 |
st.header("Predict ASL Sign")
|
95 |
uploaded_file = st.file_uploader("Upload an image of an ASL sign", type=["jpg", "jpeg", "png"])
|
96 |
|
97 |
if uploaded_file is not None:
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
if probabilities is not None:
|
104 |
-
st.subheader("Top 5 Predictions:")
|
105 |
-
top_indices = np.argsort(probabilities)[::-1][:5]
|
106 |
-
for i in top_indices:
|
107 |
-
st.write(f"{model.classes_[i]}: {probabilities[i]:.2f}")
|
108 |
-
else:
|
109 |
-
st.write("No hand detected in the image.")
|
110 |
-
|
111 |
-
with col2:
|
112 |
-
st.header("Draw Hand Landmarks")
|
113 |
all_alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
114 |
excluded_alphabets = 'DMNPTUVXZ'
|
115 |
available_alphabets = ''.join(set(all_alphabets) - set(excluded_alphabets))
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
if
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# Function to plot hand landmarks
|
74 |
def plot_hand_landmarks(landmarks, title):
|
75 |
+
fig, ax = plt.subplots(figsize=(10, 10))
|
76 |
+
ax.scatter(landmarks[:, 0], landmarks[:, 1], c='blue', s=50)
|
77 |
for connection in mp.solutions.hands.HAND_CONNECTIONS:
|
78 |
start_idx = connection[0]
|
79 |
end_idx = connection[1]
|
80 |
ax.plot([landmarks[start_idx, 0], landmarks[end_idx, 0]],
|
81 |
+
[landmarks[start_idx, 1], landmarks[end_idx, 1]], 'r-', linewidth=2)
|
82 |
ax.invert_yaxis()
|
83 |
+
ax.set_title(title, fontsize=16)
|
84 |
ax.axis('off')
|
85 |
return fig
|
86 |
|
87 |
# Streamlit app
|
88 |
+
st.set_page_config(layout="wide")
|
89 |
st.title("ASL Recognition App")
|
90 |
|
91 |
+
# Create tabs for different functionalities
|
92 |
+
tab1, tab2 = st.tabs(["Predict ASL Sign", "View Hand Landmarks"])
|
93 |
|
94 |
+
with tab1:
|
95 |
st.header("Predict ASL Sign")
|
96 |
uploaded_file = st.file_uploader("Upload an image of an ASL sign", type=["jpg", "jpeg", "png"])
|
97 |
|
98 |
if uploaded_file is not None:
|
99 |
+
col1, col2 = st.columns(2)
|
100 |
+
with col1:
|
101 |
+
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
|
102 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
103 |
+
|
104 |
+
with col2:
|
105 |
+
probabilities, landmarks = process_and_predict(image)
|
106 |
+
|
107 |
+
if probabilities is not None:
|
108 |
+
st.subheader("Top 5 Predictions:")
|
109 |
+
top_indices = np.argsort(probabilities)[::-1][:5]
|
110 |
+
for i in top_indices:
|
111 |
+
st.write(f"{model.classes_[i]}: {probabilities[i]:.2f}")
|
112 |
+
|
113 |
+
fig = plot_hand_landmarks(landmarks, "Detected Hand Landmarks")
|
114 |
+
st.pyplot(fig)
|
115 |
+
else:
|
116 |
+
st.write("No hand detected in the image.")
|
117 |
|
118 |
+
with tab2:
|
119 |
+
st.header("View Hand Landmarks")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
all_alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
121 |
excluded_alphabets = 'DMNPTUVXZ'
|
122 |
available_alphabets = ''.join(set(all_alphabets) - set(excluded_alphabets))
|
123 |
|
124 |
+
selected_alphabets = st.multiselect("Select alphabets to view landmarks:", list(available_alphabets))
|
125 |
+
|
126 |
+
if selected_alphabets:
|
127 |
+
cols = st.columns(min(3, len(selected_alphabets)))
|
128 |
+
for idx, alphabet in enumerate(selected_alphabets):
|
129 |
+
with cols[idx % 3]:
|
130 |
+
image_path = f'asl test set/{alphabet.lower()}.jpeg'
|
131 |
+
if os.path.exists(image_path):
|
132 |
+
image = cv2.imread(image_path)
|
133 |
+
_, landmarks = process_and_predict(image)
|
134 |
+
if landmarks is not None:
|
135 |
+
fig = plot_hand_landmarks(landmarks, f"Hand Landmarks for {alphabet}")
|
136 |
+
st.pyplot(fig)
|
137 |
+
else:
|
138 |
+
st.write(f"No hand detected for {alphabet}")
|
139 |
+
else:
|
140 |
+
st.write(f"Image not found for {alphabet}")
|
141 |
+
|
142 |
+
# Release MediaPipe resources
|
143 |
+
hands.close()
|