Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,8 @@ 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,35 +55,21 @@ def calculate_angles(landmarks):
|
|
54 |
|
55 |
# Function to process image and predict alphabet
|
56 |
def process_and_predict(image):
|
57 |
-
|
58 |
-
|
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 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
return probabilities, landmarks
|
86 |
|
87 |
# Function to plot hand landmarks
|
88 |
def plot_hand_landmarks(landmarks, title):
|
@@ -98,20 +85,24 @@ def plot_hand_landmarks(landmarks, title):
|
|
98 |
ax.axis('off')
|
99 |
return fig
|
100 |
|
101 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
|
|
|
|
103 |
st.title("ASL Recognition App")
|
104 |
|
105 |
-
#
|
106 |
-
st.
|
107 |
-
|
108 |
-
st.
|
109 |
-
|
110 |
-
|
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"])
|
@@ -121,22 +112,26 @@ with tab1:
|
|
121 |
uploaded_file = st.file_uploader("Upload an image of an ASL sign", type=["jpg", "jpeg", "png"])
|
122 |
|
123 |
if uploaded_file is not None:
|
124 |
-
|
125 |
-
with col1:
|
126 |
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
|
127 |
-
|
128 |
-
|
129 |
-
|
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:
|
136 |
-
st.write(f"{model.classes_[i]}: {probabilities[i]:.2f}")
|
137 |
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
with tab2:
|
142 |
st.header("View Hand Landmarks")
|
@@ -150,19 +145,22 @@ with tab2:
|
|
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(
|
154 |
st.write(f"Attempting to load: {image_path}")
|
155 |
if os.path.exists(image_path):
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
166 |
else:
|
167 |
st.error(f"Image not found for {alphabet}")
|
168 |
|
|
|
7 |
from numpy.linalg import norm
|
8 |
import matplotlib.pyplot as plt
|
9 |
import os
|
10 |
+
import base64
|
11 |
+
|
12 |
# Function to load the Random Forest model
|
13 |
@st.cache_resource
|
14 |
def load_model():
|
|
|
55 |
|
56 |
# Function to process image and predict alphabet
|
57 |
def process_and_predict(image):
|
58 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
59 |
+
results = hands.process(image_rgb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
if results.multi_hand_landmarks:
|
62 |
+
landmarks = np.array([[lm.x, lm.y] for lm in results.multi_hand_landmarks[0].landmark])
|
63 |
+
landmarks_normalized = normalize_landmarks(landmarks)
|
64 |
+
angles = calculate_angles(landmarks_normalized)
|
65 |
+
|
66 |
+
angle_columns = [f'angle_{i}' for i in range(len(angles))]
|
67 |
+
angles_df = pd.DataFrame([angles], columns=angle_columns)
|
68 |
+
|
69 |
+
probabilities = model.predict_proba(angles_df)[0]
|
70 |
+
return probabilities, landmarks
|
71 |
|
72 |
+
return None, None
|
|
|
73 |
|
74 |
# Function to plot hand landmarks
|
75 |
def plot_hand_landmarks(landmarks, title):
|
|
|
85 |
ax.axis('off')
|
86 |
return fig
|
87 |
|
88 |
+
# Function to create a download link for the README file
|
89 |
+
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
90 |
+
with open(bin_file, 'rb') as f:
|
91 |
+
data = f.read()
|
92 |
+
bin_str = base64.b64encode(data).decode()
|
93 |
+
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a>'
|
94 |
+
return href
|
95 |
|
96 |
+
# Streamlit app
|
97 |
+
st.set_page_config(layout="wide")
|
98 |
st.title("ASL Recognition App")
|
99 |
|
100 |
+
# Add README button
|
101 |
+
readme_col1, readme_col2 = st.columns([1, 3])
|
102 |
+
with readme_col1:
|
103 |
+
st.markdown("## How it works")
|
104 |
+
with readme_col2:
|
105 |
+
st.markdown(get_binary_file_downloader_html('readme.md', 'README'), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
106 |
|
107 |
# Create tabs for different functionalities
|
108 |
tab1, tab2 = st.tabs(["Predict ASL Sign", "View Hand Landmarks"])
|
|
|
112 |
uploaded_file = st.file_uploader("Upload an image of an ASL sign", type=["jpg", "jpeg", "png"])
|
113 |
|
114 |
if uploaded_file is not None:
|
115 |
+
try:
|
|
|
116 |
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
|
117 |
+
if image is not None:
|
118 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
119 |
+
probabilities, landmarks = process_and_predict(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
if probabilities is not None and landmarks is not None:
|
122 |
+
st.subheader("Top 5 Predictions:")
|
123 |
+
top_indices = np.argsort(probabilities)[::-1][:5]
|
124 |
+
for i in top_indices:
|
125 |
+
st.write(f"{model.classes_[i]}: {probabilities[i]:.2f}")
|
126 |
+
|
127 |
+
fig = plot_hand_landmarks(landmarks, "Detected Hand Landmarks")
|
128 |
+
st.pyplot(fig)
|
129 |
+
else:
|
130 |
+
st.write("No hand detected in the image.")
|
131 |
+
else:
|
132 |
+
st.error("Failed to load the image. The file might be corrupted.")
|
133 |
+
except Exception as e:
|
134 |
+
st.error(f"An error occurred while processing the image: {str(e)}")
|
135 |
|
136 |
with tab2:
|
137 |
st.header("View Hand Landmarks")
|
|
|
145 |
cols = st.columns(min(3, len(selected_alphabets)))
|
146 |
for idx, alphabet in enumerate(selected_alphabets):
|
147 |
with cols[idx % 3]:
|
148 |
+
image_path = os.path.join('asl test set', f'{alphabet.lower()}.jpeg')
|
149 |
st.write(f"Attempting to load: {image_path}")
|
150 |
if os.path.exists(image_path):
|
151 |
+
try:
|
152 |
+
image = cv2.imread(image_path)
|
153 |
+
if image is not None:
|
154 |
+
probabilities, landmarks = process_and_predict(image)
|
155 |
+
if landmarks is not None:
|
156 |
+
fig = plot_hand_landmarks(landmarks, f"Hand Landmarks for {alphabet}")
|
157 |
+
st.pyplot(fig)
|
158 |
+
else:
|
159 |
+
st.error(f"No hand detected for {alphabet}")
|
160 |
+
else:
|
161 |
+
st.error(f"Failed to load image for {alphabet}. The file might be corrupted.")
|
162 |
+
except Exception as e:
|
163 |
+
st.error(f"An error occurred while processing image for {alphabet}: {str(e)}")
|
164 |
else:
|
165 |
st.error(f"Image not found for {alphabet}")
|
166 |
|