Niharmahesh commited on
Commit
ee3ddaf
·
verified ·
1 Parent(s): 4e255b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -38
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(image_path):
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
- predicted_class = model.classes_[np.argmax(probabilities)]
71
-
72
- return predicted_class, probabilities, landmarks
73
 
74
- return None, 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
- # Define available alphabets
94
- all_alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
95
- excluded_alphabets = 'DMNPTUVXZ'
96
- available_alphabets = ''.join(set(all_alphabets) - set(excluded_alphabets))
97
 
98
- # Multi-select for alphabets
99
- selected_alphabets = st.multiselect("Select alphabets to recognize:", list(available_alphabets))
 
100
 
101
- if selected_alphabets:
102
- num_selected = len(selected_alphabets)
103
- cols = min(3, num_selected) # Maximum 3 columns
104
- rows = (num_selected + cols - 1) // cols
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
- for j, alphabet in enumerate(row_alphabets):
111
- with cols_in_row[j]:
112
- image_path = f'asl test set/{alphabet}.jpeg'
113
- if os.path.exists(image_path):
114
- predicted_class, probabilities, landmarks = process_and_predict(image_path)
115
- if predicted_class is not None:
116
- st.write(f"Alphabet: {alphabet}")
117
- st.write(f"Predicted: {predicted_class}")
118
- st.write(f"Confidence: {probabilities[model.classes_.tolist().index(predicted_class)]:.2f}")
119
- fig = plot_hand_landmarks(landmarks, f"Hand Landmarks for {alphabet}")
120
- st.pyplot(fig)
121
- else:
122
- st.write(f"No hand detected for {alphabet}")
123
- else:
124
- st.write(f"Image not found for {alphabet}")
125
-
126
- else:
127
- st.write("Please select at least one alphabet to recognize.")
 
 
 
 
 
 
 
 
 
 
 
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}")