Spaces:
Sleeping
Sleeping
Niharmahesh
commited on
Commit
•
44db7f1
1
Parent(s):
060bc0b
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import pandas as pd
|
|
7 |
from numpy.linalg import norm
|
8 |
import matplotlib.pyplot as plt
|
9 |
import os
|
10 |
-
|
11 |
# Function to load the Random Forest model
|
12 |
@st.cache_resource
|
13 |
def load_model():
|
@@ -54,21 +54,35 @@ def calculate_angles(landmarks):
|
|
54 |
|
55 |
# Function to process image and predict alphabet
|
56 |
def process_and_predict(image):
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
landmarks_normalized = normalize_landmarks(landmarks)
|
63 |
-
angles = calculate_angles(landmarks_normalized)
|
64 |
-
|
65 |
-
angle_columns = [f'angle_{i}' for i in range(len(angles))]
|
66 |
-
angles_df = pd.DataFrame([angles], columns=angle_columns)
|
67 |
-
|
68 |
-
probabilities = model.predict_proba(angles_df)[0]
|
69 |
-
return probabilities, landmarks
|
70 |
|
71 |
-
|
|
|
72 |
|
73 |
# Function to plot hand landmarks
|
74 |
def plot_hand_landmarks(landmarks, title):
|
@@ -85,8 +99,20 @@ def plot_hand_landmarks(landmarks, title):
|
|
85 |
return fig
|
86 |
|
87 |
# Streamlit app
|
|
|
88 |
st.title("ASL Recognition App")
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
# Create tabs for different functionalities
|
91 |
tab1, tab2 = st.tabs(["Predict ASL Sign", "View Hand Landmarks"])
|
92 |
|
@@ -103,7 +129,7 @@ with tab1:
|
|
103 |
with col2:
|
104 |
probabilities, landmarks = process_and_predict(image)
|
105 |
|
106 |
-
if probabilities is not None:
|
107 |
st.subheader("Top 5 Predictions:")
|
108 |
top_indices = np.argsort(probabilities)[::-1][:5]
|
109 |
for i in top_indices:
|
@@ -111,8 +137,6 @@ with tab1:
|
|
111 |
|
112 |
fig = plot_hand_landmarks(landmarks, "Detected Hand Landmarks")
|
113 |
st.pyplot(fig)
|
114 |
-
else:
|
115 |
-
st.write("No hand detected in the image.")
|
116 |
|
117 |
with tab2:
|
118 |
st.header("View Hand Landmarks")
|
@@ -126,17 +150,19 @@ with tab2:
|
|
126 |
cols = st.columns(min(3, len(selected_alphabets)))
|
127 |
for idx, alphabet in enumerate(selected_alphabets):
|
128 |
with cols[idx % 3]:
|
129 |
-
image_path = f'
|
|
|
130 |
if os.path.exists(image_path):
|
131 |
image = cv2.imread(image_path)
|
132 |
-
|
|
|
|
|
|
|
133 |
if landmarks is not None:
|
134 |
fig = plot_hand_landmarks(landmarks, f"Hand Landmarks for {alphabet}")
|
135 |
st.pyplot(fig)
|
136 |
-
else:
|
137 |
-
st.write(f"No hand detected for {alphabet}")
|
138 |
else:
|
139 |
-
st.
|
140 |
|
141 |
# Release MediaPipe resources
|
142 |
hands.close()
|
|
|
7 |
from numpy.linalg import norm
|
8 |
import matplotlib.pyplot as plt
|
9 |
import os
|
10 |
+
|
11 |
# Function to load the Random Forest model
|
12 |
@st.cache_resource
|
13 |
def load_model():
|
|
|
54 |
|
55 |
# Function to process image and predict alphabet
|
56 |
def process_and_predict(image):
|
57 |
+
if image is None:
|
58 |
+
st.error("Failed to load the image. Please check if the file exists and is not corrupted.")
|
59 |
+
return None, None
|
60 |
+
|
61 |
+
try:
|
62 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
63 |
+
except cv2.error:
|
64 |
+
st.error("Failed to convert the image. The image might be corrupted or in an unsupported format.")
|
65 |
+
return None, None
|
66 |
+
|
67 |
+
try:
|
68 |
+
results = hands.process(image_rgb)
|
69 |
+
except Exception as e:
|
70 |
+
st.error(f"An error occurred while processing the image: {str(e)}")
|
71 |
+
return None, None
|
72 |
+
|
73 |
+
if not results.multi_hand_landmarks:
|
74 |
+
st.warning("No hands detected in the image.")
|
75 |
+
return None, None
|
76 |
+
|
77 |
+
landmarks = np.array([[lm.x, lm.y] for lm in results.multi_hand_landmarks[0].landmark])
|
78 |
+
landmarks_normalized = normalize_landmarks(landmarks)
|
79 |
+
angles = calculate_angles(landmarks_normalized)
|
80 |
|
81 |
+
angle_columns = [f'angle_{i}' for i in range(len(angles))]
|
82 |
+
angles_df = pd.DataFrame([angles], columns=angle_columns)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
probabilities = model.predict_proba(angles_df)[0]
|
85 |
+
return probabilities, landmarks
|
86 |
|
87 |
# Function to plot hand landmarks
|
88 |
def plot_hand_landmarks(landmarks, title):
|
|
|
99 |
return fig
|
100 |
|
101 |
# Streamlit app
|
102 |
+
st.set_page_config(layout="wide")
|
103 |
st.title("ASL Recognition App")
|
104 |
|
105 |
+
# Debug information
|
106 |
+
st.write("Current working directory:", os.getcwd())
|
107 |
+
image_directory = 'asl test set'
|
108 |
+
st.write("Image directory path:", os.path.abspath(image_directory))
|
109 |
+
|
110 |
+
if os.path.exists(image_directory):
|
111 |
+
image_files = os.listdir(image_directory)
|
112 |
+
st.write("Files in the image directory:", image_files)
|
113 |
+
else:
|
114 |
+
st.error(f"The directory '{image_directory}' does not exist.")
|
115 |
+
|
116 |
# Create tabs for different functionalities
|
117 |
tab1, tab2 = st.tabs(["Predict ASL Sign", "View Hand Landmarks"])
|
118 |
|
|
|
129 |
with col2:
|
130 |
probabilities, landmarks = process_and_predict(image)
|
131 |
|
132 |
+
if probabilities is not None and landmarks is not None:
|
133 |
st.subheader("Top 5 Predictions:")
|
134 |
top_indices = np.argsort(probabilities)[::-1][:5]
|
135 |
for i in top_indices:
|
|
|
137 |
|
138 |
fig = plot_hand_landmarks(landmarks, "Detected Hand Landmarks")
|
139 |
st.pyplot(fig)
|
|
|
|
|
140 |
|
141 |
with tab2:
|
142 |
st.header("View Hand Landmarks")
|
|
|
150 |
cols = st.columns(min(3, len(selected_alphabets)))
|
151 |
for idx, alphabet in enumerate(selected_alphabets):
|
152 |
with cols[idx % 3]:
|
153 |
+
image_path = os.path.join(image_directory, f'{alphabet.lower()}.jpeg')
|
154 |
+
st.write(f"Attempting to load: {image_path}")
|
155 |
if os.path.exists(image_path):
|
156 |
image = cv2.imread(image_path)
|
157 |
+
if image is None:
|
158 |
+
st.error(f"Failed to load image for {alphabet}. The file might be corrupted.")
|
159 |
+
continue
|
160 |
+
probabilities, landmarks = process_and_predict(image)
|
161 |
if landmarks is not None:
|
162 |
fig = plot_hand_landmarks(landmarks, f"Hand Landmarks for {alphabet}")
|
163 |
st.pyplot(fig)
|
|
|
|
|
164 |
else:
|
165 |
+
st.error(f"Image not found for {alphabet}")
|
166 |
|
167 |
# Release MediaPipe resources
|
168 |
hands.close()
|