Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from numpy.linalg import norm
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import os
|
| 10 |
import base64
|
|
|
|
| 11 |
st.set_page_config(layout="wide")
|
| 12 |
# Function to load the Random Forest model
|
| 13 |
@st.cache_resource
|
|
@@ -25,15 +26,6 @@ model = load_model()
|
|
| 25 |
if model is None:
|
| 26 |
st.stop()
|
| 27 |
|
| 28 |
-
# Initialize MediaPipe Hands
|
| 29 |
-
@st.cache_resource
|
| 30 |
-
def load_mediapipe_model():
|
| 31 |
-
mp_hands = mp.solutions.hands
|
| 32 |
-
return mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5)
|
| 33 |
-
|
| 34 |
-
hands = load_mediapipe_model()
|
| 35 |
-
mp_drawing = mp.solutions.drawing_utils
|
| 36 |
-
|
| 37 |
# Function to normalize landmarks
|
| 38 |
def normalize_landmarks(landmarks):
|
| 39 |
center = np.mean(landmarks, axis=0)
|
|
@@ -55,19 +47,21 @@ def calculate_angles(landmarks):
|
|
| 55 |
|
| 56 |
# Function to process image and predict alphabet
|
| 57 |
def process_and_predict(image):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
return None, None
|
| 73 |
|
|
@@ -75,7 +69,8 @@ def process_and_predict(image):
|
|
| 75 |
def plot_hand_landmarks(landmarks, title):
|
| 76 |
fig, ax = plt.subplots(figsize=(10, 10))
|
| 77 |
ax.scatter(landmarks[:, 0], landmarks[:, 1], c='blue', s=50)
|
| 78 |
-
|
|
|
|
| 79 |
start_idx = connection[0]
|
| 80 |
end_idx = connection[1]
|
| 81 |
ax.plot([landmarks[start_idx, 0], landmarks[end_idx, 0]],
|
|
@@ -162,8 +157,4 @@ with tab2:
|
|
| 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 |
-
|
| 167 |
-
# Don't close hands here, as it might be None
|
| 168 |
-
if hands is not None:
|
| 169 |
-
hands.close()
|
|
|
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import os
|
| 10 |
import base64
|
| 11 |
+
|
| 12 |
st.set_page_config(layout="wide")
|
| 13 |
# Function to load the Random Forest model
|
| 14 |
@st.cache_resource
|
|
|
|
| 26 |
if model is None:
|
| 27 |
st.stop()
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Function to normalize landmarks
|
| 30 |
def normalize_landmarks(landmarks):
|
| 31 |
center = np.mean(landmarks, axis=0)
|
|
|
|
| 47 |
|
| 48 |
# Function to process image and predict alphabet
|
| 49 |
def process_and_predict(image):
|
| 50 |
+
mp_hands = mp.solutions.hands
|
| 51 |
+
with mp_hands.Hands(static_image_mode=True, max_num_hands=1, min_detection_confidence=0.5) as hands:
|
| 52 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 53 |
+
results = hands.process(image_rgb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
if results.multi_hand_landmarks:
|
| 56 |
+
landmarks = np.array([[lm.x, lm.y] for lm in results.multi_hand_landmarks[0].landmark])
|
| 57 |
+
landmarks_normalized = normalize_landmarks(landmarks)
|
| 58 |
+
angles = calculate_angles(landmarks_normalized)
|
| 59 |
+
|
| 60 |
+
angle_columns = [f'angle_{i}' for i in range(len(angles))]
|
| 61 |
+
angles_df = pd.DataFrame([angles], columns=angle_columns)
|
| 62 |
+
|
| 63 |
+
probabilities = model.predict_proba(angles_df)[0]
|
| 64 |
+
return probabilities, landmarks
|
| 65 |
|
| 66 |
return None, None
|
| 67 |
|
|
|
|
| 69 |
def plot_hand_landmarks(landmarks, title):
|
| 70 |
fig, ax = plt.subplots(figsize=(10, 10))
|
| 71 |
ax.scatter(landmarks[:, 0], landmarks[:, 1], c='blue', s=50)
|
| 72 |
+
mp_hands = mp.solutions.hands
|
| 73 |
+
for connection in mp_hands.HAND_CONNECTIONS:
|
| 74 |
start_idx = connection[0]
|
| 75 |
end_idx = connection[1]
|
| 76 |
ax.plot([landmarks[start_idx, 0], landmarks[end_idx, 0]],
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
st.error(f"An error occurred while processing image for {alphabet}: {str(e)}")
|
| 159 |
else:
|
| 160 |
+
st.error(f"Image not found for {alphabet}")
|
|
|
|
|
|
|
|
|
|
|
|