Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -53,8 +53,7 @@ def calculate_angles(landmarks):
|
|
53 |
return angles
|
54 |
|
55 |
# Function to process image and predict alphabet
|
56 |
-
def process_and_predict(
|
57 |
-
image = cv2.imread(image_path)
|
58 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
59 |
results = hands.process(image_rgb)
|
60 |
|
@@ -67,11 +66,9 @@ def process_and_predict(image_path):
|
|
67 |
angles_df = pd.DataFrame([angles], columns=angle_columns)
|
68 |
|
69 |
probabilities = model.predict_proba(angles_df)[0]
|
70 |
-
|
71 |
-
|
72 |
-
return predicted_class, probabilities, landmarks
|
73 |
|
74 |
-
return None, None
|
75 |
|
76 |
# Function to plot hand landmarks
|
77 |
def plot_hand_landmarks(landmarks, title):
|
@@ -90,38 +87,44 @@ def plot_hand_landmarks(landmarks, title):
|
|
90 |
# Streamlit app
|
91 |
st.title("ASL Recognition App")
|
92 |
|
93 |
-
#
|
94 |
-
|
95 |
-
excluded_alphabets = 'DMNPTUVXZ'
|
96 |
-
available_alphabets = ''.join(set(all_alphabets) - set(excluded_alphabets))
|
97 |
|
98 |
-
|
99 |
-
|
|
|
100 |
|
101 |
-
if
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
for i in range(0, num_selected, cols):
|
107 |
-
row_alphabets = selected_alphabets[i:i+cols]
|
108 |
-
cols_in_row = st.columns(len(row_alphabets))
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
return angles
|
54 |
|
55 |
# Function to process image and predict alphabet
|
56 |
+
def process_and_predict(image):
|
|
|
57 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
58 |
results = hands.process(image_rgb)
|
59 |
|
|
|
66 |
angles_df = pd.DataFrame([angles], columns=angle_columns)
|
67 |
|
68 |
probabilities = model.predict_proba(angles_df)[0]
|
69 |
+
return probabilities, landmarks
|
|
|
|
|
70 |
|
71 |
+
return None, None
|
72 |
|
73 |
# Function to plot hand landmarks
|
74 |
def plot_hand_landmarks(landmarks, title):
|
|
|
87 |
# Streamlit app
|
88 |
st.title("ASL Recognition App")
|
89 |
|
90 |
+
# Create two columns
|
91 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
92 |
|
93 |
+
with col1:
|
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 |
+
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
|
99 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
100 |
+
|
101 |
+
probabilities, landmarks = process_and_predict(image)
|
|
|
|
|
|
|
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 |
+
selected_alphabet = st.selectbox("Select an alphabet to draw landmarks:", list(available_alphabets))
|
118 |
+
|
119 |
+
if selected_alphabet:
|
120 |
+
image_path = f'asl test set/{selected_alphabet.lower()}.jpeg'
|
121 |
+
if os.path.exists(image_path):
|
122 |
+
image = cv2.imread(image_path)
|
123 |
+
_, landmarks = process_and_predict(image)
|
124 |
+
if landmarks is not None:
|
125 |
+
fig = plot_hand_landmarks(landmarks, f"Hand Landmarks for {selected_alphabet}")
|
126 |
+
st.pyplot(fig)
|
127 |
+
else:
|
128 |
+
st.write(f"No hand detected for {selected_alphabet}")
|
129 |
+
else:
|
130 |
+
st.write(f"Image not found for {selected_alphabet}")
|